Skip to content

Commit

Permalink
[material-ui][Select] Deprecate composed classes (#44925)
Browse files Browse the repository at this point in the history
  • Loading branch information
sai6855 authored Jan 3, 2025
1 parent 00c5b99 commit 18bbe40
Show file tree
Hide file tree
Showing 17 changed files with 421 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 <path>
```

### 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:
Expand Down
9 changes: 6 additions & 3 deletions docs/pages/material-ui/api/select.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
9 changes: 6 additions & 3 deletions docs/translations/api-docs/select/select.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@
"iconFilled": {
"description": "Styles applied to {{nodeName}} if {{conditions}}.",
"nodeName": "the icon component",
"conditions": "<code>variant=\"filled\"</code>"
"conditions": "<code>variant=\"filled\"</code>",
"deprecationInfo": "Combine the <a href=\"/material-ui/api/select/#select-classes-icon\">.MuiSelect-icon</a> and <a href=\"/material-ui/api/select/#select-classes-filled\">.MuiSelect-filled</a> classes instead. See <a href=\"/material-ui/migration/migrating-from-deprecated-apis/\">Migrating from deprecated APIs</a> for more details."
},
"iconOpen": {
"description": "Styles applied to {{nodeName}} if {{conditions}}.",
Expand All @@ -107,12 +108,14 @@
"iconOutlined": {
"description": "Styles applied to {{nodeName}} if {{conditions}}.",
"nodeName": "the icon component",
"conditions": "<code>variant=\"outlined\"</code>"
"conditions": "<code>variant=\"outlined\"</code>",
"deprecationInfo": "Combine the <a href=\"/material-ui/api/select/#select-classes-icon\">.MuiSelect-icon</a> and <a href=\"/material-ui/api/select/#select-classes-outlined\">.MuiSelect-outlined</a> classes instead. See <a href=\"/material-ui/migration/migrating-from-deprecated-apis/\">Migrating from deprecated APIs</a> for more details."
},
"iconStandard": {
"description": "Styles applied to {{nodeName}} if {{conditions}}.",
"nodeName": "the icon component",
"conditions": "<code>variant=\"standard\"</code>"
"conditions": "<code>variant=\"standard\"</code>",
"deprecationInfo": "Combine the <a href=\"/material-ui/api/select/#select-classes-icon\">.MuiSelect-icon</a> and <a href=\"/material-ui/api/select/#select-classes-standard\">.MuiSelect-standard</a> classes instead. See <a href=\"/material-ui/migration/migrating-from-deprecated-apis/\">Migrating from deprecated APIs</a> for more details."
},
"multiple": {
"description": "Styles applied to {{nodeName}} if {{conditions}}.",
Expand Down
48 changes: 48 additions & 0 deletions packages/mui-codemod/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1406,6 +1406,54 @@ npx @mui/codemod@latest deprecations/popper-props <path>
npx @mui/codemod@latest deprecations/outlined-input-props <path>
```

#### `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 <path>
```

#### `slider-props`

```diff
Expand Down
2 changes: 2 additions & 0 deletions packages/mui-codemod/src/deprecations/all/deprecations-all.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions packages/mui-codemod/src/deprecations/all/postcss.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand All @@ -33,5 +34,6 @@ module.exports = {
toggleButtonGroupClassesPlugin,
tabClassesPlugin,
tableSortLabelClassesPlugin,
selectClassesPlugin,
],
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './select-classes';
Original file line number Diff line number Diff line change
@@ -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,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const { plugin } = require('./postcss-plugin');

module.exports = {
plugins: [plugin],
};
125 changes: 125 additions & 0 deletions packages/mui-codemod/src/deprecations/select-classes/select-classes.js
Original file line number Diff line number Diff line change
@@ -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);
}
Loading

0 comments on commit 18bbe40

Please sign in to comment.