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

[List] Add support for list item container style overrides #25777

Merged
merged 13 commits into from
Apr 16, 2021
Merged
19 changes: 19 additions & 0 deletions docs/src/pages/guides/migration-v4/migration-v4.md
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,25 @@ As the core components use emotion as a styled engine, the props used by emotion
+<Icon>icon-name</Icon>
```

### ListItem

- The overrides for the container slot should be defined under new section in the theme's `components`.

```diff
const theme = createMuiTheme({
components: {
- MuiListItem: {
- styleOverrides: {
- container: { marginTop: '10px' },
+ MuiListItemContainer: {
+ styleOverrides: {
+ root: { marginTop: '10px' },
},
},
},
});
```

### Menu

- The onE\* transition props were removed. Use TransitionProps instead.
Expand Down
1 change: 1 addition & 0 deletions packages/material-ui/src/ListItem/ListItem.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ declare const ListItem: OverridableComponent<
>;

export type ListItemClassKey = keyof NonNullable<ListItemProps['classes']>;
export type ListItemContainerClassKey = 'root';

export type ListItemProps<D extends React.ElementType = 'li', P = {}> = OverrideProps<
ListItemTypeMap<P, D>,
Expand Down
9 changes: 6 additions & 3 deletions packages/material-ui/src/ListItem/ListItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,15 @@ export const ListItemRoot = experimentalStyled(
}),
}));

const containerOverridesResolver = (props, styles) => styles.root;

const ListItemContainer = experimentalStyled(
'li',
{},
{
name: 'MuiListItem',
slot: 'Container',
overridesResolver,
name: 'MuiListItemContainer',
slot: 'Root',
overridesResolver: containerOverridesResolver,
},
)({
position: 'relative',
Expand Down Expand Up @@ -263,6 +265,7 @@ const ListItem = React.forwardRef(function ListItem(inProps, ref) {
as={ContainerComponent}
className={clsx(classes.container, ContainerClassName)}
ref={handleRef}
styleProps={styleProps}
{...ContainerProps}
>
<Root
Expand Down
32 changes: 32 additions & 0 deletions packages/material-ui/src/ListItem/ListItem.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
fireEvent,
queries,
} from 'test/utils';
import { ThemeProvider, createMuiTheme } from '@material-ui/core/styles';
import ListItemText from '@material-ui/core/ListItemText';
import ListItemSecondaryAction from '@material-ui/core/ListItemSecondaryAction';
import ListItem, { listItemClasses as classes } from '@material-ui/core/ListItem';
Expand Down Expand Up @@ -203,4 +204,35 @@ describe('<ListItem />', () => {
expect(button).to.have.class(classes.focusVisible);
});
});

it('container overrides should work', function test() {
if (/jsdom/.test(window.navigator.userAgent)) {
this.skip();
}

const testStyle = {
marginTop: '13px',
};

const theme = createMuiTheme({
components: {
MuiListItemContainer: {
styleOverrides: {
root: testStyle,
},
},
},
});

const { container } = render(
<ThemeProvider theme={theme}>
<ListItem>
Test<ListItemSecondaryAction>SecondaryAction</ListItemSecondaryAction>
</ListItem>
</ThemeProvider>,
);

const listItemContainer = container.getElementsByClassName(classes.container)[0];
expect(listItemContainer).to.toHaveComputedStyle(testStyle);
});
});
3 changes: 3 additions & 0 deletions packages/material-ui/src/styles/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,9 @@ export interface Components {
defaultProps?: ComponentsProps['MuiListItem'];
styleOverrides?: ComponentsOverrides['MuiListItem'];
};
MuiListItemContainer?: {
styleOverrides?: ComponentsOverrides['MuiListItem'];
};
MuiListItemAvatar?: {
defaultProps?: ComponentsProps['MuiListItemAvatar'];
styleOverrides?: ComponentsOverrides['MuiListItemAvatar'];
Expand Down
3 changes: 2 additions & 1 deletion packages/material-ui/src/styles/overrides.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ import { LinearProgressClassKey } from '../LinearProgress';
import { LinkClassKey } from '../Link';
import { ListClassKey } from '../List';
import { ListItemAvatarClassKey } from '../ListItemAvatar';
import { ListItemClassKey } from '../ListItem';
import { ListItemClassKey, ListItemContainerClassKey } from '../ListItem';
import { ListItemIconClassKey } from '../ListItemIcon';
import { ListItemSecondaryActionClassKey } from '../ListItemSecondaryAction';
import { ListItemTextClassKey } from '../ListItemText';
Expand Down Expand Up @@ -174,6 +174,7 @@ export interface ComponentNameToClassKey {
MuiLink: LinkClassKey;
MuiList: ListClassKey;
MuiListItem: ListItemClassKey;
MuiListItemContainer: ListItemContainerClassKey;
MuiListItemAvatar: ListItemAvatarClassKey;
MuiListItemIcon: ListItemIconClassKey;
MuiListItemSecondaryAction: ListItemSecondaryActionClassKey;
Expand Down