Skip to content

Commit

Permalink
[Table] Use makeStyles over withStyles
Browse files Browse the repository at this point in the history
  • Loading branch information
oliviertassinari committed Mar 23, 2019
1 parent 6e750ac commit b183af8
Show file tree
Hide file tree
Showing 15 changed files with 216 additions and 48 deletions.
1 change: 1 addition & 0 deletions packages/material-ui-styles/src/useThemeProps/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './useThemeProps';
15 changes: 15 additions & 0 deletions packages/material-ui-styles/src/useThemeProps/useThemeProps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import useTheme from '../useTheme';
import getThemeProps from '../getThemeProps';

function useThemeProps(props, options) {
const { defaultTheme, name } = options;
const theme = useTheme() || defaultTheme;
const output = getThemeProps({
theme,
name,
props,
});
return output;
}

export default useThemeProps;
2 changes: 1 addition & 1 deletion packages/material-ui-styles/src/withStyles/withStyles.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const withStyles = (stylesOrCreator, options = {}) => Component => {
});

let WithStyles = React.forwardRef(function WithStyles(props, ref) {
const { classes: classesProp, innerRef, ...other } = props;
const { classes: _, innerRef, ...other } = props;
const classes = useStyles(props);

let theme;
Expand Down
19 changes: 15 additions & 4 deletions packages/material-ui/src/Table/Table.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React from 'react';
import PropTypes from 'prop-types';
import clsx from 'clsx';
import withStyles from '../styles/withStyles';
import makeStyles from '../styles/makeStyles';
import useThemeProps from '../styles/useThemeProps';
import TableContext from './TableContext';

export const styles = {
Expand All @@ -14,8 +15,14 @@ export const styles = {
},
};

export const useStyles = makeStyles(styles, { name: 'MuiTable' });

const Table = React.forwardRef(function Table(props, ref) {
const { classes, className, component: Component, padding, size, ...other } = props;
const { classes: _, className, component: Component, padding, size, ...other } = useThemeProps(
props,
{ name: 'MuiTable' },
);
const classes = useStyles(props);
const table = React.useMemo(() => ({ padding, size }), [padding, size]);

return (
Expand All @@ -34,7 +41,7 @@ Table.propTypes = {
* Override or extend the styles applied to the component.
* See [CSS API](#css) below for more details.
*/
classes: PropTypes.object.isRequired,
classes: PropTypes.object,
/**
* @ignore
*/
Expand Down Expand Up @@ -65,4 +72,8 @@ Table.defaultProps = {
size: 'medium',
};

export default withStyles(styles, { name: 'MuiTable' })(Table);
Table.options = {
name: 'MuiTable',
};

export default Table;
24 changes: 17 additions & 7 deletions packages/material-ui/src/TableBody/TableBody.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
import React from 'react';
import PropTypes from 'prop-types';
import clsx from 'clsx';
import withStyles from '../styles/withStyles';
import makeStyles from '../styles/makeStyles';
import useThemeProps from '../styles/useThemeProps';
import Tablelvl2Context from '../Table/Tablelvl2Context';

const tablelvl2 = {
variant: 'body',
};

export const styles = {
/* Styles applied to the root element. */
root: {
display: 'table-row-group',
},
};

const tablelvl2 = {
variant: 'body',
};
const useStyles = makeStyles(styles, { name: 'MuiTableBody' });

const TableBody = React.forwardRef(function TableBody(props, ref) {
const { classes, className, component: Component, ...other } = props;
const { classes: _, className, component: Component, ...other } = useThemeProps(props, {
name: 'MuiTableBody',
});
const classes = useStyles(props);

return (
<Tablelvl2Context.Provider value={tablelvl2}>
Expand All @@ -34,7 +40,7 @@ TableBody.propTypes = {
* Override or extend the styles applied to the component.
* See [CSS API](#css) below for more details.
*/
classes: PropTypes.object.isRequired,
classes: PropTypes.object,
/**
* @ignore
*/
Expand All @@ -50,4 +56,8 @@ TableBody.defaultProps = {
component: 'tbody',
};

export default withStyles(styles, { name: 'MuiTableBody' })(TableBody);
TableBody.options = {
name: 'MuiTableBody',
};

export default TableBody;
18 changes: 13 additions & 5 deletions packages/material-ui/src/TableCell/TableCell.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React from 'react';
import PropTypes from 'prop-types';
import clsx from 'clsx';
import withStyles from '../styles/withStyles';
import makeStyles from '../styles/makeStyles';
import useThemeProps from '../styles/useThemeProps';
import { capitalize } from '../utils/helpers';
import { darken, fade, lighten } from '../styles/colorManipulator';
import TableContext from '../Table/TableContext';
Expand Down Expand Up @@ -98,11 +99,13 @@ export const styles = theme => ({
},
});

const useStyles = makeStyles(styles, { name: 'MuiTableCell' });

const TableCell = React.forwardRef(function TableCell(props, ref) {
const {
align,
children,
classes,
classes: _,
className,
component,
padding: paddingProp,
Expand All @@ -111,8 +114,9 @@ const TableCell = React.forwardRef(function TableCell(props, ref) {
sortDirection,
variant,
...other
} = props;
} = useThemeProps(props, { name: 'MuiTableCell' });

const classes = useStyles(props);
const table = React.useContext(TableContext);
const tablelvl2 = React.useContext(Tablelvl2Context);

Expand Down Expand Up @@ -177,7 +181,7 @@ TableCell.propTypes = {
* Override or extend the styles applied to the component.
* See [CSS API](#css) below for more details.
*/
classes: PropTypes.object.isRequired,
classes: PropTypes.object,
/**
* @ignore
*/
Expand Down Expand Up @@ -216,4 +220,8 @@ TableCell.defaultProps = {
align: 'inherit',
};

export default withStyles(styles, { name: 'MuiTableCell' })(TableCell);
TableCell.options = {
name: 'MuiTableCell',
};

export default TableCell;
24 changes: 17 additions & 7 deletions packages/material-ui/src/TableFooter/TableFooter.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
import React from 'react';
import PropTypes from 'prop-types';
import clsx from 'clsx';
import withStyles from '../styles/withStyles';
import makeStyles from '../styles/makeStyles';
import useThemeProps from '../styles/useThemeProps';
import Tablelvl2Context from '../Table/Tablelvl2Context';

const tablelvl2 = {
variant: 'footer',
};

export const styles = {
/* Styles applied to the root element. */
root: {
display: 'table-footer-group',
},
};

const tablelvl2 = {
variant: 'footer',
};
const useStyles = makeStyles(styles, { name: 'MuiTableFooter' });

const TableFooter = React.forwardRef(function TableFooter(props, ref) {
const { classes, className, component: Component, ...other } = props;
const { classes: _, className, component: Component, ...other } = useThemeProps(props, {
name: 'MuiTableFooter',
});
const classes = useStyles(props);

return (
<Tablelvl2Context.Provider value={tablelvl2}>
Expand All @@ -34,7 +40,7 @@ TableFooter.propTypes = {
* Override or extend the styles applied to the component.
* See [CSS API](#css) below for more details.
*/
classes: PropTypes.object.isRequired,
classes: PropTypes.object,
/**
* @ignore
*/
Expand All @@ -50,4 +56,8 @@ TableFooter.defaultProps = {
component: 'tfoot',
};

export default withStyles(styles, { name: 'MuiTableFooter' })(TableFooter);
TableFooter.options = {
name: 'MuiTableFooter',
};

export default TableFooter;
24 changes: 17 additions & 7 deletions packages/material-ui/src/TableHead/TableHead.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
import React from 'react';
import PropTypes from 'prop-types';
import clsx from 'clsx';
import withStyles from '../styles/withStyles';
import makeStyles from '../styles/makeStyles';
import useThemeProps from '../styles/useThemeProps';
import Tablelvl2Context from '../Table/Tablelvl2Context';

const tablelvl2 = {
variant: 'head',
};

export const styles = {
/* Styles applied to the root element. */
root: {
display: 'table-header-group',
},
};

const tablelvl2 = {
variant: 'head',
};
const useStyles = makeStyles(styles, { name: 'MuiTableHead' });

const TableHead = React.forwardRef(function TableHead(props, ref) {
const { classes, className, component: Component, ...other } = props;
const { classes: _, className, component: Component, ...other } = useThemeProps(props, {
name: 'MuiTableHead',
});
const classes = useStyles(props);

return (
<Tablelvl2Context.Provider value={tablelvl2}>
Expand All @@ -34,7 +40,7 @@ TableHead.propTypes = {
* Override or extend the styles applied to the component.
* See [CSS API](#css) below for more details.
*/
classes: PropTypes.object.isRequired,
classes: PropTypes.object,
/**
* @ignore
*/
Expand All @@ -50,4 +56,8 @@ TableHead.defaultProps = {
component: 'thead',
};

export default withStyles(styles, { name: 'MuiTableHead' })(TableHead);
TableHead.options = {
name: 'MuiTableHead',
};

export default TableHead;
15 changes: 11 additions & 4 deletions packages/material-ui/src/TableRow/TableRow.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import PropTypes from 'prop-types';
import clsx from 'clsx';
import withStyles from '../styles/withStyles';
import makeStyles from '../styles/makeStyles';
import Tablelvl2Context from '../Table/Tablelvl2Context';

export const styles = theme => ({
Expand Down Expand Up @@ -35,13 +35,16 @@ export const styles = theme => ({
footer: {},
});

const useStyles = makeStyles(styles, { name: 'MuiTableRow' });

/**
* Will automatically set dynamic row height
* based on the material table element parent (head, body, etc).
*/
const TableRow = React.forwardRef(function TableRow(props, ref) {
const { classes, className, component: Component, hover, selected, ...other } = props;
const { classes: _, className, component: Component, hover, selected, ...other } = props;
const tablelvl2 = React.useContext(Tablelvl2Context);
const classes = useStyles(props);

return (
<Component
Expand Down Expand Up @@ -70,7 +73,7 @@ TableRow.propTypes = {
* Override or extend the styles applied to the component.
* See [CSS API](#css) below for more details.
*/
classes: PropTypes.object.isRequired,
classes: PropTypes.object,
/**
* @ignore
*/
Expand All @@ -96,4 +99,8 @@ TableRow.defaultProps = {
selected: false,
};

export default withStyles(styles, { name: 'MuiTableRow' })(TableRow);
TableRow.options = {
name: 'MuiTableRow',
};

export default TableRow;
18 changes: 13 additions & 5 deletions packages/material-ui/src/TableSortLabel/TableSortLabel.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import React from 'react';
import PropTypes from 'prop-types';
import clsx from 'clsx';
import ArrowDownwardIcon from '../internal/svg-icons/ArrowDownward';
import withStyles from '../styles/withStyles';
import makeStyles from '../styles/makeStyles';
import useThemeProps from '../styles/useThemeProps';
import ButtonBase from '../ButtonBase';
import { capitalize } from '../utils/helpers';

Expand Down Expand Up @@ -59,20 +60,23 @@ export const styles = theme => ({
},
});

const useStyles = makeStyles(styles, { name: 'MuiTableSortLabel' });

/**
* A button based label for placing inside `TableCell` for column sorting.
*/
const TableSortLabel = React.forwardRef(function TableSortLabel(props, ref) {
const {
active,
children,
classes,
classes: _,
className,
direction,
hideSortIcon,
IconComponent,
...other
} = props;
} = useThemeProps(props, { name: 'MuiTableSortLabel' });
const classes = useStyles(props);

return (
<ButtonBase
Expand Down Expand Up @@ -105,7 +109,7 @@ TableSortLabel.propTypes = {
* Override or extend the styles applied to the component.
* See [CSS API](#css) below for more details.
*/
classes: PropTypes.object.isRequired,
classes: PropTypes.object,
/**
* @ignore
*/
Expand All @@ -131,4 +135,8 @@ TableSortLabel.defaultProps = {
IconComponent: ArrowDownwardIcon,
};

export default withStyles(styles, { name: 'MuiTableSortLabel' })(TableSortLabel);
TableSortLabel.options = {
name: 'MuiTableSortLabel',
};

export default TableSortLabel;
1 change: 1 addition & 0 deletions packages/material-ui/src/styles/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ export { default as MuiThemeProvider } from './MuiThemeProvider';
export { default as styled } from './styled';
export * from './transitions';
export { default as useTheme } from './useTheme';
export { default as useThemeProps } from './useThemeProps';
export { default as withStyles } from './withStyles';
export { default as withTheme } from './withTheme';
Loading

0 comments on commit b183af8

Please sign in to comment.