UNPKG

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