diff --git a/packages/material-ui-styles/src/useThemeProps/index.js b/packages/material-ui-styles/src/useThemeProps/index.js new file mode 100644 index 00000000000000..9bc4fe796288cd --- /dev/null +++ b/packages/material-ui-styles/src/useThemeProps/index.js @@ -0,0 +1 @@ +export { default } from './useThemeProps'; diff --git a/packages/material-ui-styles/src/useThemeProps/useThemeProps.d.ts b/packages/material-ui-styles/src/useThemeProps/useThemeProps.d.ts new file mode 100644 index 00000000000000..e69de29bb2d1d6 diff --git a/packages/material-ui-styles/src/useThemeProps/useThemeProps.js b/packages/material-ui-styles/src/useThemeProps/useThemeProps.js new file mode 100644 index 00000000000000..4775971219caf2 --- /dev/null +++ b/packages/material-ui-styles/src/useThemeProps/useThemeProps.js @@ -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; diff --git a/packages/material-ui-styles/src/withStyles/withStyles.js b/packages/material-ui-styles/src/withStyles/withStyles.js index 3cf3cbe9bb2d84..f099f1ec9c325f 100644 --- a/packages/material-ui-styles/src/withStyles/withStyles.js +++ b/packages/material-ui-styles/src/withStyles/withStyles.js @@ -45,7 +45,7 @@ const withStyles = (stylesOrCreator, options = {}) => Component => { }); const WithStyles = React.forwardRef(function WithStyles(props, ref) { - const { classes: classesProp, innerRef, ...other } = props; + const { classes: _, innerRef, ...other } = props; const classes = useStyles(props); let theme; diff --git a/packages/material-ui/src/Table/Table.js b/packages/material-ui/src/Table/Table.js index 81d260355393c8..4719858cb51991 100644 --- a/packages/material-ui/src/Table/Table.js +++ b/packages/material-ui/src/Table/Table.js @@ -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 = { @@ -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 ( @@ -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 */ @@ -65,4 +72,9 @@ Table.defaultProps = { size: 'medium', }; -export default withStyles(styles, { name: 'MuiTable' })(Table); +Table.useStyles = useStyles; +Table.options = { + name: 'MuiTable', +}; + +export default Table; diff --git a/packages/material-ui/src/TableBody/TableBody.js b/packages/material-ui/src/TableBody/TableBody.js index fb4f6c7102c2c8..4cab1263ad19df 100644 --- a/packages/material-ui/src/TableBody/TableBody.js +++ b/packages/material-ui/src/TableBody/TableBody.js @@ -1,9 +1,14 @@ 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: { @@ -11,12 +16,13 @@ export const styles = { }, }; -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 ( @@ -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 */ @@ -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; diff --git a/packages/material-ui/src/TableCell/TableCell.js b/packages/material-ui/src/TableCell/TableCell.js index 0b5480e8d876bf..32588c939713ac 100644 --- a/packages/material-ui/src/TableCell/TableCell.js +++ b/packages/material-ui/src/TableCell/TableCell.js @@ -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'; @@ -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, @@ -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); @@ -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 */ @@ -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; diff --git a/packages/material-ui/src/TableFooter/TableFooter.js b/packages/material-ui/src/TableFooter/TableFooter.js index ba9e344a3d7560..c5133ff7bd8cae 100644 --- a/packages/material-ui/src/TableFooter/TableFooter.js +++ b/packages/material-ui/src/TableFooter/TableFooter.js @@ -1,9 +1,14 @@ 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: { @@ -11,12 +16,13 @@ export const styles = { }, }; -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 ( @@ -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 */ @@ -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; diff --git a/packages/material-ui/src/TableHead/TableHead.js b/packages/material-ui/src/TableHead/TableHead.js index fda71dced55d36..99dd266495870b 100644 --- a/packages/material-ui/src/TableHead/TableHead.js +++ b/packages/material-ui/src/TableHead/TableHead.js @@ -1,9 +1,14 @@ 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: { @@ -11,12 +16,13 @@ export const styles = { }, }; -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 ( @@ -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 */ @@ -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; diff --git a/packages/material-ui/src/TableRow/TableRow.js b/packages/material-ui/src/TableRow/TableRow.js index 2d49ac4732ec86..e266c462b901c2 100644 --- a/packages/material-ui/src/TableRow/TableRow.js +++ b/packages/material-ui/src/TableRow/TableRow.js @@ -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 => ({ @@ -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 ( ({ }, }); +const useStyles = makeStyles(styles, { name: 'MuiTableSortLabel' }); + /** * A button based label for placing inside `TableCell` for column sorting. */ @@ -66,13 +69,14 @@ 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 ( data); + +function TableMui() { + return ( + + + + + Dessert (100g serving) + Calories + Fat (g) + Carbs (g) + Protein (g) + + + + {rows.map((row, index) => ( + + + {row.name} + + {row.calories} + {row.fat} + {row.carbs} + {row.protein} + + ))} + +
+
+ ); +} + +export default TableMui; diff --git a/pages/performance/tableRaw.js b/pages/performance/tableRaw.js new file mode 100644 index 00000000000000..062557b39e5aa6 --- /dev/null +++ b/pages/performance/tableRaw.js @@ -0,0 +1,38 @@ +/* eslint-disable react/no-array-index-key */ + +import React from 'react'; +import NoSsr from '@material-ui/core/NoSsr'; + +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 TableRaw() { + return ( + + + + + + + + + + + + + {rows.map((row, index) => ( + + + + + + + + ))} + +
Dessert (100g serving)CaloriesFat (g)Carbs (g)Protein (g)
{row.name}{row.calories}{row.fat}{row.carbs}{row.protein}
+
+ ); +} + +export default TableRaw;