UNPKG

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