Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[material-ui][Select] Deprecate composed classes #42950

Closed
wants to merge 29 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -27,6 +27,7 @@ import transformStepLabelProps from '../step-label-props';
import transformTextFieldProps from '../text-field-props';
import transformTabClasses from '../tab-classes';
import transformToggleButtonGroupClasses from '../toggle-button-group-classes';
import transformSelectClasses from '../select-classes';
import transformTooltipProps from '../tooltip-props';
import transformTablePaginationProps from '../table-pagination-props';

Expand Down Expand Up @@ -64,6 +65,7 @@ export default function deprecationsAll(file, api, options) {
file.source = transformTextFieldProps(file, api, options);
file.source = transformTabClasses(file, api, options);
file.source = transformToggleButtonGroupClasses(file, api, options);
file.source = transformSelectClasses(file, api, options);
file.source = transformTooltipProps(file, api, options);
file.source = transformTablePaginationProps(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
Loading