UNPKG

3.04 kBJavaScriptView Raw
1import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
2import _extends from "@babel/runtime/helpers/esm/extends";
3import * as React from 'react';
4import PropTypes from 'prop-types';
5import clsx from 'clsx';
6import { chainPropTypes } from '@material-ui/utils';
7import withStyles from '../styles/withStyles';
8import TableContext from './TableContext';
9export const styles = theme => ({
10 /* Styles applied to the root element. */
11 root: {
12 display: 'table',
13 width: '100%',
14 borderCollapse: 'collapse',
15 borderSpacing: 0,
16 '& caption': _extends({}, theme.typography.body2, {
17 padding: theme.spacing(2),
18 color: theme.palette.text.secondary,
19 textAlign: 'left',
20 captionSide: 'bottom'
21 })
22 },
23
24 /* Styles applied to the root element if `stickyHeader={true}`. */
25 stickyHeader: {
26 borderCollapse: 'separate'
27 }
28});
29const defaultComponent = 'table';
30const Table = /*#__PURE__*/React.forwardRef(function Table(props, ref) {
31 const {
32 classes,
33 className,
34 component: Component = defaultComponent,
35 padding = 'normal',
36 size = 'medium',
37 stickyHeader = false
38 } = props,
39 other = _objectWithoutPropertiesLoose(props, ["classes", "className", "component", "padding", "size", "stickyHeader"]);
40
41 const table = React.useMemo(() => ({
42 padding,
43 size,
44 stickyHeader
45 }), [padding, size, stickyHeader]);
46 return /*#__PURE__*/React.createElement(TableContext.Provider, {
47 value: table
48 }, /*#__PURE__*/React.createElement(Component, _extends({
49 role: Component === defaultComponent ? null : 'table',
50 ref: ref,
51 className: clsx(classes.root, className, stickyHeader && classes.stickyHeader)
52 }, other)));
53});
54process.env.NODE_ENV !== "production" ? Table.propTypes = {
55 /**
56 * The content of the table, normally `TableHead` and `TableBody`.
57 */
58 children: PropTypes.node.isRequired,
59
60 /**
61 * Override or extend the styles applied to the component.
62 * See [CSS API](#css) below for more details.
63 */
64 classes: PropTypes.object.isRequired,
65
66 /**
67 * @ignore
68 */
69 className: PropTypes.string,
70
71 /**
72 * The component used for the root node.
73 * Either a string to use a HTML element or a component.
74 */
75 component: PropTypes
76 /* @typescript-to-proptypes-ignore */
77 .elementType,
78
79 /**
80 * Allows TableCells to inherit padding of the Table.
81 * `default` is deprecated, use `normal` instead.
82 */
83 padding: chainPropTypes(PropTypes.oneOf(['normal', 'checkbox', 'none', 'default']), props => {
84 if (props.padding === 'default') {
85 return new Error('Material-UI: padding="default" was renamed to padding="normal" for consistency.');
86 }
87
88 return null;
89 }),
90
91 /**
92 * Allows TableCells to inherit size of the Table.
93 */
94 size: PropTypes.oneOf(['small', 'medium']),
95
96 /**
97 * Set the header sticky.
98 *
99 * ⚠️ It doesn't work with IE 11.
100 */
101 stickyHeader: PropTypes.bool
102} : void 0;
103export default withStyles(styles, {
104 name: 'MuiTable'
105})(Table);
\No newline at end of file