Skip to content

Commit

Permalink
[material-ui][TableSortLabel] Deprecate composed classes (#42281)
Browse files Browse the repository at this point in the history
  • Loading branch information
sai6855 authored Jun 14, 2024
1 parent c1b78be commit d32a8ce
Show file tree
Hide file tree
Showing 18 changed files with 403 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1465,6 +1465,46 @@ Here's how to migrate:
},
```

## TableSortLabel

Use the [codemod](https://github.com/mui/material-ui/tree/HEAD/packages/mui-codemod#table-sort-label-classes) below to migrate the code as described in the following sections:

```bash
npx @mui/codemod@next deprecations/table-sort-label-classes <path>
```

### Composed CSS classes

The CSS classes that composed the `direction` prop and `icon` prop were removed.

Here's how to migrate:

```diff
- .MuiTableSortLabel-iconDirectionDesc
+.MuiTableSortLabel-directionDesc > .MuiTableSortLabel-icon
- .MuiTableSortLabel-iconDirectionAsc
+.MuiTableSortLabel-directionAsc > .MuiTableSortLabel-icon
```

```diff
import { tableSortLabelClasses } from '@mui/material/TableSortLabel';

MuiTableSortLabel: {
styleOverrides: {
root: {
- [`& .${tableSortLabelClasses.iconDirectionDesc}`]: {
+ [`&.${tableSortLabelClasses.directionDesc} > .${tableSortLabelClasses.icon}`]: {
color: 'red',
},
- [`& .${tableSortLabelClasses.iconDirectionAsc}`]: {
+ [`&.${tableSortLabelClasses.directionAsc} > .${tableSortLabelClasses.icon}`]: {
color: 'red',
},
},
},
},
```

## TextField

Use the [codemod](https://github.com/mui/material-ui/tree/HEAD/packages/mui-codemod#text-field-props) below to migrate the code as described in the following sections:
Expand Down
18 changes: 16 additions & 2 deletions docs/pages/material-ui/api/table-sort-label.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@
"description": "State class applied to the root element if `active={true}`.",
"isGlobal": true
},
{
"key": "directionAsc",
"className": "MuiTableSortLabel-directionAsc",
"description": "Styles applied to the root element if `direction=\"asc\"`.",
"isGlobal": false
},
{
"key": "directionDesc",
"className": "MuiTableSortLabel-directionDesc",
"description": "Styles applied to the root element if `direction=\"desc\"`.",
"isGlobal": false
},
{
"key": "icon",
"className": "MuiTableSortLabel-icon",
Expand All @@ -39,13 +51,15 @@
"key": "iconDirectionAsc",
"className": "MuiTableSortLabel-iconDirectionAsc",
"description": "Styles applied to the icon component if `direction=\"asc\"`.",
"isGlobal": false
"isGlobal": false,
"isDeprecated": true
},
{
"key": "iconDirectionDesc",
"className": "MuiTableSortLabel-iconDirectionDesc",
"description": "Styles applied to the icon component if `direction=\"desc\"`.",
"isGlobal": false
"isGlobal": false,
"isDeprecated": true
},
{
"key": "root",
Expand Down
16 changes: 14 additions & 2 deletions docs/translations/api-docs/table-sort-label/table-sort-label.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,28 @@
"nodeName": "the root element",
"conditions": "<code>active={true}</code>"
},
"directionAsc": {
"description": "Styles applied to {{nodeName}} if {{conditions}}.",
"nodeName": "the root element",
"conditions": "<code>direction=\"asc\"</code>"
},
"directionDesc": {
"description": "Styles applied to {{nodeName}} if {{conditions}}.",
"nodeName": "the root element",
"conditions": "<code>direction=\"desc\"</code>"
},
"icon": { "description": "Styles applied to {{nodeName}}.", "nodeName": "the icon component" },
"iconDirectionAsc": {
"description": "Styles applied to {{nodeName}} if {{conditions}}.",
"nodeName": "the icon component",
"conditions": "<code>direction=\"asc\"</code>"
"conditions": "<code>direction=\"asc\"</code>",
"deprecationInfo": "Combine the <a href=\"/material-ui/api/table-sort-label/#table-sort-label-classes-icon\">.MuiTableSortLabel-icon</a> and <a href=\"/material-ui/api/table-sort-label/#table-sort-label-classes-direction-asc\">.MuiTableSortLabel-directionAsc</a> classes instead. See <a href=\"/material-ui/migration/migrating-from-deprecated-apis/\">Migrating from deprecated APIs</a> for more details."
},
"iconDirectionDesc": {
"description": "Styles applied to {{nodeName}} if {{conditions}}.",
"nodeName": "the icon component",
"conditions": "<code>direction=\"desc\"</code>"
"conditions": "<code>direction=\"desc\"</code>",
"deprecationInfo": "Combine the <a href=\"/material-ui/api/table-sort-label/#table-sort-label-classes-icon\">.MuiTableSortLabel-icon</a> and <a href=\"/material-ui/api/table-sort-label/#table-sort-label-classes-direction-desc\">.MuiTableSortLabel-directionDesc</a> classes instead. See <a href=\"/material-ui/migration/migrating-from-deprecated-apis/\">Migrating from deprecated APIs</a> for more details."
},
"root": { "description": "Styles applied to the root element." }
}
Expand Down
36 changes: 36 additions & 0 deletions packages/mui-codemod/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1510,6 +1510,42 @@ CSS transforms:
npx @mui/codemod@next deprecations/step-connector-classes <path>
```

#### `table-sort-label-classes`

JS transforms:

```diff
import { tableSortLabelClasses } from '@mui/material/TableSortLabel';

MuiTableSortLabel: {
styleOverrides: {
root: {
- [`& .${tableSortLabelClasses.iconDirectionDesc}`]: {
+ [`&.${tableSortLabelClasses.directionDesc} > .${tableSortLabelClasses.icon}`]: {
color: 'red',
},
- [`& .${tableSortLabelClasses.iconDirectionAsc}`]: {
+ [`&.${tableSortLabelClasses.directionAsc} > .${tableSortLabelClasses.icon}`]: {
color: 'red',
},
},
},
},
```

CSS transforms:

```diff
- .MuiTableSortLabel-iconDirectionDesc
+.MuiTableSortLabel-directionDesc > .MuiTableSortLabel-icon
- .MuiTableSortLabel-iconDirectionAsc
+.MuiTableSortLabel-directionAsc > .MuiTableSortLabel-icon
```

```bash
npx @mui/codemod@next deprecations/table-sort-label-classes <path>
```

### v6.0.0

#### `sx-prop`
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 @@ -19,6 +19,7 @@ import transformModalProps from '../modal-props';
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 transformStepConnectorClasses from '../step-connector-classes';
import transformStepLabelProps from '../step-label-props';
import transformTextFieldProps from '../text-field-props';
Expand Down Expand Up @@ -52,6 +53,7 @@ export default function deprecationsAll(file, api, options) {
file.source = transformSpeedDialProps(file, api, options);
file.source = transformStepConnectorClasses(file, api, options);
file.source = transformStepLabelProps(file, api, options);
file.source = transformTableSortLabelClasses(file, api, options);
file.source = transformTextFieldProps(file, api, options);
file.source = transformToggleButtonGroupClasses(file, api, options);

Expand Down
4 changes: 4 additions & 0 deletions packages/mui-codemod/src/deprecations/all/postcss.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ const {
const {
plugin: circularProgressClassesPlugin,
} = require('../circular-progress-classes/postcss-plugin');
const {
plugin: tableSortLabelClassesPlugin,
} = require('../table-sort-label-classes/postcss-plugin');

module.exports = {
plugins: [
Expand All @@ -27,5 +30,6 @@ module.exports = {
paginationItemClassesPlugin,
stepConnectorClassesPlugin,
toggleButtonGroupClassesPlugin,
tableSortLabelClassesPlugin,
],
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './table-sort-label-classes';
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const classes = [
{
deprecatedClass: ' .MuiTableSortLabel-iconDirectionDesc',
replacementSelector: '.MuiTableSortLabel-directionDesc > .MuiTableSortLabel-icon',
},
{
deprecatedClass: ' .MuiTableSortLabel-iconDirectionAsc',
replacementSelector: '.MuiTableSortLabel-directionAsc > .MuiTableSortLabel-icon',
},
];

const plugin = () => {
return {
postcssPlugin: `Replace deprecated TableSortLabel classes with new classes`,
Rule(rule) {
const { selector } = rule;

classes.forEach(({ deprecatedClass, replacementSelector }) => {
const selectorRegex = new RegExp(`${deprecatedClass}$`);

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],
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
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\/TableSortLabel$/))
.forEach((path) => {
path.node.specifiers.forEach((specifier) => {
if (
specifier.type === 'ImportSpecifier' &&
specifier.imported.name === 'tableSortLabelClasses'
) {
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('MuiTableSortLabel-', '')
.replaceAll(replacementSelectorPrefix, '')
.replaceAll(' > ', '')
.split('.')
.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.replace(' ', ''),
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 d32a8ce

Please sign in to comment.