UNPKG

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