Skip to content

Commit

Permalink
feat(CardScroll): rename prop noSpaces to padding (#7788)
Browse files Browse the repository at this point in the history
  • Loading branch information
EldarMuhamethanov authored Oct 21, 2024
1 parent bfe4df7 commit 98faaac
Show file tree
Hide file tree
Showing 17 changed files with 231 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { Card, CardScroll } from '@vkontakte/vkui';
import React from 'react';

const App = () => {
return (
<React.Fragment>
{/* noSpaces by default -> padding=true */}
<CardScroll
size="s"
>
<Card>
<div style={{ paddingBottom: '66%' }} />
</Card>
<Card>
<div style={{ paddingBottom: '66%' }} />
</Card>
</CardScroll>

{/* noSpaces="false" -> padding="true" */}
<CardScroll
size="s"
noSpaces={false}
>
<Card>
<div style={{ paddingBottom: '66%' }} />
</Card>
<Card>
<div style={{ paddingBottom: '66%' }} />
</Card>
</CardScroll>

{/* noSpaces="true" -> padding="false" */}
<CardScroll
size="s"
noSpaces={true}
>
<Card>
<div style={{ paddingBottom: '66%' }} />
</Card>
<Card>
<div style={{ paddingBottom: '66%' }} />
</Card>
</CardScroll>

{/* noSpaces="true" -> padding="false" */}
<CardScroll
size="s"
noSpaces
>
<Card>
<div style={{ paddingBottom: '66%' }} />
</Card>
<Card>
<div style={{ paddingBottom: '66%' }} />
</Card>
</CardScroll>
</React.Fragment>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`card-scroll transforms correctly 1`] = `
"import { Card, CardScroll } from '@vkontakte/vkui';
import React from 'react';
const App = () => {
return (
(<React.Fragment>
{/* noSpaces by default -> padding=true */}
<CardScroll size="s" padding>
<Card>
<div style={{ paddingBottom: '66%' }} />
</Card>
<Card>
<div style={{ paddingBottom: '66%' }} />
</Card>
</CardScroll>
{/* noSpaces="false" -> padding="true" */}
<CardScroll
size="s"
padding
>
<Card>
<div style={{ paddingBottom: '66%' }} />
</Card>
<Card>
<div style={{ paddingBottom: '66%' }} />
</Card>
</CardScroll>
{/* noSpaces="true" -> padding="false" */}
<CardScroll size="s">
<Card>
<div style={{ paddingBottom: '66%' }} />
</Card>
<Card>
<div style={{ paddingBottom: '66%' }} />
</Card>
</CardScroll>
{/* noSpaces="true" -> padding="false" */}
<CardScroll size="s">
<Card>
<div style={{ paddingBottom: '66%' }} />
</Card>
<Card>
<div style={{ paddingBottom: '66%' }} />
</Card>
</CardScroll>
</React.Fragment>)
);
};"
`;
12 changes: 12 additions & 0 deletions packages/codemods/src/transforms/v7/__tests__/card-scroll.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
jest.autoMockOff();

import { defineSnapshotTestFromFixture } from '../../../testHelpers/testHelper';

const name = 'card-scroll';
const fixtures = ['basic'] as const;

describe(name, () => {
fixtures.forEach((test) =>
defineSnapshotTestFromFixture(__dirname, name, global.TRANSFORM_OPTIONS, `${name}/${test}`),
);
});
70 changes: 70 additions & 0 deletions packages/codemods/src/transforms/v7/card-scroll.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { API, FileInfo, JSXAttribute, JSXSpreadAttribute } from 'jscodeshift';
import { getImportInfo, removeAttribute } from '../../codemod-helpers';
import { report } from '../../report';
import { JSCodeShiftOptions } from '../../types';

export const parser = 'tsx';

export default function transformer(file: FileInfo, api: API, options: JSCodeShiftOptions) {
const { alias } = options;
const j = api.jscodeshift;
const source = j(file.source);
const { localName } = getImportInfo(j, file, 'CardScroll', alias);

if (!localName) {
return source.toSource();
}

const getValueFromAttribute = (attribute: JSXAttribute): boolean | null => {
if (attribute.value?.type === 'BooleanLiteral') {
return attribute.value.value;
}
if (attribute.value?.type === 'JSXExpressionContainer') {
const expression = attribute.value.expression;
if (expression.type === 'BooleanLiteral') {
return expression.value;
}
return null;
}
return true;
};

const addPropPadding = (attributes: Array<JSXAttribute | JSXSpreadAttribute> | undefined) => {
attributes?.push(j.jsxAttribute(j.jsxIdentifier('padding')));
};

source
.find(j.JSXElement, {
openingElement: {
name: {
name: localName,
},
},
})
.forEach((path) => {
const attributes = path.node.openingElement.attributes;
const noSpacesAttr: JSXAttribute | undefined = attributes?.find(
(attr) => attr.type === 'JSXAttribute' && attr.name.name === 'noSpaces',
) as JSXAttribute | undefined;

if (!noSpacesAttr) {
addPropPadding(attributes);
return;
}
const attrValue = getValueFromAttribute(noSpacesAttr);
if (attrValue === null) {
report(
api,
`Manual changes required for ${localName}'s "noSpaces" prop. Need to change it to "padding" prop`,
);
return;
}
removeAttribute(attributes, noSpacesAttr);

if (!attrValue) {
addPropPadding(attributes);
}
});

return source.toSource();
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ export const CardScrollPlayground = (props: ComponentPlaygroundProps) => {
{...props}
propSets={[
{
padding: [true],
size: ['s', 'm', 'l', false],
},
{
padding: [true],
showArrows: [true, false, 'always'],
},
{
noSpaces: [false, true],
padding: [false],
},
]}
>
Expand Down
18 changes: 9 additions & 9 deletions packages/vkui/src/components/CardScroll/CardScroll.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
margin-block-start: 16px;
}

.withSpaces .gap {
.withPaddings .gap {
inline-size: var(--vkui--size_base_padding_horizontal--regular);
}

Expand All @@ -29,15 +29,15 @@
*/
/* stylelint-disable-next-line selector-pseudo-class-disallowed-list */
:global(.vkuiInternalSplitCol--viewWidth-tabletPlus):global(.vkuiInternalSplitCol--spaced-auto)
.withSpaces
.withPaddings
.gap {
inline-size: 16px;
}

@media (--viewWidth-smallTabletPlus) {
/* stylelint-disable-next-line selector-pseudo-class-disallowed-list */
:global(.vkuiInternalSplitCol--viewWidth-none):global(.vkuiInternalSplitCol--spaced-auto)
.withSpaces
.withPaddings
.gap {
inline-size: 16px;
}
Expand All @@ -48,33 +48,33 @@
* Group
*/
/* stylelint-disable-next-line selector-pseudo-class-disallowed-list */
:global(.vkuiInternalGroup--mode-card) .withSpaces {
:global(.vkuiInternalGroup--mode-card) .withPaddings {
margin-inline: -8px;
}

/* stylelint-disable-next-line selector-pseudo-class-disallowed-list */
:global(.vkuiInternalGroup--mode-card) .withSpaces:first-child {
:global(.vkuiInternalGroup--mode-card) .withPaddings:first-child {
padding-block-start: var(--vkui--size_cardgrid_padding_vertical--regular);
}

/* stylelint-disable-next-line selector-pseudo-class-disallowed-list */
:global(.vkuiInternalGroup--mode-card) .withSpaces:last-child {
:global(.vkuiInternalGroup--mode-card) .withPaddings:last-child {
padding-block-end: var(--vkui--size_cardgrid_padding_vertical--regular);
}

@media (--sizeX-regular) {
/* stylelint-disable-next-line selector-pseudo-class-disallowed-list */
:global(.vkuiInternalGroup--mode-none) .withSpaces {
:global(.vkuiInternalGroup--mode-none) .withPaddings {
margin-inline: -8px;
}

/* stylelint-disable-next-line selector-pseudo-class-disallowed-list */
:global(.vkuiInternalGroup--mode-none) .withSpaces:first-child {
:global(.vkuiInternalGroup--mode-none) .withPaddings:first-child {
padding-block-start: var(--vkui--size_cardgrid_padding_vertical--regular);
}

/* stylelint-disable-next-line selector-pseudo-class-disallowed-list */
:global(.vkuiInternalGroup--mode-none) .withSpaces:last-child {
:global(.vkuiInternalGroup--mode-none) .withPaddings:last-child {
padding-block-end: var(--vkui--size_cardgrid_padding_vertical--regular);
}
}
9 changes: 6 additions & 3 deletions packages/vkui/src/components/CardScroll/CardScroll.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ export interface CardScrollProps extends HTMLAttributesWithRootRef<HTMLDivElemen
*/
size?: 's' | 'm' | 'l' | false;
showArrows?: HorizontalScrollProps['showArrows'];
noSpaces?: boolean;
/**
* Добавляет отступы по краям слева и справа
*/
padding?: boolean;
}

/**
Expand All @@ -30,7 +33,7 @@ export const CardScroll = ({
children,
size = 's',
showArrows = true,
noSpaces = false,
padding = false,
Component = 'ul',
...restProps
}: CardScrollProps): React.ReactNode => {
Expand Down Expand Up @@ -99,7 +102,7 @@ export const CardScroll = ({
styles.host,
'vkuiInternalCardScroll',
size !== false && stylesSize[size],
!noSpaces && styles.withSpaces,
padding && styles.withPaddings,
)}
>
<HorizontalScroll
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 98faaac

Please sign in to comment.