Skip to content

Commit a8080de

Browse files
[material-ui][Dialog] Add codemod for deprecated props (@sai6855) (#46335)
Co-authored-by: sai chand <[email protected]> Co-authored-by: sai6855 <[email protected]>
1 parent b977f4f commit a8080de

File tree

10 files changed

+329
-1
lines changed

10 files changed

+329
-1
lines changed

docs/data/material/migration/migrating-from-deprecated-apis/migrating-from-deprecated-apis.md

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1136,10 +1136,11 @@ The Divider's `light` prop was deprecated, Use `sx={{ opacity : "0.6" }}` (or an
11361136

11371137
## Dialog
11381138

1139-
Use the [codemod](https://github.com/mui/material-ui/tree/HEAD/packages/mui-codemod#dialog-classes) below to migrate the code as described in the following sections:
1139+
Use the [dialog-classes-codemod](https://github.com/mui/material-ui/tree/HEAD/packages/mui-codemod#dialog-classes) and [dialog-props-codemod](https://github.com/mui/material-ui/tree/HEAD/packages/mui-codemod#dialog-props) below to migrate the code as described in the following sections:
11401140

11411141
```bash
11421142
npx @mui/codemod@latest deprecations/dialog-classes <path>
1143+
npx @mui/codemod@latest deprecations/dialog-props <path>
11431144
```
11441145

11451146
### Composed CSS classes
@@ -1172,6 +1173,39 @@ Here's how to migrate:
11721173
},
11731174
},
11741175
},
1176+
1177+
```
1178+
1179+
### TransitionComponent
1180+
1181+
The Dialog's `TransitionComponent` prop was deprecated in favor of `slots.transition`:
1182+
1183+
```diff
1184+
<Dialog
1185+
- TransitionComponent={CustomTransition}
1186+
+ slots={{ transition: CustomTransition }}
1187+
```
1188+
1189+
### TransitionProps
1190+
1191+
The Dialog's `TransitionProps` prop was deprecated in favor of `slotProps.transition`:
1192+
1193+
```diff
1194+
<Dialog
1195+
- TransitionProps={{ unmountOnExit: true }}
1196+
+ slotProps={{ transition: { unmountOnExit: true } }}
1197+
/>
1198+
```
1199+
1200+
### PaperProps
1201+
1202+
The Dialog's `PaperProps` prop was deprecated in favor of `slotProps.paper`:
1203+
1204+
```diff
1205+
<Dialog
1206+
- PaperProps={paperProps}
1207+
+ slotProps={{ paper: paperProps }}
1208+
/>
11751209
```
11761210

11771211
## Drawer

packages/mui-codemod/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,6 +1066,28 @@ CSS transforms:
10661066
npx @mui/codemod@latest deprecations/dialog-classes <path>
10671067
```
10681068

1069+
#### `dialog-props`
1070+
1071+
JS transforms:
1072+
1073+
```diff
1074+
<Dialog
1075+
- PaperProps={paperProps}
1076+
+ slotProps={{ paper: paperProps }}
1077+
- TransitionComponent={CustomTransition}
1078+
+ slots={{ transition: CustomTransition }}
1079+
- TransitionProps={CustomTransitionProps}
1080+
+ slotProps={{ transition: CustomTransitionProps }}
1081+
/>
1082+
},
1083+
},
1084+
},
1085+
```
1086+
1087+
```bash
1088+
npx @mui/codemod@latest deprecations/dialog-props <path>
1089+
```
1090+
10691091
#### `drawer-classes`
10701092

10711093
JS transforms:

packages/mui-codemod/src/deprecations/all/deprecations-all.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import transformCircularProgressClasses from '../circular-progress-classes';
1313
import transformDividerProps from '../divider-props';
1414
import transformDrawerClasses from '../drawer-classes';
1515
import transformDialogClasses from '../dialog-classes';
16+
import transformDialogProps from '../dialog-props';
1617
import transformFilledInputProps from '../filled-input-props';
1718
import transformFormControlLabelProps from '../form-control-label-props';
1819
import transformImageListItemBarClasses from '../image-list-item-bar-classes';
@@ -70,6 +71,7 @@ export default function deprecationsAll(file, api, options) {
7071
file.source = transformDividerProps(file, api, options);
7172
file.source = transformDrawerClasses(file, api, options);
7273
file.source = transformDialogClasses(file, api, options);
74+
file.source = transformDialogProps(file, api, options);
7375
file.source = transformFilledInputProps(file, api, options);
7476
file.source = transformFormControlLabelProps(file, api, options);
7577
file.source = transformImageListItemBarClasses(file, api, options);
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import movePropIntoSlots from '../utils/movePropIntoSlots';
2+
import movePropIntoSlotProps from '../utils/movePropIntoSlotProps';
3+
4+
/**
5+
* @param {import('jscodeshift').FileInfo} file
6+
* @param {import('jscodeshift').API} api
7+
*/
8+
export default function transformer(file, api, options) {
9+
const j = api.jscodeshift;
10+
const root = j(file.source);
11+
const printOptions = options.printOptions;
12+
13+
movePropIntoSlots(j, {
14+
root,
15+
packageName: options.packageName,
16+
componentName: 'Dialog',
17+
propName: 'TransitionComponent',
18+
slotName: 'transition',
19+
});
20+
21+
movePropIntoSlotProps(j, {
22+
root,
23+
packageName: options.packageName,
24+
componentName: 'Dialog',
25+
propName: 'TransitionProps',
26+
slotName: 'transition',
27+
});
28+
29+
movePropIntoSlotProps(j, {
30+
root,
31+
packageName: options.packageName,
32+
componentName: 'Dialog',
33+
propName: 'PaperProps',
34+
slotName: 'paper',
35+
});
36+
37+
return root.toSource(printOptions);
38+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import path from 'path';
2+
import { expect } from 'chai';
3+
import { jscodeshift } from '../../../testUtils';
4+
import transform from './dialog-props';
5+
import readFile from '../../util/readFile';
6+
7+
function read(fileName) {
8+
return readFile(path.join(__dirname, fileName));
9+
}
10+
11+
describe('@mui/codemod', () => {
12+
describe('deprecations', () => {
13+
describe('dialog-props', () => {
14+
it('transforms props as needed', () => {
15+
const actual = transform({ source: read('./test-cases/actual.js') }, { jscodeshift }, {});
16+
17+
const expected = read('./test-cases/expected.js');
18+
expect(actual).to.equal(expected, 'The transformed version should be correct');
19+
});
20+
21+
it('should be idempotent', () => {
22+
const actual = transform({ source: read('./test-cases/expected.js') }, { jscodeshift }, {});
23+
24+
const expected = read('./test-cases/expected.js');
25+
expect(actual).to.equal(expected, 'The transformed version should be correct');
26+
});
27+
});
28+
29+
describe('[theme] dialog-props', () => {
30+
it('transforms props as needed', () => {
31+
const actual = transform(
32+
{ source: read('./test-cases/theme.actual.js') },
33+
{ jscodeshift },
34+
{},
35+
);
36+
37+
const expected = read('./test-cases/theme.expected.js');
38+
expect(actual).to.equal(expected, 'The transformed version should be correct');
39+
});
40+
41+
it('should be idempotent', () => {
42+
const actual = transform(
43+
{ source: read('./test-cases/theme.expected.js') },
44+
{ jscodeshift },
45+
{},
46+
);
47+
48+
const expected = read('./test-cases/theme.expected.js');
49+
expect(actual).to.equal(expected, 'The transformed version should be correct');
50+
});
51+
});
52+
});
53+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default } from './dialog-props';
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import Dialog from '@mui/material/Dialog';
2+
import { Dialog as MyDialog } from '@mui/material';
3+
4+
<Dialog
5+
TransitionComponent={CustomTransition}
6+
TransitionProps={CustomTransitionProps}
7+
PaperProps={PaperProps}
8+
/>;
9+
<MyDialog
10+
TransitionComponent={CustomTransition}
11+
TransitionProps={CustomTransitionProps}
12+
PaperProps={PaperProps}
13+
/>;
14+
<Dialog
15+
TransitionComponent={CustomTransition}
16+
TransitionProps={CustomTransitionProps}
17+
slots={{
18+
root: 'div',
19+
}}
20+
PaperProps={PaperProps}
21+
/>;
22+
<MyDialog
23+
TransitionComponent={CustomTransition}
24+
TransitionProps={CustomTransitionProps}
25+
slots={{
26+
...outerSlots,
27+
}}
28+
PaperProps={PaperProps}
29+
/>;
30+
<Dialog
31+
TransitionComponent={ComponentTransition}
32+
TransitionProps={CustomTransitionProps}
33+
slots={{
34+
root: 'div',
35+
transition: SlotTransition,
36+
}}
37+
PaperProps={PaperProps}
38+
/>;
39+
// should skip non MUI components
40+
<NonMuiDialog
41+
TransitionComponent={CustomTransition}
42+
TransitionProps={CustomTransitionProps}
43+
PaperProps={PaperProps}
44+
/>;
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import Dialog from '@mui/material/Dialog';
2+
import { Dialog as MyDialog } from '@mui/material';
3+
4+
<Dialog
5+
slots={{
6+
transition: CustomTransition
7+
}}
8+
slotProps={{
9+
transition: CustomTransitionProps,
10+
paper: PaperProps
11+
}} />;
12+
<MyDialog
13+
slots={{
14+
transition: CustomTransition
15+
}}
16+
slotProps={{
17+
transition: CustomTransitionProps,
18+
paper: PaperProps
19+
}} />;
20+
<Dialog
21+
slots={{
22+
root: 'div',
23+
transition: CustomTransition
24+
}}
25+
slotProps={{
26+
transition: CustomTransitionProps,
27+
paper: PaperProps
28+
}} />;
29+
<MyDialog
30+
slots={{
31+
...outerSlots,
32+
transition: CustomTransition
33+
}}
34+
slotProps={{
35+
transition: CustomTransitionProps,
36+
paper: PaperProps
37+
}} />;
38+
<Dialog
39+
slots={{
40+
root: 'div',
41+
transition: SlotTransition,
42+
}}
43+
slotProps={{
44+
transition: CustomTransitionProps,
45+
paper: PaperProps
46+
}} />;
47+
// should skip non MUI components
48+
<NonMuiDialog
49+
TransitionComponent={CustomTransition}
50+
TransitionProps={CustomTransitionProps}
51+
PaperProps={PaperProps}
52+
/>;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
fn({
2+
MuiDialog: {
3+
defaultProps: {
4+
TransitionComponent: CustomTransition,
5+
TransitionProps: CustomTransitionProps,
6+
PaperProps: PaperProps,
7+
},
8+
},
9+
});
10+
11+
fn({
12+
MuiDialog: {
13+
defaultProps: {
14+
TransitionComponent: CustomTransition,
15+
TransitionProps: CustomTransitionProps,
16+
slots: {
17+
root: 'div',
18+
},
19+
PaperProps: PaperProps,
20+
},
21+
},
22+
});
23+
24+
fn({
25+
MuiDialog: {
26+
defaultProps: {
27+
TransitionComponent: ComponentTransition,
28+
TransitionProps: CustomTransitionProps,
29+
slots: {
30+
root: 'div',
31+
transition: SlotTransition,
32+
},
33+
PaperProps: PaperProps,
34+
},
35+
},
36+
});
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
fn({
2+
MuiDialog: {
3+
defaultProps: {
4+
slots: {
5+
transition: CustomTransition
6+
},
7+
8+
slotProps: {
9+
transition: CustomTransitionProps,
10+
paper: PaperProps
11+
}
12+
},
13+
},
14+
});
15+
16+
fn({
17+
MuiDialog: {
18+
defaultProps: {
19+
slots: {
20+
root: 'div',
21+
transition: CustomTransition
22+
},
23+
24+
slotProps: {
25+
transition: CustomTransitionProps,
26+
paper: PaperProps
27+
}
28+
},
29+
},
30+
});
31+
32+
fn({
33+
MuiDialog: {
34+
defaultProps: {
35+
slots: {
36+
root: 'div',
37+
transition: SlotTransition,
38+
},
39+
40+
slotProps: {
41+
transition: CustomTransitionProps,
42+
paper: PaperProps
43+
}
44+
},
45+
},
46+
});

0 commit comments

Comments
 (0)