-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
/
TableCell.js
228 lines (216 loc) · 6.41 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
import React from 'react';
import PropTypes from 'prop-types';
import clsx from 'clsx';
import makeStyles from '../styles/makeStyles';
import useThemeProps from '../styles/useThemeProps';
import { capitalize } from '../utils/helpers';
import { darken, fade, lighten } from '../styles/colorManipulator';
import TableContext from '../Table/TableContext';
import Tablelvl2Context from '../Table/Tablelvl2Context';
export const styles = theme => ({
/* Styles applied to the root element. */
root: {
...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: `1px solid
${
theme.palette.type === 'light'
? lighten(fade(theme.palette.divider, 1), 0.88)
: darken(fade(theme.palette.divider, 1), 0.68)
}`,
textAlign: 'left',
padding: '13px 40px 13px 16px',
'&:last-child': {
paddingRight: 16,
},
},
/* Styles applied to the root element if `variant="head"` or `context.table.head`. */
head: {
color: theme.palette.text.secondary,
fontSize: theme.typography.pxToRem(12),
lineHeight: theme.typography.pxToRem(21),
fontWeight: theme.typography.fontWeightMedium,
},
/* Styles applied to the root element if `variant="body"` or `context.table.body`. */
body: {
color: theme.palette.text.primary,
fontWeight: theme.typography.fontWeightRegular,
},
/* Styles applied to the root element if `variant="footer"` or `context.table.footer`. */
footer: {
color: theme.palette.text.secondary,
lineHeight: theme.typography.pxToRem(21),
fontSize: theme.typography.pxToRem(12),
},
/* Styles applied to the root element if `padding="dense"`. */
sizeSmall: {
padding: '5px 24px 5px 16px',
'&:last-child': {
paddingRight: 16,
},
'&$paddingCheckbox': {
width: 24, // prevent the checkbox column from growing
padding: '0px 12px 0 16px',
'&:last-child': {
paddingLeft: 12,
paddingRight: 16,
},
'& > *': {
padding: 0,
},
},
},
/* Styles applied to the root element if `padding="checkbox"`. */
paddingCheckbox: {
width: 48, // prevent the checkbox column from growing
padding: '0 0 0 4px',
'&:last-child': {
paddingLeft: 0,
paddingRight: 4,
},
},
/* Styles applied to the root element if `padding="none"`. */
paddingNone: {
padding: 0,
'&:last-child': {
padding: 0,
},
},
/* Styles applied to the root element if `align="left"`. */
alignLeft: {
textAlign: 'left',
},
/* Styles applied to the root element if `align="center"`. */
alignCenter: {
textAlign: 'center',
},
/* Styles applied to the root element if `align="right"`. */
alignRight: {
textAlign: 'right',
flexDirection: 'row-reverse',
},
/* Styles applied to the root element if `align="justify"`. */
alignJustify: {
textAlign: 'justify',
},
});
const useStyles = makeStyles(styles, { name: 'MuiTableCell' });
const TableCell = React.forwardRef(function TableCell(props, ref) {
const {
align,
children,
classes: _,
className,
component,
padding: paddingProp,
scope: scopeProp,
size: sizeProp,
sortDirection,
variant,
...other
} = useThemeProps(props, { name: 'MuiTableCell' });
const classes = useStyles(props);
const table = React.useContext(TableContext);
const tablelvl2 = React.useContext(Tablelvl2Context);
let Component;
if (component) {
Component = component;
} else {
Component = tablelvl2 && tablelvl2.variant === 'head' ? 'th' : 'td';
}
let scope = scopeProp;
if (!scope && tablelvl2 && tablelvl2.variant === 'head') {
scope = 'col';
}
const padding = paddingProp || (table && table.padding ? table.padding : 'default');
const size = sizeProp || (table && table.size ? table.size : 'medium');
let ariaSort = null;
if (sortDirection) {
ariaSort = sortDirection === 'asc' ? 'ascending' : 'descending';
}
return (
<Component
ref={ref}
className={clsx(
classes.root,
{
[classes.head]: variant ? variant === 'head' : tablelvl2 && tablelvl2.variant === 'head',
[classes.body]: variant ? variant === 'body' : tablelvl2 && tablelvl2.variant === 'body',
[classes.footer]: variant
? variant === 'footer'
: tablelvl2 && tablelvl2.variant === 'footer',
[classes[`align${capitalize(align)}`]]: align !== 'inherit',
[classes[`padding${capitalize(padding)}`]]: padding !== 'default',
[classes[`size${capitalize(size)}`]]: size !== 'medium',
},
className,
)}
aria-sort={ariaSort}
scope={scope}
{...other}
>
{children}
</Component>
);
});
TableCell.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.
*/
align: PropTypes.oneOf(['inherit', 'left', 'center', 'right', 'justify']),
/**
* The table cell contents.
*/
children: PropTypes.node,
/**
* Override or extend the styles applied to the component.
* See [CSS API](#css) below for more details.
*/
classes: PropTypes.object,
/**
* @ignore
*/
className: PropTypes.string,
/**
* The component used for the root node.
* Either a string to use a DOM element or a component.
*/
component: PropTypes.elementType,
/**
* Sets the padding applied to the cell.
* By default, the Table parent component set the value.
*/
padding: PropTypes.oneOf(['default', 'checkbox', 'none']),
/**
* Set scope attribute.
*/
scope: PropTypes.string,
/**
* Specify the size of the cell.
* By default, the Table parent component set the value (`normal`).
*/
size: PropTypes.oneOf(['small', 'medium']),
/**
* Set aria-sort direction.
*/
sortDirection: PropTypes.oneOf(['asc', 'desc', false]),
/**
* Specify the cell type.
* By default, the TableHead, TableBody or TableFooter parent component set the value.
*/
variant: PropTypes.oneOf(['head', 'body', 'footer']),
};
TableCell.defaultProps = {
align: 'inherit',
};
TableCell.useStyles = useStyles;
TableCell.options = {
name: 'MuiTableCell',
};
export default TableCell;