From 18bbe409322aae1f6475de1d4b52485ce5515ff3 Mon Sep 17 00:00:00 2001 From: sai chand <60743144+sai6855@users.noreply.github.com> Date: Fri, 3 Jan 2025 18:40:45 +0530 Subject: [PATCH] [material-ui][Select] Deprecate composed classes (#44925) --- .../migrating-from-deprecated-apis.md | 46 +++++++ docs/pages/material-ui/api/select.json | 9 +- docs/translations/api-docs/select/select.json | 9 +- packages/mui-codemod/README.md | 48 +++++++ .../src/deprecations/all/deprecations-all.js | 2 + .../src/deprecations/all/postcss.config.js | 2 + .../src/deprecations/select-classes/index.js | 1 + .../select-classes/postcss-plugin.js | 37 ++++++ .../select-classes/postcss.config.js | 5 + .../select-classes/select-classes.js | 125 ++++++++++++++++++ .../select-classes/select-classes.test.js | 78 +++++++++++ .../select-classes/test-cases/actual.css | 11 ++ .../select-classes/test-cases/actual.js | 8 ++ .../select-classes/test-cases/expected.css | 11 ++ .../select-classes/test-cases/expected.js | 8 ++ .../mui-material/src/Select/Select.test.js | 18 +++ .../mui-material/src/Select/selectClasses.ts | 12 +- 17 files changed, 421 insertions(+), 9 deletions(-) create mode 100644 packages/mui-codemod/src/deprecations/select-classes/index.js create mode 100644 packages/mui-codemod/src/deprecations/select-classes/postcss-plugin.js create mode 100644 packages/mui-codemod/src/deprecations/select-classes/postcss.config.js create mode 100644 packages/mui-codemod/src/deprecations/select-classes/select-classes.js create mode 100644 packages/mui-codemod/src/deprecations/select-classes/select-classes.test.js create mode 100644 packages/mui-codemod/src/deprecations/select-classes/test-cases/actual.css create mode 100644 packages/mui-codemod/src/deprecations/select-classes/test-cases/actual.js create mode 100644 packages/mui-codemod/src/deprecations/select-classes/test-cases/expected.css create mode 100644 packages/mui-codemod/src/deprecations/select-classes/test-cases/expected.js diff --git a/docs/data/material/migration/migrating-from-deprecated-apis/migrating-from-deprecated-apis.md b/docs/data/material/migration/migrating-from-deprecated-apis/migrating-from-deprecated-apis.md index e05e1b56ee9715..24c27c33051d02 100644 --- a/docs/data/material/migration/migrating-from-deprecated-apis/migrating-from-deprecated-apis.md +++ b/docs/data/material/migration/migrating-from-deprecated-apis/migrating-from-deprecated-apis.md @@ -1528,6 +1528,52 @@ The Popper's prop `componentsProps` was deprecated in favor of `slotProps`: /> ``` +## Select + +Use the [codemod](https://github.com/mui/material-ui/tree/HEAD/packages/mui-codemod#select-classes) below to migrate the code as described in the following sections: + +```bash +npx @mui/codemod@latest deprecations/select-classes +``` + +### Composed CSS classes + +The CSS classes that composed the `icon` class and `variant` prop were removed. + +Here's how to migrate: + +```diff +- .MuiSelect-iconFilled ++ .MuiSelect-filled ~ .MuiSelect-icon +- .MuiSelect-iconOutlined ++ .MuiSelect-outlined ~ .MuiSelect-icon +- .MuiSelect-iconStandard ++ .MuiSelect-standard ~ .MuiSelect-icon +``` + +```diff + import { selectClasses } from '@mui/material/Select'; + + MuiSelect: { + styleOverrides: { + root: { +- [`& .${selectClasses.iconFilled}`]: { ++ [`& .${selectClasses.filled} ~ .${selectClasses.icon}`]: { + color: 'red', + }, +- [`& .${selectClasses.iconOutlined}`]: { ++ [`& .${selectClasses.outlined} ~ .${selectClasses.icon}`]: { + color: 'red', + }, +- [`& .${selectClasses.iconStandard}`]: { ++ [`& .${selectClasses.standard} ~ .${selectClasses.icon}`]: { + color: 'red', + }, + }, + }, + }, +``` + ## Slider Use the [codemod](https://github.com/mui/material-ui/tree/HEAD/packages/mui-codemod#slider-props) below to migrate the code as described in the following sections: diff --git a/docs/pages/material-ui/api/select.json b/docs/pages/material-ui/api/select.json index da48bad8df8888..10a81c97dc1f8f 100644 --- a/docs/pages/material-ui/api/select.json +++ b/docs/pages/material-ui/api/select.json @@ -96,7 +96,8 @@ "key": "iconFilled", "className": "MuiSelect-iconFilled", "description": "Styles applied to the icon component if `variant=\"filled\"`.", - "isGlobal": false + "isGlobal": false, + "isDeprecated": true }, { "key": "iconOpen", @@ -108,13 +109,15 @@ "key": "iconOutlined", "className": "MuiSelect-iconOutlined", "description": "Styles applied to the icon component if `variant=\"outlined\"`.", - "isGlobal": false + "isGlobal": false, + "isDeprecated": true }, { "key": "iconStandard", "className": "MuiSelect-iconStandard", "description": "Styles applied to the icon component if `variant=\"standard\"`.", - "isGlobal": false + "isGlobal": false, + "isDeprecated": true }, { "key": "multiple", diff --git a/docs/translations/api-docs/select/select.json b/docs/translations/api-docs/select/select.json index bd82f2ae26b5db..00349fcfb4081a 100644 --- a/docs/translations/api-docs/select/select.json +++ b/docs/translations/api-docs/select/select.json @@ -97,7 +97,8 @@ "iconFilled": { "description": "Styles applied to {{nodeName}} if {{conditions}}.", "nodeName": "the icon component", - "conditions": "variant=\"filled\"" + "conditions": "variant=\"filled\"", + "deprecationInfo": "Combine the .MuiSelect-icon and .MuiSelect-filled classes instead. See Migrating from deprecated APIs for more details." }, "iconOpen": { "description": "Styles applied to {{nodeName}} if {{conditions}}.", @@ -107,12 +108,14 @@ "iconOutlined": { "description": "Styles applied to {{nodeName}} if {{conditions}}.", "nodeName": "the icon component", - "conditions": "variant=\"outlined\"" + "conditions": "variant=\"outlined\"", + "deprecationInfo": "Combine the .MuiSelect-icon and .MuiSelect-outlined classes instead. See Migrating from deprecated APIs for more details." }, "iconStandard": { "description": "Styles applied to {{nodeName}} if {{conditions}}.", "nodeName": "the icon component", - "conditions": "variant=\"standard\"" + "conditions": "variant=\"standard\"", + "deprecationInfo": "Combine the .MuiSelect-icon and .MuiSelect-standard classes instead. See Migrating from deprecated APIs for more details." }, "multiple": { "description": "Styles applied to {{nodeName}} if {{conditions}}.", diff --git a/packages/mui-codemod/README.md b/packages/mui-codemod/README.md index 92fb762bf05f5d..aa51cb7a8373c2 100644 --- a/packages/mui-codemod/README.md +++ b/packages/mui-codemod/README.md @@ -1406,6 +1406,54 @@ npx @mui/codemod@latest deprecations/popper-props npx @mui/codemod@latest deprecations/outlined-input-props ``` +#### `select-classes` + +JS transforms: + +```diff + import { selectClasses } from '@mui/material/Select'; + + MuiSelect: { + styleOverrides: { + root: { +- [`& .${selectClasses.iconFilled}`]: { ++ [`& .${selectClasses.filled} ~ .${selectClasses.icon}`]: { + color: 'red', + }, +- [`& .${selectClasses.iconOutlined}`]: { ++ [`& .${selectClasses.outlined} ~ .${selectClasses.icon}`]: { + color: 'red', + }, +- [`& .${selectClasses.iconStandard}`]: { ++ [`& .${selectClasses.standard} ~ .${selectClasses.icon}`]: { + color: 'red', + }, + }, + }, + }, +``` + +CSS transforms: + +```diff +- .MuiSelect-iconFilled ++ .MuiSelect-filled ~ .MuiSelect-icon +``` + +```diff +- .MuiSelect-iconOutlined ++ .MuiSelect-outlined ~ .MuiSelect-icon +``` + +```diff +- .MuiSelect-iconStandard ++ .MuiSelect-standard ~ .MuiSelect-icon +``` + +```bash +npx @mui/codemod@latest deprecations/select-classes +``` + #### `slider-props` ```diff diff --git a/packages/mui-codemod/src/deprecations/all/deprecations-all.js b/packages/mui-codemod/src/deprecations/all/deprecations-all.js index c6a16ef48d29ba..08306a961d8891 100644 --- a/packages/mui-codemod/src/deprecations/all/deprecations-all.js +++ b/packages/mui-codemod/src/deprecations/all/deprecations-all.js @@ -21,6 +21,7 @@ import transformOutlinedInputProps from '../outlined-input-props'; import transformPaginationItemClasses from '../pagination-item-classes'; import transformSpeedDialProps from '../speed-dial-props'; import transformTableSortLabelClasses from '../table-sort-label-classes'; +import transformSelectClasses from '../select-classes'; import transformStepConnectorClasses from '../step-connector-classes'; import transformStepContentProps from '../step-content-props'; import transformStepLabelProps from '../step-label-props'; @@ -62,6 +63,7 @@ export default function deprecationsAll(file, api, options) { file.source = transformStepLabelProps(file, api, options); file.source = transformTableSortLabelClasses(file, api, options); file.source = transformTextFieldProps(file, api, options); + file.source = transformSelectClasses(file, api, options); file.source = transformTabClasses(file, api, options); file.source = transformToggleButtonGroupClasses(file, api, options); file.source = transformTooltipProps(file, api, options); diff --git a/packages/mui-codemod/src/deprecations/all/postcss.config.js b/packages/mui-codemod/src/deprecations/all/postcss.config.js index a37e524e2072dc..948f51a315bae8 100644 --- a/packages/mui-codemod/src/deprecations/all/postcss.config.js +++ b/packages/mui-codemod/src/deprecations/all/postcss.config.js @@ -19,6 +19,7 @@ const { plugin: tabClassesPlugin } = require('../tab-classes/postcss-plugin'); const { plugin: tableSortLabelClassesPlugin, } = require('../table-sort-label-classes/postcss-plugin'); +const { plugin: selectClassesPlugin } = require('../select-classes/postcss-plugin'); module.exports = { plugins: [ @@ -33,5 +34,6 @@ module.exports = { toggleButtonGroupClassesPlugin, tabClassesPlugin, tableSortLabelClassesPlugin, + selectClassesPlugin, ], }; diff --git a/packages/mui-codemod/src/deprecations/select-classes/index.js b/packages/mui-codemod/src/deprecations/select-classes/index.js new file mode 100644 index 00000000000000..14de6453e01e88 --- /dev/null +++ b/packages/mui-codemod/src/deprecations/select-classes/index.js @@ -0,0 +1 @@ +export { default } from './select-classes'; diff --git a/packages/mui-codemod/src/deprecations/select-classes/postcss-plugin.js b/packages/mui-codemod/src/deprecations/select-classes/postcss-plugin.js new file mode 100644 index 00000000000000..be0deea8b61a12 --- /dev/null +++ b/packages/mui-codemod/src/deprecations/select-classes/postcss-plugin.js @@ -0,0 +1,37 @@ +const classes = [ + { + deprecatedClass: ' .MuiSelect-iconFilled', + replacementSelector: ' .MuiSelect-filled ~ .MuiSelect-icon', + }, + { + deprecatedClass: ' .MuiSelect-iconOutlined', + replacementSelector: ' .MuiSelect-outlined ~ .MuiSelect-icon', + }, + { + deprecatedClass: ' .MuiSelect-iconStandard', + replacementSelector: ' .MuiSelect-standard ~ .MuiSelect-icon', + }, +]; + +const plugin = () => { + return { + postcssPlugin: `Replace deprecated Select classes with new classes`, + Rule(rule) { + const { selector } = rule; + + classes.forEach(({ deprecatedClass, replacementSelector }) => { + const selectorRegex = new RegExp(`${deprecatedClass.trim()}$`); + + if (selector.match(selectorRegex)) { + rule.selector = selector.replace(selectorRegex, replacementSelector); + } + }); + }, + }; +}; +plugin.postcss = true; + +module.exports = { + plugin, + classes, +}; diff --git a/packages/mui-codemod/src/deprecations/select-classes/postcss.config.js b/packages/mui-codemod/src/deprecations/select-classes/postcss.config.js new file mode 100644 index 00000000000000..23bebc1125be6e --- /dev/null +++ b/packages/mui-codemod/src/deprecations/select-classes/postcss.config.js @@ -0,0 +1,5 @@ +const { plugin } = require('./postcss-plugin'); + +module.exports = { + plugins: [plugin], +}; diff --git a/packages/mui-codemod/src/deprecations/select-classes/select-classes.js b/packages/mui-codemod/src/deprecations/select-classes/select-classes.js new file mode 100644 index 00000000000000..ffd978bd092c0c --- /dev/null +++ b/packages/mui-codemod/src/deprecations/select-classes/select-classes.js @@ -0,0 +1,125 @@ +import { classes } from './postcss-plugin'; + +/** + * @param {import('jscodeshift').FileInfo} file + * @param {import('jscodeshift').API} api + */ +export default function transformer(file, api, options) { + const j = api.jscodeshift; + const root = j(file.source); + const printOptions = options.printOptions; + classes.forEach(({ deprecatedClass, replacementSelector }) => { + const replacementSelectorPrefix = '&'; + root + .find(j.ImportDeclaration) + .filter((path) => path.node.source.value.match(/^@mui\/material\/Select$/)) + .forEach((path) => { + path.node.specifiers.forEach((specifier) => { + if (specifier.type === 'ImportSpecifier' && specifier.imported.name === 'selectClasses') { + const deprecatedAtomicClass = deprecatedClass.replace( + `${deprecatedClass.split('-')[0]}-`, + '', + ); + root + .find(j.MemberExpression, { + object: { name: specifier.local.name }, + property: { name: deprecatedAtomicClass }, + }) + .forEach((memberExpression) => { + const parent = memberExpression.parentPath.parentPath.value; + if (parent.type === j.TemplateLiteral.name) { + const memberExpressionIndex = parent.expressions.findIndex( + (expression) => expression === memberExpression.value, + ); + const precedingTemplateElement = parent.quasis[memberExpressionIndex]; + const atomicClasses = replacementSelector + .replaceAll('MuiSelect-', '') + .replaceAll(replacementSelectorPrefix, '') + .replaceAll(' ~ ', '') + .split('.') + .map((className) => className.trim()) + .filter(Boolean); + + if ( + precedingTemplateElement.value.raw.endsWith( + deprecatedClass.startsWith(' ') + ? `${replacementSelectorPrefix} .` + : `${replacementSelectorPrefix}.`, + ) + ) { + const atomicClassesArgs = [ + memberExpressionIndex, + 1, + ...atomicClasses.map((atomicClass) => + j.memberExpression( + memberExpression.value.object, + j.identifier(atomicClass), + ), + ), + ]; + parent.expressions.splice(...atomicClassesArgs); + + if (replacementSelector.includes(' ~ ')) { + const quasisArgs = [ + memberExpressionIndex, + 1, + j.templateElement( + { + raw: precedingTemplateElement.value.raw, + cooked: precedingTemplateElement.value.cooked.replace(' ', ''), + }, + false, + ), + j.templateElement({ raw: ' ~ .', cooked: ' ~ .' }, false), + ]; + + if (atomicClasses.length === 3) { + quasisArgs.splice( + 3, + 0, + j.templateElement({ raw: '.', cooked: '.' }, false), + ); + } + + parent.quasis.splice(...quasisArgs); + } else { + parent.quasis.splice( + memberExpressionIndex, + 1, + j.templateElement( + { + raw: precedingTemplateElement.value.raw, + cooked: precedingTemplateElement.value.cooked, + }, + false, + ), + + j.templateElement({ raw: '.', cooked: '.' }, false), + ); + } + } + } + }); + } + }); + }); + + const selectorRegex = new RegExp(`${replacementSelectorPrefix}${deprecatedClass}$`); + root + .find( + j.Literal, + (literal) => typeof literal.value === 'string' && literal.value.match(selectorRegex), + ) + .forEach((path) => { + path.replace( + j.literal( + path.value.value.replace( + selectorRegex, + `${replacementSelectorPrefix}${replacementSelector}`, + ), + ), + ); + }); + }); + return root.toSource(printOptions); +} diff --git a/packages/mui-codemod/src/deprecations/select-classes/select-classes.test.js b/packages/mui-codemod/src/deprecations/select-classes/select-classes.test.js new file mode 100644 index 00000000000000..a7bc4264dff0aa --- /dev/null +++ b/packages/mui-codemod/src/deprecations/select-classes/select-classes.test.js @@ -0,0 +1,78 @@ +import path from 'path'; +import { expect } from 'chai'; +import postcss from 'postcss'; +import { jscodeshift } from '../../../testUtils'; +import jsTransform from './select-classes'; +import { plugin as postcssPlugin } from './postcss-plugin'; +import readFile from '../../util/readFile'; + +function read(fileName) { + return readFile(path.join(__dirname, fileName)); +} + +const postcssProcessor = postcss([postcssPlugin]); + +describe('@mui/codemod', () => { + describe('deprecations', () => { + describe('select-classes', () => { + describe('js-transform', () => { + it('transforms props as needed', () => { + const actual = jsTransform( + { source: read('./test-cases/actual.js') }, + { jscodeshift }, + { printOptions: { quote: 'double', trailingComma: true } }, + ); + + const expected = read('./test-cases/expected.js'); + expect(actual).to.equal(expected, 'The transformed version should be correct'); + }); + + it('should be idempotent', () => { + const actual = jsTransform( + { source: read('./test-cases/expected.js') }, + { jscodeshift }, + {}, + ); + + const expected = read('./test-cases/expected.js'); + expect(actual).to.equal(expected, 'The transformed version should be correct'); + }); + }); + + describe('css-transform', () => { + it('transforms classes as needed', async () => { + const actual = await postcssProcessor.process(read('./test-cases/actual.css'), { + from: undefined, + }); + + const expected = read('./test-cases/expected.css'); + expect(actual.css).to.equal(expected, 'The transformed version should be correct'); + }); + + it('should be idempotent', async () => { + const actual = await postcssProcessor.process(read('./test-cases/expected.css'), { + from: undefined, + }); + + const expected = read('./test-cases/expected.css'); + expect(actual.css).to.equal(expected, 'The transformed version should be correct'); + }); + }); + + describe('test-cases', () => { + it('should not be the same', () => { + const actualJS = read('./test-cases/actual.js'); + const expectedJS = read('./test-cases/expected.js'); + expect(actualJS).not.to.equal(expectedJS, 'The actual and expected should be different'); + + const actualCSS = read('./test-cases/actual.css'); + const expectedCSS = read('./test-cases/expected.css'); + expect(actualCSS).not.to.equal( + expectedCSS, + 'The actual and expected should be different', + ); + }); + }); + }); + }); +}); diff --git a/packages/mui-codemod/src/deprecations/select-classes/test-cases/actual.css b/packages/mui-codemod/src/deprecations/select-classes/test-cases/actual.css new file mode 100644 index 00000000000000..6f0ff7f77f9c88 --- /dev/null +++ b/packages/mui-codemod/src/deprecations/select-classes/test-cases/actual.css @@ -0,0 +1,11 @@ +.MuiSelect-iconFilled { + color: red; +} + +.MuiSelect-iconOutlined { + color: red; +} + +.MuiSelect-iconStandard { + color: red; +} diff --git a/packages/mui-codemod/src/deprecations/select-classes/test-cases/actual.js b/packages/mui-codemod/src/deprecations/select-classes/test-cases/actual.js new file mode 100644 index 00000000000000..17b450ff684cfb --- /dev/null +++ b/packages/mui-codemod/src/deprecations/select-classes/test-cases/actual.js @@ -0,0 +1,8 @@ +import { selectClasses } from '@mui/material/Select'; + +('& .MuiSelect-iconFilled'); +('& .MuiSelect-iconOutlined'); +('& .MuiSelect-iconStandard'); +`& .${selectClasses.iconFilled}`; +`& .${selectClasses.iconOutlined}`; +`& .${selectClasses.iconStandard}`; diff --git a/packages/mui-codemod/src/deprecations/select-classes/test-cases/expected.css b/packages/mui-codemod/src/deprecations/select-classes/test-cases/expected.css new file mode 100644 index 00000000000000..5a6773edc80c28 --- /dev/null +++ b/packages/mui-codemod/src/deprecations/select-classes/test-cases/expected.css @@ -0,0 +1,11 @@ + .MuiSelect-filled ~ .MuiSelect-icon { + color: red; +} + + .MuiSelect-outlined ~ .MuiSelect-icon { + color: red; +} + + .MuiSelect-standard ~ .MuiSelect-icon { + color: red; +} diff --git a/packages/mui-codemod/src/deprecations/select-classes/test-cases/expected.js b/packages/mui-codemod/src/deprecations/select-classes/test-cases/expected.js new file mode 100644 index 00000000000000..bc288e72b4cb36 --- /dev/null +++ b/packages/mui-codemod/src/deprecations/select-classes/test-cases/expected.js @@ -0,0 +1,8 @@ +import { selectClasses } from '@mui/material/Select'; + +("& .MuiSelect-filled ~ .MuiSelect-icon"); +("& .MuiSelect-outlined ~ .MuiSelect-icon"); +("& .MuiSelect-standard ~ .MuiSelect-icon"); +`& .${selectClasses.filled} ~ .${selectClasses.icon}`; +`& .${selectClasses.outlined} ~ .${selectClasses.icon}`; +`& .${selectClasses.standard} ~ .${selectClasses.icon}`; diff --git a/packages/mui-material/src/Select/Select.test.js b/packages/mui-material/src/Select/Select.test.js index a36065667dc939..50e2a07eab3667 100644 --- a/packages/mui-material/src/Select/Select.test.js +++ b/packages/mui-material/src/Select/Select.test.js @@ -1782,4 +1782,22 @@ describe('); + expect(container.querySelector('.MuiSelect-iconOutlined')).not.to.equal(null); + expect(container.querySelector('.MuiSelect-outlined ~ .MuiSelect-icon')).not.to.equal(null); + }); + + it('standard icon should be selected from below css selectors', () => { + const { container } = render(); + expect(container.querySelector('.MuiSelect-iconFilled')).not.to.equal(null); + expect(container.querySelector('.MuiSelect-filled ~ .MuiSelect-icon')).not.to.equal(null); + }); }); diff --git a/packages/mui-material/src/Select/selectClasses.ts b/packages/mui-material/src/Select/selectClasses.ts index a468b714f1f786..d1539fc694dae3 100644 --- a/packages/mui-material/src/Select/selectClasses.ts +++ b/packages/mui-material/src/Select/selectClasses.ts @@ -22,11 +22,17 @@ export interface SelectClasses { icon: string; /** Styles applied to the icon component if the popup is open. */ iconOpen: string; - /** Styles applied to the icon component if `variant="filled"`. */ + /** Styles applied to the icon component if `variant="filled"`. + * @deprecated Combine the [.MuiSelect-icon](/material-ui/api/select/#select-classes-icon) and [.MuiSelect-filled](/material-ui/api/select/#select-classes-filled) classes instead. See [Migrating from deprecated APIs](/material-ui/migration/migrating-from-deprecated-apis/) for more details. + */ iconFilled: string; - /** Styles applied to the icon component if `variant="outlined"`. */ + /** Styles applied to the icon component if `variant="outlined"`. + * @deprecated Combine the [.MuiSelect-icon](/material-ui/api/select/#select-classes-icon) and [.MuiSelect-outlined](/material-ui/api/select/#select-classes-outlined) classes instead. See [Migrating from deprecated APIs](/material-ui/migration/migrating-from-deprecated-apis/) for more details. + */ iconOutlined: string; - /** Styles applied to the icon component if `variant="standard"`. */ + /** Styles applied to the icon component if `variant="standard"`. + * @deprecated Combine the [.MuiSelect-icon](/material-ui/api/select/#select-classes-icon) and [.MuiSelect-standard](/material-ui/api/select/#select-classes-standard) classes instead. See [Migrating from deprecated APIs](/material-ui/migration/migrating-from-deprecated-apis/) for more details. + */ iconStandard: string; /** Styles applied to the underlying native input component. */ nativeInput: string;