-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
/
Copy pathTableCell.js
312 lines (295 loc) · 8.65 KB
/
TableCell.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
'use client';
import * as React from 'react';
import PropTypes from 'prop-types';
import clsx from 'clsx';
import composeClasses from '@mui/utils/composeClasses';
import { darken, alpha, lighten } from '@mui/system/colorManipulator';
import capitalize from '../utils/capitalize';
import TableContext from '../Table/TableContext';
import Tablelvl2Context from '../Table/Tablelvl2Context';
import { styled } from '../zero-styled';
import memoTheme from '../utils/memoTheme';
import { useDefaultProps } from '../DefaultPropsProvider';
import tableCellClasses, { getTableCellUtilityClass } from './tableCellClasses';
const useUtilityClasses = (ownerState) => {
const { classes, variant, align, padding, size, stickyHeader } = ownerState;
const slots = {
root: [
'root',
variant,
stickyHeader && 'stickyHeader',
align !== 'inherit' && `align${capitalize(align)}`,
padding !== 'normal' && `padding${capitalize(padding)}`,
`size${capitalize(size)}`,
],
};
return composeClasses(slots, getTableCellUtilityClass, classes);
};
const TableCellRoot = styled('td', {
name: 'MuiTableCell',
slot: 'Root',
overridesResolver: (props, styles) => {
const { ownerState } = props;
return [
styles.root,
styles[ownerState.variant],
styles[`size${capitalize(ownerState.size)}`],
ownerState.padding !== 'normal' && styles[`padding${capitalize(ownerState.padding)}`],
ownerState.align !== 'inherit' && styles[`align${capitalize(ownerState.align)}`],
ownerState.stickyHeader && styles.stickyHeader,
];
},
})(
memoTheme(({ theme }) => ({
...theme.typography.body2,
display: 'table-cell',
verticalAlign: 'inherit',
// Workaround for a rendering bug with spanned columns in Chrome 62.0.
// Removes the alpha (sets it to 1), and lightens or darkens the theme color.
borderBottom: theme.vars
? `1px solid ${theme.vars.palette.TableCell.border}`
: `1px solid
${
theme.palette.mode === 'light'
? lighten(alpha(theme.palette.divider, 1), 0.88)
: darken(alpha(theme.palette.divider, 1), 0.68)
}`,
textAlign: 'left',
padding: 16,
variants: [
{
props: {
variant: 'head',
},
style: {
color: (theme.vars || theme).palette.text.primary,
lineHeight: theme.typography.pxToRem(24),
fontWeight: theme.typography.fontWeightMedium,
},
},
{
props: {
variant: 'body',
},
style: {
color: (theme.vars || theme).palette.text.primary,
},
},
{
props: {
variant: 'footer',
},
style: {
color: (theme.vars || theme).palette.text.secondary,
lineHeight: theme.typography.pxToRem(21),
fontSize: theme.typography.pxToRem(12),
},
},
{
props: {
size: 'small',
},
style: {
padding: '6px 16px',
[`&.${tableCellClasses.paddingCheckbox}`]: {
width: 24, // prevent the checkbox column from growing
padding: '0 12px 0 16px',
'& > *': {
padding: 0,
},
},
},
},
{
props: {
padding: 'checkbox',
},
style: {
width: 48, // prevent the checkbox column from growing
padding: '0 0 0 4px',
},
},
{
props: {
padding: 'none',
},
style: {
padding: 0,
},
},
{
props: {
align: 'left',
},
style: {
textAlign: 'left',
},
},
{
props: {
align: 'center',
},
style: {
textAlign: 'center',
},
},
{
props: {
align: 'right',
},
style: {
textAlign: 'right',
flexDirection: 'row-reverse',
},
},
{
props: {
align: 'justify',
},
style: {
textAlign: 'justify',
},
},
{
props: ({ ownerState }) => ownerState.stickyHeader,
style: {
position: 'sticky',
top: 0,
zIndex: 2,
backgroundColor: (theme.vars || theme).palette.background.default,
},
},
],
})),
);
/**
* The component renders a `<th>` element when the parent context is a header
* or otherwise a `<td>` element.
*/
const TableCell = React.forwardRef(function TableCell(inProps, ref) {
const props = useDefaultProps({ props: inProps, name: 'MuiTableCell' });
const {
align = 'inherit',
className,
component: componentProp,
padding: paddingProp,
scope: scopeProp,
size: sizeProp,
sortDirection,
variant: variantProp,
...other
} = props;
const table = React.useContext(TableContext);
const tablelvl2 = React.useContext(Tablelvl2Context);
const isHeadCell = tablelvl2 && tablelvl2.variant === 'head';
let component;
if (componentProp) {
component = componentProp;
} else {
component = isHeadCell ? 'th' : 'td';
}
let scope = scopeProp;
// scope is not a valid attribute for <td/> elements.
// source: https://html.spec.whatwg.org/multipage/tables.html#the-td-element
if (component === 'td') {
scope = undefined;
} else if (!scope && isHeadCell) {
scope = 'col';
}
const variant = variantProp || (tablelvl2 && tablelvl2.variant);
const ownerState = {
...props,
align,
component,
padding: paddingProp || (table && table.padding ? table.padding : 'normal'),
size: sizeProp || (table && table.size ? table.size : 'medium'),
sortDirection,
stickyHeader: variant === 'head' && table && table.stickyHeader,
variant,
};
const classes = useUtilityClasses(ownerState);
let ariaSort = null;
if (sortDirection) {
ariaSort = sortDirection === 'asc' ? 'ascending' : 'descending';
}
return (
<TableCellRoot
as={component}
ref={ref}
className={clsx(classes.root, className)}
aria-sort={ariaSort}
scope={scope}
ownerState={ownerState}
{...other}
/>
);
});
TableCell.propTypes /* remove-proptypes */ = {
// ┌────────────────────────────── Warning ──────────────────────────────┐
// │ These PropTypes are generated from the TypeScript type definitions. │
// │ To update them, edit the d.ts file and run `pnpm proptypes`. │
// └─────────────────────────────────────────────────────────────────────┘
/**
* Set the text-align on the table cell content.
*
* Monetary or generally number fields **should be right aligned** as that allows
* you to add them up quickly in your head without having to worry about decimals.
* @default 'inherit'
*/
align: PropTypes.oneOf(['center', 'inherit', 'justify', 'left', 'right']),
/**
* The content of the component.
*/
children: PropTypes.node,
/**
* Override or extend the styles applied to the component.
*/
classes: PropTypes.object,
/**
* @ignore
*/
className: PropTypes.string,
/**
* The component used for the root node.
* Either a string to use a HTML element or a component.
*/
component: PropTypes.elementType,
/**
* Sets the padding applied to the cell.
* The prop defaults to the value (`'default'`) inherited from the parent Table component.
*/
padding: PropTypes.oneOf(['checkbox', 'none', 'normal']),
/**
* Set scope attribute.
*/
scope: PropTypes.string,
/**
* Specify the size of the cell.
* The prop defaults to the value (`'medium'`) inherited from the parent Table component.
*/
size: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([
PropTypes.oneOf(['medium', 'small']),
PropTypes.string,
]),
/**
* Set aria-sort direction.
*/
sortDirection: PropTypes.oneOf(['asc', 'desc', false]),
/**
* The system prop that allows defining system overrides as well as additional CSS styles.
*/
sx: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object, PropTypes.bool])),
PropTypes.func,
PropTypes.object,
]),
/**
* Specify the cell type.
* The prop defaults to the value inherited from the parent TableHead, TableBody, or TableFooter components.
*/
variant: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([
PropTypes.oneOf(['body', 'footer', 'head']),
PropTypes.string,
]),
};
export default TableCell;