UNPKG

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