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][ListItemText] Add slots and slotProps #44571

Merged
merged 17 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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 @@ -1275,6 +1275,36 @@ The ListItemSecondaryAction component was deprecated in favor of the `secondaryA
</ListItem>
```

## ListItemText

Use the [codemod](https://github.com/mui/material-ui/tree/HEAD/packages/mui-codemod#list-item-text-props) below to migrate the code as described in the following sections:

```bash
npx @mui/codemod@latest deprecations/list-item-text-props <path>
```

### primaryTypographyProps

The ListItemText's `primaryTypographyProps` props were deprecated in favor of `slotProps.primary`:

```diff
<ListItemText
- primaryTypographyProps={primaryTypographyProps}
+ slotProps={{ primary: primaryTypographyProps }}
/>
```

### secondaryTypographyProps

The ListItemText's `secondaryTypographyProps` props were deprecated in favor of `slotProps.secondary`:

```diff
<ListItemText
- secondaryTypographyProps={secondaryTypographyProps}
+ slotProps={{ secondary: secondaryTypographyProps }}
/>
```

## ImageListItemBar

Use the [codemod](https://github.com/mui/material-ui/tree/HEAD/packages/mui-codemod#image-list-item-bar-classes) below to migrate the code as described in the following sections:
Expand Down
52 changes: 38 additions & 14 deletions docs/pages/material-ui/api/list-item-text.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,31 @@
"disableTypography": { "type": { "name": "bool" }, "default": "false" },
"inset": { "type": { "name": "bool" }, "default": "false" },
"primary": { "type": { "name": "node" } },
"primaryTypographyProps": { "type": { "name": "object" } },
"primaryTypographyProps": {
"type": { "name": "object" },
"deprecated": true,
"deprecationInfo": "Use <code>slotProps.primary</code> instead. This prop will be removed in v7. See <a href=\"/material-ui/migration/migrating-from-deprecated-apis/\">Migrating from deprecated APIs</a> for more details."
},
"secondary": { "type": { "name": "node" } },
"secondaryTypographyProps": { "type": { "name": "object" } },
"secondaryTypographyProps": {
"type": { "name": "object" },
"deprecated": true,
"deprecationInfo": "Use <code>slotProps.secondary</code> instead. This prop will be removed in v7. See <a href=\"/material-ui/migration/migrating-from-deprecated-apis/\">Migrating from deprecated APIs</a> for more details."
},
"slotProps": {
"type": {
"name": "shape",
"description": "{ primary?: func<br>&#124;&nbsp;object, secondary?: func<br>&#124;&nbsp;object }"
},
"default": "{}"
},
"slots": {
"type": {
"name": "shape",
"description": "{ primary?: elementType, secondary?: elementType }"
},
"default": "{}"
},
"sx": {
"type": {
"name": "union",
Expand All @@ -21,6 +43,20 @@
"import ListItemText from '@mui/material/ListItemText';",
"import { ListItemText } from '@mui/material';"
],
"slots": [
{
"name": "primary",
"description": "The component that renders the main content.",
"default": "span",
"class": "MuiListItemText-primary"
},
{
"name": "secondary",
"description": "The component that renders the main content.",
"default": "p",
"class": "MuiListItemText-secondary"
}
],
"classes": [
{
"key": "dense",
Expand All @@ -40,23 +76,11 @@
"description": "Styles applied to the Typography component if primary and secondary are set.",
"isGlobal": false
},
{
"key": "primary",
"className": "MuiListItemText-primary",
"description": "Styles applied to the primary `Typography` component.",
"isGlobal": false
},
{
"key": "root",
"className": "MuiListItemText-root",
"description": "Styles applied to the root element.",
"isGlobal": false
},
{
"key": "secondary",
"className": "MuiListItemText-secondary",
"description": "Styles applied to the secondary `Typography` component.",
"isGlobal": false
}
],
"spread": true,
Expand Down
16 changes: 7 additions & 9 deletions docs/translations/api-docs/list-item-text/list-item-text.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
"secondaryTypographyProps": {
"description": "These props will be forwarded to the secondary typography component (as long as disableTypography is not <code>true</code>)."
},
"slotProps": { "description": "The props used for each slot inside." },
"slots": { "description": "The components used for each slot inside." },
"sx": {
"description": "The system prop that allows defining system overrides as well as additional CSS styles."
}
Expand All @@ -37,14 +39,10 @@
"nodeName": "the Typography component",
"conditions": "primary and secondary are set"
},
"primary": {
"description": "Styles applied to {{nodeName}}.",
"nodeName": "the primary <code>Typography</code> component"
},
"root": { "description": "Styles applied to the root element." },
"secondary": {
"description": "Styles applied to {{nodeName}}.",
"nodeName": "the secondary <code>Typography</code> component"
}
"root": { "description": "Styles applied to the root element." }
},
"slotDescriptions": {
"primary": "The component that renders the main content.",
"secondary": "The component that renders the main content."
}
}
26 changes: 26 additions & 0 deletions packages/mui-codemod/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1107,6 +1107,32 @@ npx @mui/codemod@latest deprecations/form-control-label-props <path>
npx @mui/codemod@latest deprecations/list-item-props <path>
```

#### `list-item-text-props`

```diff
<ListItemText
- primaryTypographyProps={primaryTypographyProps}
+ slotProps={{ primary: primaryTypographyProps }}
- secondaryTypographyProps={secondaryTypographyProps}
+ slotProps={{ secondary: secondaryTypographyProps }}
/>
```

```diff
MuiListItemText: {
defaultProps: {
- primaryTypographyProps:primaryTypographyProps
+ slotProps:{ primary: primaryTypographyProps }
- secondaryTypographyProps:secondaryTypographyProps
+ slotProps:{ secondary: secondaryTypographyProps }
},
},
```

```bash
npx @mui/codemod@latest deprecations/list-item-text-props <path>
```

#### `image-list-item-bar-classes`

JS transforms:
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 @@ -15,6 +15,7 @@ import transformFormControlLabelProps from '../form-control-label-props';
import transformImageListItemBarClasses from '../image-list-item-bar-classes';
import transformInputBaseProps from '../input-base-props';
import transformInputProps from '../input-props';
import transformListItemTextProps from '../list-item-text-props';
import transformModalProps from '../modal-props';
import transformOutlinedInputProps from '../outlined-input-props';
import transformPaginationItemClasses from '../pagination-item-classes';
Expand Down Expand Up @@ -49,6 +50,7 @@ export default function deprecationsAll(file, api, options) {
file.source = transformImageListItemBarClasses(file, api, options);
file.source = transformInputBaseProps(file, api, options);
file.source = transformInputProps(file, api, options);
file.source = transformListItemTextProps(file, api, options);
file.source = transformModalProps(file, api, options);
file.source = transformOutlinedInputProps(file, api, options);
file.source = transformPaginationItemClasses(file, api, options);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './list-item-text-props';
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import replaceComponentsWithSlots from '../utils/replaceComponentsWithSlots';
import movePropIntoSlotProps from '../utils/movePropIntoSlotProps';

/**
* @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;

replaceComponentsWithSlots(j, { root, componentName: 'ListItemText' });

movePropIntoSlotProps(j, {
root,
componentName: 'ListItemText',
propName: 'primaryTypographyProps',
slotName: 'primary',
});

movePropIntoSlotProps(j, {
root,
componentName: 'ListItemText',
propName: 'secondaryTypographyProps',
slotName: 'secondary',
});

return root.toSource(printOptions);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import path from 'path';
import { expect } from 'chai';
import { jscodeshift } from '../../../testUtils';
import transform from './list-item-text-props';
import readFile from '../../util/readFile';

function read(fileName) {
return readFile(path.join(__dirname, fileName));
}

describe('@mui/codemod', () => {
describe('deprecations', () => {
describe('list-item-text-props', () => {
it('transforms props as needed', () => {
const actual = transform({ source: read('./test-cases/actual.js') }, { jscodeshift }, {});

const expected = read('./test-cases/expected.js');
expect(actual).to.equal(expected, 'The transformed version should be correct');
});

it('should be idempotent', () => {
const actual = transform({ 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('[theme] list-item-text-props', () => {
it('transforms props as needed', () => {
const actual = transform(
{ source: read('./test-cases/theme.actual.js') },
{ jscodeshift },
{ printOptions: { trailingComma: false } },
);

const expected = read('./test-cases/theme.expected.js');
expect(actual).to.equal(expected, 'The transformed version should be correct');
});

it('should be idempotent', () => {
const actual = transform(
{ source: read('./test-cases/theme.expected.js') },
{ jscodeshift },
{},
);

const expected = read('./test-cases/theme.expected.js');
expect(actual).to.equal(expected, 'The transformed version should be correct');
});
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import ListItemText from '@mui/material/ListItemText';

<ListItemText secondaryTypographyProps={secondaryTypographyProps} />;
<ListItemText primaryTypographyProps={primaryTypographyProps} />;
<ListItemText
primaryTypographyProps={primaryTypographyProps}
secondaryTypographyProps={secondaryTypographyProps}
/>;
<ListItemText
slotProps={{ primary: primarySlotProps }}
secondaryTypographyProps={secondaryTypographyProps}
/>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import ListItemText from '@mui/material/ListItemText';

<ListItemText slotProps={{
secondary: secondaryTypographyProps
}} />;
<ListItemText slotProps={{
primary: primaryTypographyProps
}} />;
<ListItemText
slotProps={{
primary: primaryTypographyProps,
secondary: secondaryTypographyProps
}} />;
<ListItemText
slotProps={{
primary: primarySlotProps,
secondary: secondaryTypographyProps
}} />;
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
fn({
MuiListItemText: {
defaultProps: {
primaryTypographyProps: primaryTypographyProps,
},
},
});

fn({
MuiListItemText: {
defaultProps: {
secondaryTypographyProps: secondaryTypographyProps,
},
},
});
fn({
MuiListItemText: {
defaultProps: {
primaryTypographyProps: primaryTypographyProps,
secondaryTypographyProps: secondaryTypographyProps,
},
},
});

fn({
MuiListItemText: {
defaultProps: {
slotProps: { primary: primarySlotProps },
secondaryTypographyProps: secondaryTypographyProps,
},
},
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
fn({
MuiListItemText: {
defaultProps: {
slotProps: {
primary: primaryTypographyProps
},
},
},
});

fn({
MuiListItemText: {
defaultProps: {
slotProps: {
secondary: secondaryTypographyProps
},
},
},
});
fn({
MuiListItemText: {
defaultProps: {
slotProps: {
primary: primaryTypographyProps,
secondary: secondaryTypographyProps
}
},
},
});

fn({
MuiListItemText: {
defaultProps: {
slotProps: {
primary: primarySlotProps,
secondary: secondaryTypographyProps
}
},
},
});
Loading