-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(CellButton): mv mode to appearance (#7684)
h2. Описание см. #7683 h2. Release notes h2. BREAKING CHANGE - CellButton: свойство `mode` заменено на `appearance` со значениями `'accent' | 'neutral' | 'negative'`, также для `appearance="accent"` (a.k.a `mode="primary"`) возвращён токен `--vkui--color_text_accent`, а вот при комбинации с `centered` применяется `--vkui--color_text_accent_themed` ```diff <CellButton - mode="danger" + appearance="negative" > Создать что-нибудь </CellButton> <CellButton - mode="primary" + appearance="accent" > Создать что-нибудь </CellButton> ```
- Loading branch information
Showing
32 changed files
with
233 additions
and
66 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
packages/codemods/src/transforms/v7/__testfixtures__/cell-button/basic.input.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { CellButton } from '@vkontakte/vkui'; | ||
import React from 'react'; | ||
|
||
const App = () => { | ||
return ( | ||
<React.Fragment> | ||
{/* mode="primary" -> appearance="accent" */} | ||
<CellButton mode="primary"> | ||
Создать что-нибудь | ||
</CellButton> | ||
|
||
{/* mode="primary" -> appearance="accent" */} | ||
<CellButton mode={"primary"}> | ||
Создать что-нибудь | ||
</CellButton> | ||
|
||
{/* mode="danger" -> appearance="negative" */} | ||
<CellButton mode="danger"> | ||
Создать что-нибудь | ||
</CellButton> | ||
|
||
{/* mode="danger" -> appearance="negative" */} | ||
<CellButton mode={"danger"}> | ||
Создать что-нибудь | ||
</CellButton> | ||
|
||
{/* do nothing 1 */} | ||
<CellButton> | ||
Создать что-нибудь | ||
</CellButton> | ||
|
||
{/* do nothing 2 */} | ||
<CellButton centered> | ||
Создать что-нибудь | ||
</CellButton> | ||
</React.Fragment> | ||
); | ||
}; |
37 changes: 37 additions & 0 deletions
37
packages/codemods/src/transforms/v7/__tests__/__snapshots__/cell-button.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`cell-button transforms correctly 1`] = ` | ||
"import { CellButton } from '@vkontakte/vkui'; | ||
import React from 'react'; | ||
const App = () => { | ||
return ( | ||
(<React.Fragment> | ||
{/* mode="primary" -> appearance="accent" */} | ||
<CellButton appearance="accent"> | ||
Создать что-нибудь | ||
</CellButton> | ||
{/* mode="primary" -> appearance="accent" */} | ||
<CellButton appearance="accent"> | ||
Создать что-нибудь | ||
</CellButton> | ||
{/* mode="danger" -> appearance="negative" */} | ||
<CellButton appearance="negative"> | ||
Создать что-нибудь | ||
</CellButton> | ||
{/* mode="danger" -> appearance="negative" */} | ||
<CellButton appearance="negative"> | ||
Создать что-нибудь | ||
</CellButton> | ||
{/* do nothing 1 */} | ||
<CellButton> | ||
Создать что-нибудь | ||
</CellButton> | ||
{/* do nothing 2 */} | ||
<CellButton centered> | ||
Создать что-нибудь | ||
</CellButton> | ||
</React.Fragment>) | ||
); | ||
};" | ||
`; |
11 changes: 11 additions & 0 deletions
11
packages/codemods/src/transforms/v7/__tests__/cell-button.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
jest.autoMockOff(); | ||
import { defineSnapshotTestFromFixture } from '../../../testHelpers/testHelper'; | ||
|
||
const name = 'cell-button'; | ||
const fixtures = ['basic'] as const; | ||
|
||
describe(name, () => { | ||
fixtures.forEach((test) => | ||
defineSnapshotTestFromFixture(__dirname, name, global.TRANSFORM_OPTIONS, `${name}/${test}`), | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { API, FileInfo, JSXAttribute } from 'jscodeshift'; | ||
import { getImportInfo } 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, 'CellButton', alias); | ||
if (!localName) { | ||
return source.toSource(); | ||
} | ||
|
||
const attributeToReplace = 'mode'; | ||
const newAttributeName = 'appearance'; | ||
|
||
const modeToAppearance: Record<string, string> = { | ||
primary: 'accent', | ||
danger: 'negative', | ||
}; | ||
|
||
const getValueFromAttribute = (attribute: JSXAttribute): string | null => { | ||
if (attribute.value?.type === 'StringLiteral') { | ||
return attribute.value.value; | ||
} | ||
if (attribute.value?.type === 'JSXExpressionContainer') { | ||
const expression = attribute.value.expression; | ||
if (expression.type === 'StringLiteral') { | ||
return expression.value; | ||
} | ||
} | ||
return null; | ||
}; | ||
|
||
source | ||
.find(j.JSXElement, { openingElement: { name: { name: localName } } }) | ||
.find(j.JSXAttribute, { name: { name: attributeToReplace } }) | ||
.forEach((path) => { | ||
const component = path.node; | ||
component.name.name = newAttributeName; | ||
const value = getValueFromAttribute(component); | ||
if (!value || !modeToAppearance[value]) { | ||
report( | ||
api, | ||
`: ${localName} has been changed. Manual changes required: need to change 'mode' prop to 'appearance'`, | ||
); | ||
return; | ||
} | ||
component.value = j.stringLiteral(modeToAppearance[value]); | ||
}); | ||
|
||
return source.toSource(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 17 additions & 11 deletions
28
packages/vkui/src/components/CellButton/CellButton.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,31 @@ | ||
import { render, screen } from '@testing-library/react'; | ||
import { baselineComponent } from '../../testing/utils'; | ||
import { CellButton } from './CellButton'; | ||
import { appearanceClassNames, CellButton } from './CellButton'; | ||
import styles from './CellButton.module.css'; | ||
|
||
describe('CellButton', () => { | ||
describe(CellButton, () => { | ||
baselineComponent((props) => <CellButton {...props}>CellButton</CellButton>); | ||
|
||
it('should have danger className with mode danger', () => { | ||
render( | ||
<CellButton mode="danger" data-testid="cell"> | ||
Check | ||
</CellButton>, | ||
); | ||
expect(screen.getByTestId('cell')).toHaveClass(styles.modeDanger); | ||
}); | ||
it('should have danger className with mode danger', () => { | ||
it.each(['accent', 'neutral', 'negative', undefined] as const)( | ||
'should have expected className for appearance="%s"', | ||
(appearance) => { | ||
render( | ||
<CellButton appearance={appearance} data-testid="cell"> | ||
Check | ||
</CellButton>, | ||
); | ||
expect(screen.getByTestId('cell')).toHaveClass( | ||
appearance ? appearanceClassNames[appearance] : appearanceClassNames.accent, | ||
); | ||
}, | ||
); | ||
it('should have appearance="accent" by default if centered', () => { | ||
render( | ||
<CellButton centered data-testid="cell"> | ||
Check | ||
</CellButton>, | ||
); | ||
expect(screen.getByTestId('cell')).toHaveClass(styles.centered); | ||
expect(screen.getByTestId('cell')).toHaveClass(appearanceClassNames.accent); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...ents/CellButton/__image_snapshots__/cellbutton-android-chromium-dark-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions
4
...nts/CellButton/__image_snapshots__/cellbutton-android-chromium-light-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions
4
...components/CellButton/__image_snapshots__/cellbutton-ios-webkit-dark-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions
4
...omponents/CellButton/__image_snapshots__/cellbutton-ios-webkit-light-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions
4
...onents/CellButton/__image_snapshots__/cellbutton-vkcom-chromium-dark-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions
4
...nents/CellButton/__image_snapshots__/cellbutton-vkcom-chromium-light-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions
4
...ponents/CellButton/__image_snapshots__/cellbutton-vkcom-firefox-dark-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions
4
...onents/CellButton/__image_snapshots__/cellbutton-vkcom-firefox-light-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions
4
...mponents/CellButton/__image_snapshots__/cellbutton-vkcom-webkit-dark-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions
4
...ponents/CellButton/__image_snapshots__/cellbutton-vkcom-webkit-light-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions
4
...mponents/FormItem/__image_snapshots__/formitem-android-chromium-dark-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion
2
...ponents/FormItem/__image_snapshots__/formitem-android-chromium-light-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions
4
...src/components/FormItem/__image_snapshots__/formitem-ios-webkit-dark-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions
4
...components/FormItem/__image_snapshots__/formitem-vkcom-chromium-dark-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions
4
...omponents/FormItem/__image_snapshots__/formitem-vkcom-chromium-light-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions
4
.../components/FormItem/__image_snapshots__/formitem-vkcom-firefox-dark-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions
4
...components/FormItem/__image_snapshots__/formitem-vkcom-firefox-light-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.