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 1d91e0a commit e152271
Show file tree
Hide file tree
Showing 17 changed files with 316 additions and 50 deletions.
80 changes: 79 additions & 1 deletion packages/material-ui-benchmark/src/core.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
/* eslint-disable no-console */
/* eslint-disable no-console, react/no-array-index-key */

import Benchmark from 'benchmark';
import React from 'react';
import ReactDOMServer from 'react-dom/server';
import { StylesProvider } from '@material-ui/styles';
import ButtonBase from '@material-ui/core/ButtonBase';
import Table from '@material-ui/core/Table';
import TableBody from '@material-ui/core/TableBody';
import TableCell from '@material-ui/core/TableCell';
import TableHead from '@material-ui/core/TableHead';
import TableRow from '@material-ui/core/TableRow';

const suite = new Benchmark.Suite('core', {
onError: event => {
Expand All @@ -13,6 +18,65 @@ const suite = new Benchmark.Suite('core', {
});
Benchmark.options.minSamples = 100;

const data = { name: 'Frozen yoghurt', calories: 159, fat: 6.0, carbs: 24, protein: 4.0 };
const rows = Array.from(new Array(100)).map(() => data);

function TableMui() {
return (
<Table>
<TableHead>
<TableRow>
<TableCell>Dessert (100g serving)</TableCell>
<TableCell>Calories</TableCell>
<TableCell>Fat (g)</TableCell>
<TableCell>Carbs (g)</TableCell>
<TableCell>Protein (g)</TableCell>
</TableRow>
</TableHead>
<TableBody>
{rows.map((row, index) => (
<TableRow key={index}>
<TableCell component="th" scope="row">
{row.name}
</TableCell>
<TableCell>{row.calories}</TableCell>
<TableCell>{row.fat}</TableCell>
<TableCell>{row.carbs}</TableCell>
<TableCell>{row.protein}</TableCell>
</TableRow>
))}
</TableBody>
</Table>
);
}

function TableRaw() {
return (
<table>
<thead>
<tr>
<th>Dessert (100g serving)</th>
<th>Calories</th>
<th>Fat (g)</th>
<th>Carbs (g)</th>
<th>Protein (g)</th>
</tr>
</thead>
<tbody>
{rows.map((row, index) => (
<tr key={index}>
<th scope="row">{row.name}</th>
<td>{row.calories}</td>
<td>{row.fat}</td>
<td>{row.carbs}</td>
<td>{row.protein}</td>
</tr>
))}
</tbody>
</table>
);
}

function NakedButton(props) {
return <button type="button" {...props} />;
}
Expand All @@ -26,6 +90,20 @@ class HocButton extends React.Component {
}

suite
.add('TableRaw', () => {
ReactDOMServer.renderToString(
<StylesProvider sheetsCache={null} sheetsManager={new Map()}>
<TableRaw />
</StylesProvider>,
);
})
.add('TableMui', () => {
ReactDOMServer.renderToString(
<StylesProvider sheetsCache={null} sheetsManager={new Map()}>
<TableMui />
</StylesProvider>,
);
})
.add('ButtonBase', () => {
ReactDOMServer.renderToString(
<StylesProvider sheetsManager={new Map()}>
Expand Down
2 changes: 2 additions & 0 deletions packages/material-ui-styles/src/useThemeProps/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default } from './useThemeProps';
export * from './useThemeProps';
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';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default function useThemeProps(props: any, options: any): any;
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;
24 changes: 20 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,18 @@ 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: classesProp,
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 +45,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 +76,9 @@ Table.defaultProps = {
size: 'medium',
};

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

export default Table;
25 changes: 18 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: classesProp, 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,9 @@ TableBody.defaultProps = {
component: 'tbody',
};

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

export default TableBody;
19 changes: 14 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: classesProp,
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,9 @@ TableCell.defaultProps = {
align: 'inherit',
};

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

export default TableCell;
25 changes: 18 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: classesProp, 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,9 @@ TableFooter.defaultProps = {
component: 'tfoot',
};

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

export default TableFooter;
25 changes: 18 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: classesProp, 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,9 @@ TableHead.defaultProps = {
component: 'thead',
};

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

export default TableHead;
Loading

0 comments on commit e152271

Please sign in to comment.