UNPKG

7 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 capitalize from '../utils/capitalize';
9import { darken, alpha, lighten } from '../styles/colorManipulator';
10import TableContext from '../Table/TableContext';
11import Tablelvl2Context from '../Table/Tablelvl2Context';
12export const styles = theme => ({
13 /* Styles applied to the root element. */
14 root: _extends({}, theme.typography.body2, {
15 display: 'table-cell',
16 verticalAlign: 'inherit',
17 // Workaround for a rendering bug with spanned columns in Chrome 62.0.
18 // Removes the alpha (sets it to 1), and lightens or darkens the theme color.
19 borderBottom: `1px solid
20 ${theme.palette.type === 'light' ? lighten(alpha(theme.palette.divider, 1), 0.88) : darken(alpha(theme.palette.divider, 1), 0.68)}`,
21 textAlign: 'left',
22 padding: 16
23 }),
24
25 /* Styles applied to the root element if `variant="head"` or `context.table.head`. */
26 head: {
27 color: theme.palette.text.primary,
28 lineHeight: theme.typography.pxToRem(24),
29 fontWeight: theme.typography.fontWeightMedium
30 },
31
32 /* Styles applied to the root element if `variant="body"` or `context.table.body`. */
33 body: {
34 color: theme.palette.text.primary
35 },
36
37 /* Styles applied to the root element if `variant="footer"` or `context.table.footer`. */
38 footer: {
39 color: theme.palette.text.secondary,
40 lineHeight: theme.typography.pxToRem(21),
41 fontSize: theme.typography.pxToRem(12)
42 },
43
44 /* Styles applied to the root element if `size="small"`. */
45 sizeSmall: {
46 padding: '6px 24px 6px 16px',
47 '&:last-child': {
48 paddingRight: 16
49 },
50 '&$paddingCheckbox': {
51 width: 24,
52 // prevent the checkbox column from growing
53 padding: '0 12px 0 16px',
54 '&:last-child': {
55 paddingLeft: 12,
56 paddingRight: 16
57 },
58 '& > *': {
59 padding: 0
60 }
61 }
62 },
63
64 /* Styles applied to the root element if `padding="checkbox"`. */
65 paddingCheckbox: {
66 width: 48,
67 // prevent the checkbox column from growing
68 padding: '0 0 0 4px',
69 '&:last-child': {
70 paddingLeft: 0,
71 paddingRight: 4
72 }
73 },
74
75 /* Styles applied to the root element if `padding="none"`. */
76 paddingNone: {
77 padding: 0,
78 '&:last-child': {
79 padding: 0
80 }
81 },
82
83 /* Styles applied to the root element if `align="left"`. */
84 alignLeft: {
85 textAlign: 'left'
86 },
87
88 /* Styles applied to the root element if `align="center"`. */
89 alignCenter: {
90 textAlign: 'center'
91 },
92
93 /* Styles applied to the root element if `align="right"`. */
94 alignRight: {
95 textAlign: 'right',
96 flexDirection: 'row-reverse'
97 },
98
99 /* Styles applied to the root element if `align="justify"`. */
100 alignJustify: {
101 textAlign: 'justify'
102 },
103
104 /* Styles applied to the root element if `context.table.stickyHeader={true}`. */
105 stickyHeader: {
106 position: 'sticky',
107 top: 0,
108 left: 0,
109 zIndex: 2,
110 backgroundColor: theme.palette.background.default
111 }
112});
113/**
114 * The component renders a `<th>` element when the parent context is a header
115 * or otherwise a `<td>` element.
116 */
117
118const TableCell = /*#__PURE__*/React.forwardRef(function TableCell(props, ref) {
119 const {
120 align = 'inherit',
121 classes,
122 className,
123 component,
124 padding: paddingProp,
125 scope: scopeProp,
126 size: sizeProp,
127 sortDirection,
128 variant: variantProp
129 } = props,
130 other = _objectWithoutPropertiesLoose(props, ["align", "classes", "className", "component", "padding", "scope", "size", "sortDirection", "variant"]);
131
132 const table = React.useContext(TableContext);
133 const tablelvl2 = React.useContext(Tablelvl2Context);
134 const isHeadCell = tablelvl2 && tablelvl2.variant === 'head';
135 let role;
136 let Component;
137
138 if (component) {
139 Component = component;
140 role = isHeadCell ? 'columnheader' : 'cell';
141 } else {
142 Component = isHeadCell ? 'th' : 'td';
143 }
144
145 let scope = scopeProp;
146
147 if (!scope && isHeadCell) {
148 scope = 'col';
149 }
150
151 const padding = paddingProp || (table && table.padding ? table.padding : 'normal');
152 const size = sizeProp || (table && table.size ? table.size : 'medium');
153 const variant = variantProp || tablelvl2 && tablelvl2.variant;
154 let ariaSort = null;
155
156 if (sortDirection) {
157 ariaSort = sortDirection === 'asc' ? 'ascending' : 'descending';
158 }
159
160 return /*#__PURE__*/React.createElement(Component, _extends({
161 ref: ref,
162 className: clsx(classes.root, classes[variant], className, align !== 'inherit' && classes[`align${capitalize(align)}`], padding !== 'normal' && classes[`padding${capitalize(padding)}`], size !== 'medium' && classes[`size${capitalize(size)}`], variant === 'head' && table && table.stickyHeader && classes.stickyHeader),
163 "aria-sort": ariaSort,
164 role: role,
165 scope: scope
166 }, other));
167});
168process.env.NODE_ENV !== "production" ? TableCell.propTypes = {
169 /**
170 * Set the text-align on the table cell content.
171 *
172 * Monetary or generally number fields **should be right aligned** as that allows
173 * you to add them up quickly in your head without having to worry about decimals.
174 */
175 align: PropTypes.oneOf(['center', 'inherit', 'justify', 'left', 'right']),
176
177 /**
178 * The table cell contents.
179 */
180 children: PropTypes.node,
181
182 /**
183 * Override or extend the styles applied to the component.
184 * See [CSS API](#css) below for more details.
185 */
186 classes: PropTypes.object,
187
188 /**
189 * @ignore
190 */
191 className: PropTypes.string,
192
193 /**
194 * The component used for the root node.
195 * Either a string to use a HTML element or a component.
196 */
197 component: PropTypes
198 /* @typescript-to-proptypes-ignore */
199 .elementType,
200
201 /**
202 * Sets the padding applied to the cell.
203 * By default, the Table parent component set the value (`normal`).
204 * `default` is deprecated, use `normal` instead.
205 */
206 padding: chainPropTypes(PropTypes.oneOf(['normal', 'checkbox', 'none', 'default']), props => {
207 if (props.padding === 'default') {
208 return new Error('Material-UI: padding="default" was renamed to padding="normal" for consistency.');
209 }
210
211 return null;
212 }),
213
214 /**
215 * Set scope attribute.
216 */
217 scope: PropTypes.string,
218
219 /**
220 * Specify the size of the cell.
221 * By default, the Table parent component set the value (`medium`).
222 */
223 size: PropTypes.oneOf(['medium', 'small']),
224
225 /**
226 * Set aria-sort direction.
227 */
228 sortDirection: PropTypes.oneOf(['asc', 'desc', false]),
229
230 /**
231 * Specify the cell type.
232 * By default, the TableHead, TableBody or TableFooter parent component set the value.
233 */
234 variant: PropTypes.oneOf(['body', 'footer', 'head'])
235} : void 0;
236export default withStyles(styles, {
237 name: 'MuiTableCell'
238})(TableCell);
\No newline at end of file