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] Migrate ListItemSecondaryAction to emotion #24593

Merged
merged 4 commits into from
Jan 25, 2021
Merged
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
5 changes: 3 additions & 2 deletions docs/pages/api-docs/list-item-secondary-action.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"props": {
"children": { "type": { "name": "node" } },
"classes": { "type": { "name": "object" } }
"classes": { "type": { "name": "object" } },
"sx": { "type": { "name": "object" } }
},
"name": "ListItemSecondaryAction",
"styles": {
Expand All @@ -14,6 +15,6 @@
"filename": "/packages/material-ui/src/ListItemSecondaryAction/ListItemSecondaryAction.js",
"inheritance": null,
"demos": "<ul><li><a href=\"/components/lists/\">Lists</a></li></ul>",
"styledComponent": false,
"styledComponent": true,
"cssComponent": false
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"componentDescription": "Must be used as the last child of ListItem to function properly.",
"propDescriptions": {
"children": "The content of the component, normally an <code>IconButton</code> or selection control.",
"classes": "Override or extend the styles applied to the component. See <a href=\"#css\">CSS API</a> below for more details."
"classes": "Override or extend the styles applied to the component. See <a href=\"#css\">CSS API</a> below for more details.",
"sx": "The system prop that allows defining system overrides as well as additional CSS styles. See the <a href=\"/system/basics/#the-sx-prop\">`sx` page</a> for more details."
},
"classDescriptions": {
"root": { "description": "Styles applied to the root element." },
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { InternalStandardProps as StandardProps } from '..';
import { SxProps } from '@material-ui/system';
import { InternalStandardProps as StandardProps, Theme } from '..';

export interface ListItemSecondaryActionProps
extends StandardProps<React.HTMLAttributes<HTMLDivElement>> {
Expand All @@ -15,6 +16,10 @@ export interface ListItemSecondaryActionProps
/** Styles applied to the root element when the parent `ListItem` has `disableGutters={true}`. */
disableGutters?: string;
};
/**
* The system prop that allows defining system overrides as well as additional CSS styles.
*/
sx?: SxProps<Theme>;
}

export type ListItemSecondaryActionClassKey = keyof NonNullable<
Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,63 @@
import * as React from 'react';
import PropTypes from 'prop-types';
import clsx from 'clsx';
import { deepmerge } from '@material-ui/utils';
import { unstable_composeClasses as composeClasses } from '@material-ui/unstyled';
import experimentalStyled from '../styles/experimentalStyled';
import useThemeProps from '../styles/useThemeProps';
import ListContext from '../List/ListContext';
import withStyles from '../styles/withStyles';

export const styles = {
/* Styles applied to the root element. */
root: {
position: 'absolute',
right: 16,
top: '50%',
transform: 'translateY(-50%)',
import { getListItemSecondaryActionClassesUtilityClass } from './listItemSecondaryActionClasses';

const overridesResolver = (props, styles) => {
const { styleProps } = props;

return deepmerge(styles.root || {}, {
...(styleProps.disableGutters && styles.disableGutters),
});
};

const useUtilityClasses = (styleProps) => {
const { disableGutters, classes } = styleProps;

const slots = {
root: ['root', disableGutters && 'disableGutters'],
};

return composeClasses(slots, getListItemSecondaryActionClassesUtilityClass, classes);
};

const ListItemSecondaryActionRoot = experimentalStyled(
'div',
{},
{
name: 'MuiListItemSecondaryAction',
slot: 'Root',
overridesResolver,
},
/* Styles applied to the root element when the parent `ListItem` has `disableGutters={true}`. */
disableGutters: {
)(({ styleProps }) => ({
position: 'absolute',
right: 16,
top: '50%',
transform: 'translateY(-50%)',
...(styleProps.disableGutters && {
right: 0,
},
};
}),
}));

/**
* Must be used as the last child of ListItem to function properly.
*/
const ListItemSecondaryAction = React.forwardRef(function ListItemSecondaryAction(props, ref) {
const { classes, className, ...other } = props;
const ListItemSecondaryAction = React.forwardRef(function ListItemSecondaryAction(inProps, ref) {
const props = useThemeProps({ props: inProps, name: 'MuiListItemSecondaryAction' });
const { className, ...other } = props;
const context = React.useContext(ListContext);
const styleProps = { ...props, disableGutters: context.disableGutters };
const classes = useUtilityClasses(styleProps);

return (
<div
className={clsx(
classes.root,
{ [classes.disableGutters]: context.disableGutters },
className,
)}
<ListItemSecondaryActionRoot
className={clsx(classes.root, className)}
styleProps={styleProps}
ref={ref}
{...other}
/>
Expand All @@ -55,8 +81,12 @@ ListItemSecondaryAction.propTypes = {
* @ignore
*/
className: PropTypes.string,
/**
* The system prop that allows defining system overrides as well as additional CSS styles.
*/
sx: PropTypes.object,
};

ListItemSecondaryAction.muiName = 'ListItemSecondaryAction';

export default withStyles(styles, { name: 'MuiListItemSecondaryAction' })(ListItemSecondaryAction);
export default ListItemSecondaryAction;
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
import * as React from 'react';
import { expect } from 'chai';
import { getClasses, createMount, createClientRender, describeConformance } from 'test/utils';
import { createMount, createClientRender, describeConformanceV5 } from 'test/utils';
import ListItemSecondaryAction from './ListItemSecondaryAction';
import ListItem from '../ListItem';
import classes from './listItemSecondaryActionClasses';

describe('<ListItemSecondaryAction />', () => {
const mount = createMount();
const render = createClientRender();
let classes;

before(() => {
classes = getClasses(<ListItemSecondaryAction />);
});

describeConformance(<ListItemSecondaryAction />, () => ({
describeConformanceV5(<ListItemSecondaryAction />, () => ({
classes,
inheritComponent: 'div',
mount,
refInstanceof: window.HTMLDivElement,
skip: ['componentProp'],
muiName: 'MuiListItemSecondaryAction',
skip: ['componentProp', 'componentsProp', 'themeVariants'],
mnajdova marked this conversation as resolved.
Show resolved Hide resolved
}));

it('should render without classes that disable gutters', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export interface ListItemSecondaryActionClasses {
root: string;
disableGutters: string;
}

declare const listItemSecondaryActionClasses: ListItemSecondaryActionClasses;

export function getListItemSecondaryActionClassesUtilityClass(slot: string): string;

export default listItemSecondaryActionClasses;
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { generateUtilityClass, generateUtilityClasses } from '@material-ui/unstyled';

export function getListItemSecondaryActionClassesUtilityClass(slot) {
return generateUtilityClass('MuiListItemSecondaryAction', slot);
}

const listItemSecondaryActionClasses = generateUtilityClasses('MuiListItemSecondaryAction', [
'root',
'disableGutters',
]);

export default listItemSecondaryActionClasses;