Skip to content

Commit ce28e80

Browse files
[Table] Use makeStyles over withStyles
1 parent 0c0f9b1 commit ce28e80

File tree

17 files changed

+316
-50
lines changed

17 files changed

+316
-50
lines changed

packages/material-ui-benchmark/src/core.js

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1-
/* eslint-disable no-console */
1+
/* eslint-disable no-console, react/no-array-index-key */
22

33
import Benchmark from 'benchmark';
44
import React from 'react';
55
import ReactDOMServer from 'react-dom/server';
66
import { StylesProvider } from '@material-ui/styles';
77
import ButtonBase from '@material-ui/core/ButtonBase';
8+
import Table from '@material-ui/core/Table';
9+
import TableBody from '@material-ui/core/TableBody';
10+
import TableCell from '@material-ui/core/TableCell';
11+
import TableHead from '@material-ui/core/TableHead';
12+
import TableRow from '@material-ui/core/TableRow';
813

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

21+
const data = { name: 'Frozen yoghurt', calories: 159, fat: 6.0, carbs: 24, protein: 4.0 };
22+
const rows = Array.from(new Array(100)).map(() => data);
23+
24+
function TableMui() {
25+
return (
26+
<Table>
27+
<TableHead>
28+
<TableRow>
29+
<TableCell>Dessert (100g serving)</TableCell>
30+
<TableCell>Calories</TableCell>
31+
<TableCell>Fat (g)</TableCell>
32+
<TableCell>Carbs (g)</TableCell>
33+
<TableCell>Protein (g)</TableCell>
34+
</TableRow>
35+
</TableHead>
36+
<TableBody>
37+
{rows.map((row, index) => (
38+
<TableRow key={index}>
39+
<TableCell component="th" scope="row">
40+
{row.name}
41+
</TableCell>
42+
<TableCell>{row.calories}</TableCell>
43+
<TableCell>{row.fat}</TableCell>
44+
<TableCell>{row.carbs}</TableCell>
45+
<TableCell>{row.protein}</TableCell>
46+
</TableRow>
47+
))}
48+
</TableBody>
49+
</Table>
50+
);
51+
}
52+
53+
function TableRaw() {
54+
return (
55+
<table>
56+
<thead>
57+
<tr>
58+
<th>Dessert (100g serving)</th>
59+
<th>Calories</th>
60+
<th>Fat (g)</th>
61+
<th>Carbs (g)</th>
62+
<th>Protein (g)</th>
63+
</tr>
64+
</thead>
65+
<tbody>
66+
{rows.map((row, index) => (
67+
<tr key={index}>
68+
<th scope="row">{row.name}</th>
69+
<td>{row.calories}</td>
70+
<td>{row.fat}</td>
71+
<td>{row.carbs}</td>
72+
<td>{row.protein}</td>
73+
</tr>
74+
))}
75+
</tbody>
76+
</table>
77+
);
78+
}
79+
1680
function NakedButton(props) {
1781
return <button type="button" {...props} />;
1882
}
@@ -26,6 +90,20 @@ class HocButton extends React.Component {
2690
}
2791

2892
suite
93+
.add('TableRaw', () => {
94+
ReactDOMServer.renderToString(
95+
<StylesProvider sheetsCache={null} sheetsManager={new Map()}>
96+
<TableRaw />
97+
</StylesProvider>,
98+
);
99+
})
100+
.add('TableMui', () => {
101+
ReactDOMServer.renderToString(
102+
<StylesProvider sheetsCache={null} sheetsManager={new Map()}>
103+
<TableMui />
104+
</StylesProvider>,
105+
);
106+
})
29107
.add('ButtonBase', () => {
30108
ReactDOMServer.renderToString(
31109
<StylesProvider sheetsManager={new Map()}>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export { default } from './useThemeProps';
2+
export * from './useThemeProps';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default } from './useThemeProps';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default function useThemeProps(props: any, options: any): any;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import useTheme from '../useTheme';
2+
import getThemeProps from '../getThemeProps';
3+
4+
function useThemeProps(props, options) {
5+
const { defaultTheme, name } = options;
6+
const theme = useTheme() || defaultTheme;
7+
const output = getThemeProps({
8+
theme,
9+
name,
10+
props,
11+
});
12+
return output;
13+
}
14+
15+
export default useThemeProps;

packages/material-ui/src/Table/Table.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import React from 'react';
22
import PropTypes from 'prop-types';
33
import clsx from 'clsx';
4-
import withStyles from '../styles/withStyles';
4+
import makeStyles from '../styles/makeStyles';
5+
import useThemeProps from '../styles/useThemeProps';
56
import TableContext from './TableContext';
67

78
export const styles = {
@@ -14,8 +15,18 @@ export const styles = {
1415
},
1516
};
1617

18+
export const useStyles = makeStyles(styles, { name: 'MuiTable' });
19+
1720
const Table = React.forwardRef(function Table(props, ref) {
18-
const { classes, className, component: Component, padding, size, ...other } = props;
21+
const {
22+
classes: classesProp,
23+
className,
24+
component: Component,
25+
padding,
26+
size,
27+
...other
28+
} = useThemeProps(props, { name: 'MuiTable' });
29+
const classes = useStyles(props);
1930
const table = React.useMemo(() => ({ padding, size }), [padding, size]);
2031

2132
return (
@@ -34,7 +45,7 @@ Table.propTypes = {
3445
* Override or extend the styles applied to the component.
3546
* See [CSS API](#css) below for more details.
3647
*/
37-
classes: PropTypes.object.isRequired,
48+
classes: PropTypes.object,
3849
/**
3950
* @ignore
4051
*/
@@ -65,4 +76,9 @@ Table.defaultProps = {
6576
size: 'medium',
6677
};
6778

68-
export default withStyles(styles, { name: 'MuiTable' })(Table);
79+
Table.useStyles = useStyles;
80+
Table.options = {
81+
name: 'MuiTable',
82+
};
83+
84+
export default Table;

packages/material-ui/src/TableBody/TableBody.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,28 @@
11
import React from 'react';
22
import PropTypes from 'prop-types';
33
import clsx from 'clsx';
4-
import withStyles from '../styles/withStyles';
4+
import makeStyles from '../styles/makeStyles';
5+
import useThemeProps from '../styles/useThemeProps';
56
import Tablelvl2Context from '../Table/Tablelvl2Context';
67

8+
const tablelvl2 = {
9+
variant: 'body',
10+
};
11+
712
export const styles = {
813
/* Styles applied to the root element. */
914
root: {
1015
display: 'table-row-group',
1116
},
1217
};
1318

14-
const tablelvl2 = {
15-
variant: 'body',
16-
};
19+
const useStyles = makeStyles(styles, { name: 'MuiTableBody' });
1720

1821
const TableBody = React.forwardRef(function TableBody(props, ref) {
19-
const { classes, className, component: Component, ...other } = props;
22+
const { classes: classesProp, className, component: Component, ...other } = useThemeProps(props, {
23+
name: 'MuiTableBody',
24+
});
25+
const classes = useStyles(props);
2026

2127
return (
2228
<Tablelvl2Context.Provider value={tablelvl2}>
@@ -34,7 +40,7 @@ TableBody.propTypes = {
3440
* Override or extend the styles applied to the component.
3541
* See [CSS API](#css) below for more details.
3642
*/
37-
classes: PropTypes.object.isRequired,
43+
classes: PropTypes.object,
3844
/**
3945
* @ignore
4046
*/
@@ -50,4 +56,9 @@ TableBody.defaultProps = {
5056
component: 'tbody',
5157
};
5258

53-
export default withStyles(styles, { name: 'MuiTableBody' })(TableBody);
59+
TableBody.useStyles = useStyles;
60+
TableBody.options = {
61+
name: 'MuiTableBody',
62+
};
63+
64+
export default TableBody;

packages/material-ui/src/TableCell/TableCell.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import React from 'react';
22
import PropTypes from 'prop-types';
33
import clsx from 'clsx';
4-
import withStyles from '../styles/withStyles';
4+
import makeStyles from '../styles/makeStyles';
5+
import useThemeProps from '../styles/useThemeProps';
56
import { capitalize } from '../utils/helpers';
67
import { darken, fade, lighten } from '../styles/colorManipulator';
78
import TableContext from '../Table/TableContext';
@@ -98,11 +99,13 @@ export const styles = theme => ({
9899
},
99100
});
100101

102+
const useStyles = makeStyles(styles, { name: 'MuiTableCell' });
103+
101104
const TableCell = React.forwardRef(function TableCell(props, ref) {
102105
const {
103106
align,
104107
children,
105-
classes,
108+
classes: classesProp,
106109
className,
107110
component,
108111
padding: paddingProp,
@@ -111,8 +114,9 @@ const TableCell = React.forwardRef(function TableCell(props, ref) {
111114
sortDirection,
112115
variant,
113116
...other
114-
} = props;
117+
} = useThemeProps(props, { name: 'MuiTableCell' });
115118

119+
const classes = useStyles(props);
116120
const table = React.useContext(TableContext);
117121
const tablelvl2 = React.useContext(Tablelvl2Context);
118122

@@ -177,7 +181,7 @@ TableCell.propTypes = {
177181
* Override or extend the styles applied to the component.
178182
* See [CSS API](#css) below for more details.
179183
*/
180-
classes: PropTypes.object.isRequired,
184+
classes: PropTypes.object,
181185
/**
182186
* @ignore
183187
*/
@@ -216,4 +220,9 @@ TableCell.defaultProps = {
216220
align: 'inherit',
217221
};
218222

219-
export default withStyles(styles, { name: 'MuiTableCell' })(TableCell);
223+
TableCell.useStyles = useStyles;
224+
TableCell.options = {
225+
name: 'MuiTableCell',
226+
};
227+
228+
export default TableCell;

packages/material-ui/src/TableFooter/TableFooter.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,28 @@
11
import React from 'react';
22
import PropTypes from 'prop-types';
33
import clsx from 'clsx';
4-
import withStyles from '../styles/withStyles';
4+
import makeStyles from '../styles/makeStyles';
5+
import useThemeProps from '../styles/useThemeProps';
56
import Tablelvl2Context from '../Table/Tablelvl2Context';
67

8+
const tablelvl2 = {
9+
variant: 'footer',
10+
};
11+
712
export const styles = {
813
/* Styles applied to the root element. */
914
root: {
1015
display: 'table-footer-group',
1116
},
1217
};
1318

14-
const tablelvl2 = {
15-
variant: 'footer',
16-
};
19+
const useStyles = makeStyles(styles, { name: 'MuiTableFooter' });
1720

1821
const TableFooter = React.forwardRef(function TableFooter(props, ref) {
19-
const { classes, className, component: Component, ...other } = props;
22+
const { classes: classesProp, className, component: Component, ...other } = useThemeProps(props, {
23+
name: 'MuiTableFooter',
24+
});
25+
const classes = useStyles(props);
2026

2127
return (
2228
<Tablelvl2Context.Provider value={tablelvl2}>
@@ -34,7 +40,7 @@ TableFooter.propTypes = {
3440
* Override or extend the styles applied to the component.
3541
* See [CSS API](#css) below for more details.
3642
*/
37-
classes: PropTypes.object.isRequired,
43+
classes: PropTypes.object,
3844
/**
3945
* @ignore
4046
*/
@@ -50,4 +56,9 @@ TableFooter.defaultProps = {
5056
component: 'tfoot',
5157
};
5258

53-
export default withStyles(styles, { name: 'MuiTableFooter' })(TableFooter);
59+
TableFooter.useStyles = useStyles;
60+
TableFooter.options = {
61+
name: 'MuiTableFooter',
62+
};
63+
64+
export default TableFooter;

packages/material-ui/src/TableHead/TableHead.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,28 @@
11
import React from 'react';
22
import PropTypes from 'prop-types';
33
import clsx from 'clsx';
4-
import withStyles from '../styles/withStyles';
4+
import makeStyles from '../styles/makeStyles';
5+
import useThemeProps from '../styles/useThemeProps';
56
import Tablelvl2Context from '../Table/Tablelvl2Context';
67

8+
const tablelvl2 = {
9+
variant: 'head',
10+
};
11+
712
export const styles = {
813
/* Styles applied to the root element. */
914
root: {
1015
display: 'table-header-group',
1116
},
1217
};
1318

14-
const tablelvl2 = {
15-
variant: 'head',
16-
};
19+
const useStyles = makeStyles(styles, { name: 'MuiTableHead' });
1720

1821
const TableHead = React.forwardRef(function TableHead(props, ref) {
19-
const { classes, className, component: Component, ...other } = props;
22+
const { classes: classesProp, className, component: Component, ...other } = useThemeProps(props, {
23+
name: 'MuiTableHead',
24+
});
25+
const classes = useStyles(props);
2026

2127
return (
2228
<Tablelvl2Context.Provider value={tablelvl2}>
@@ -34,7 +40,7 @@ TableHead.propTypes = {
3440
* Override or extend the styles applied to the component.
3541
* See [CSS API](#css) below for more details.
3642
*/
37-
classes: PropTypes.object.isRequired,
43+
classes: PropTypes.object,
3844
/**
3945
* @ignore
4046
*/
@@ -50,4 +56,9 @@ TableHead.defaultProps = {
5056
component: 'thead',
5157
};
5258

53-
export default withStyles(styles, { name: 'MuiTableHead' })(TableHead);
59+
TableHead.useStyles = useStyles;
60+
TableHead.options = {
61+
name: 'MuiTableHead',
62+
};
63+
64+
export default TableHead;

0 commit comments

Comments
 (0)