UNPKG

@amaui/ui-react

Version:
227 lines (226 loc) 12.3 kB
"use strict"; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const jsx_runtime_1 = require("react/jsx-runtime"); const react_1 = __importDefault(require("react")); const utils_1 = require("@amaui/utils"); const style_react_1 = require("@amaui/style-react"); const IconMaterialArrowDownwardAltW100_1 = __importDefault(require("@amaui/icons-material-rounded-react/IconMaterialArrowDownwardAltW100")); const Line_1 = __importDefault(require("../Line")); const IconButton_1 = __importDefault(require("../IconButton")); const Type_1 = __importDefault(require("../Type")); const Divider_1 = __importDefault(require("../Divider")); const utils_2 = require("../utils"); const Tooltip_1 = __importDefault(require("../Tooltip")); const useStyle = (0, style_react_1.style)(theme => ({ root: Object.assign(Object.assign({ display: 'table-cell', position: 'relative', zIndex: '1', background: 'inherit' }, theme.typography.values.b2), { '&::before': { content: "''", position: 'absolute', inset: '0', background: 'currentColor', opacity: '0', transition: theme.methods.transitions.make('opacity', { duration: 'xxs' }), zIndex: '-1' } }), value: {}, size_small: { padding: `${theme.methods.space.value(1, 'px')} ${theme.methods.space.value(2, 'px')}` }, size_regular: { padding: theme.methods.space.value(2, 'px') }, size_large: { padding: theme.methods.space.value(3, 'px') }, head: { fontWeight: 'bold' }, body: {}, divider: { '&.amaui-Divider-root': { position: 'absolute', left: '0px', bottom: '0px', margin: '0px' } }, noWeight: { fontWeight: '400' }, sticky: { position: 'sticky', zIndex: '14', '&::after': { content: "''", position: 'absolute', top: '0', right: '-24px', bottom: '0', width: '24px', transition: theme.methods.transitions.make('box-shadow'), pointerEvents: 'none' } }, stickyActive: { '&::after': { boxShadow: `inset 11px 0 7px -7px ${theme.palette.light ? 'rgba(0, 0, 0, 0.04)' : 'rgba(255, 255, 255, 0.14)'}` } }, sort: { cursor: 'pointer', borderRadius: 2, '& > *:nth-child(1)': { // '&:focus': { // outline: `1px solid ${theme.palette.text.default.primary}`, // outlineOffset: 2 // } } }, sortedBy: { '& .amaui-Icon-root': { transition: theme.methods.transitions.make('transform') } }, sortedBy_asc: { '& .amaui-Icon-root': { transform: 'rotate(180deg)' } } }), { name: 'amaui-TableCell' }); const TableCell = react_1.default.forwardRef((props_, ref) => { const theme = (0, style_react_1.useAmauiTheme)(); const props = react_1.default.useMemo(() => { var _a, _b, _c, _d, _e, _f, _g, _h; return (Object.assign(Object.assign(Object.assign({}, (_d = (_c = (_b = (_a = theme === null || theme === void 0 ? void 0 : theme.ui) === null || _a === void 0 ? void 0 : _a.elements) === null || _b === void 0 ? void 0 : _b.all) === null || _c === void 0 ? void 0 : _c.props) === null || _d === void 0 ? void 0 : _d.default), (_h = (_g = (_f = (_e = theme === null || theme === void 0 ? void 0 : theme.ui) === null || _e === void 0 ? void 0 : _e.elements) === null || _f === void 0 ? void 0 : _f.amauiTableCell) === null || _g === void 0 ? void 0 : _g.props) === null || _h === void 0 ? void 0 : _h.default), props_)); }, [props_]); const Line = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.Line) || Line_1.default; }, [theme]); const Type = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.Type) || Type_1.default; }, [theme]); const IconButton = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.IconButton) || IconButton_1.default; }, [theme]); const Divider = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.Divider) || Divider_1.default; }, [theme]); const { tonal = true, color, size = 'regular', position = 'body', align = 'center', justify = 'flex-end', noWeight, sort, sortedBy: sortedBy_, sortedByDefault, onSort: onSort_, sticky, stickyPosition = 'left', stickyOffset, disabled, IconArrow = IconMaterialArrowDownwardAltW100_1.default, IconButtonSortProps, TypeProps, DividerProps, Component = props.position === 'head' ? 'th' : 'td', className, style, children } = props, other = __rest(props, ["tonal", "color", "size", "position", "align", "justify", "noWeight", "sort", "sortedBy", "sortedByDefault", "onSort", "sticky", "stickyPosition", "stickyOffset", "disabled", "IconArrow", "IconButtonSortProps", "TypeProps", "DividerProps", "Component", "className", "style", "children"]); const { classes } = useStyle(); const [root, setRoot] = react_1.default.useState(); const [stickyActive, setStickyActive] = react_1.default.useState(false); const [offset, setOffset] = react_1.default.useState(0); const [sortedBy, setSortedBy] = react_1.default.useState(sortedByDefault); const refs = { root: react_1.default.useRef(undefined) }; const init = react_1.default.useCallback(() => { setTimeout(() => { var _a; if (sticky) { const parent = (_a = refs.root.current) === null || _a === void 0 ? void 0 : _a.parentElement; if (parent) { const elements = Array.from(parent.children); const index = elements.findIndex(item => item === refs.root.current); let elementsOffset = stickyPosition === 'left' ? elements.slice(0, index) : elements.slice(index + 1); elementsOffset = elementsOffset.filter(item => item.classList.contains('amaui-TableCell-sticky')); const offset_ = elementsOffset.reduce((result, item) => { result += item.clientWidth; return result; }, 0); setOffset((0, utils_1.clamp)(offset_, 0)); } } }, 140); }, [sticky, stickyPosition]); react_1.default.useEffect(() => { // init init(); }, []); react_1.default.useEffect(() => { if (sortedBy !== sortedBy_) setSortedBy(sortedBy_); }, [sortedBy_]); react_1.default.useEffect(() => { if (sticky) { if (root) { const parentOverflow = (0, utils_2.getOverflowParent)(root); const offsetPrevious = stickyPosition === 'left' ? root.offsetLeft : (window.innerWidth - root.getBoundingClientRect().right); const method = () => { const offsetNew = stickyPosition === 'left' ? root.offsetLeft : (window.innerWidth - root.getBoundingClientRect().right); setStickyActive(offsetPrevious !== offsetNew); }; if (parentOverflow) parentOverflow.addEventListener('scroll', method, { passive: false }); return () => { parentOverflow.removeEventListener('scroll', method); }; } } }, [sticky, stickyPosition, root]); const onSort = react_1.default.useCallback(() => { let valueNew; setSortedBy(item => { valueNew = item === 'asc' ? 'desc' : 'asc'; return valueNew; }); if ((0, utils_1.is)('function', onSort_)) onSort_(valueNew); }, [onSort_]); const stylesOther = {}; if (sticky) { stylesOther[stickyPosition === 'left' ? 'left' : 'right'] = offset + (stickyOffset !== undefined ? stickyOffset : 0); if (position === 'head') stylesOther.zIndex = '17'; } return ((0, jsx_runtime_1.jsxs)(Component, Object.assign({ ref: (item) => { if (ref) { if ((0, utils_1.is)('function', ref)) ref(item); ref.current = item; } refs.root.current = item; setRoot(item); }, role: 'cell', className: (0, style_react_1.classNames)([ (0, utils_2.staticClassName)('TableCell', theme) && [ `amaui-TableCell-root`, `amaui-TableCell-size-${size}`, sticky && `amaui-TableCell-sticky`, stickyPosition && `amaui-TableCell-sticky-position-${stickyPosition}`, stickyActive && `amaui-TableHead-sticky-active` ], className, classes.root, position === 'head' ? classes.head : classes.body, noWeight && classes.noWeight, sticky && classes.sticky, stickyActive && classes.stickyActive ]), style: Object.assign(Object.assign({}, style), stylesOther) }, other, { children: [(0, jsx_runtime_1.jsxs)(Line, Object.assign({ gap: 0, direction: 'row', align: align, justify: justify, className: (0, style_react_1.classNames)([ (0, utils_2.staticClassName)('TableCell', theme) && [ `amaui-TableCell-value` ], classes.value, classes[`size_${size}`], sort !== undefined && classes.sort ]) }, { children: [(0, utils_1.is)('simple', children) ? ((0, jsx_runtime_1.jsx)(Type, Object.assign({ version: 'l2', tabIndex: sort !== undefined ? 0 : undefined }, TypeProps, { className: (0, style_react_1.classNames)([ (0, utils_2.staticClassName)('TableCell', theme) && [ `amaui-TableCell-value-type` ], TypeProps === null || TypeProps === void 0 ? void 0 : TypeProps.className ]) }, { children: children }))) : (children && react_1.default.cloneElement(children, { tabIndex: sort !== undefined ? 0 : undefined })), sort && ((0, jsx_runtime_1.jsx)(Tooltip_1.default, Object.assign({ name: sortedBy === 'asc' ? 'Ascending' : 'Descending' }, { children: (0, jsx_runtime_1.jsx)(IconButton, Object.assign({ size: 'small', onClick: onSort, disabled: disabled, className: (0, style_react_1.classNames)([ (0, utils_2.staticClassName)('TableCell', theme) && [ `amaui-TableCell-sort-icon-button` ], classes.sortedBy, classes[`sortedBy_${sortedBy}`] ]) }, { children: (0, jsx_runtime_1.jsx)(IconArrow, {}) })) })))] })), (0, jsx_runtime_1.jsx)(Divider, Object.assign({ color: 'inherit' }, DividerProps, { className: (0, style_react_1.classNames)([ DividerProps === null || DividerProps === void 0 ? void 0 : DividerProps.className, classes.divider ]) }))] }))); }); TableCell.displayName = 'amaui-TableCell'; exports.default = TableCell;