UNPKG

@amaui/ui-react

Version:
241 lines 9.71 kB
import _extends from "@babel/runtime/helpers/extends"; import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; const _excluded = ["tonal", "color", "size", "position", "align", "justify", "noWeight", "sort", "sortedBy", "sortedByDefault", "onSort", "sticky", "stickyPosition", "stickyOffset", "disabled", "IconArrow", "IconButtonSortProps", "TypeProps", "DividerProps", "Component", "className", "style", "children"]; function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; } import React from 'react'; import { clamp, is } from '@amaui/utils'; import { classNames, style as styleMethod, useAmauiTheme } from '@amaui/style-react'; import IconMaterialArrowDownwardAlt from '@amaui/icons-material-rounded-react/IconMaterialArrowDownwardAltW100'; import LineElement from '../Line'; import IconButtonElement from '../IconButton'; import TypeElement from '../Type'; import DividerElement from '../Divider'; import { getOverflowParent, staticClassName } from '../utils'; import Tooltip from '../Tooltip'; const useStyle = styleMethod(theme => ({ root: _objectSpread(_objectSpread({ 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 = /*#__PURE__*/React.forwardRef((props_, ref) => { const theme = useAmauiTheme(); const props = React.useMemo(() => _objectSpread(_objectSpread(_objectSpread({}, theme?.ui?.elements?.all?.props?.default), theme?.ui?.elements?.amauiTableCell?.props?.default), props_), [props_]); const Line = React.useMemo(() => theme?.elements?.Line || LineElement, [theme]); const Type = React.useMemo(() => theme?.elements?.Type || TypeElement, [theme]); const IconButton = React.useMemo(() => theme?.elements?.IconButton || IconButtonElement, [theme]); const Divider = React.useMemo(() => theme?.elements?.Divider || DividerElement, [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 = IconMaterialArrowDownwardAlt, IconButtonSortProps, TypeProps, DividerProps, Component = props.position === 'head' ? 'th' : 'td', className, style, children } = props, other = _objectWithoutProperties(props, _excluded); const { classes } = useStyle(); const [root, setRoot] = React.useState(); const [stickyActive, setStickyActive] = React.useState(false); const [offset, setOffset] = React.useState(0); const [sortedBy, setSortedBy] = React.useState(sortedByDefault); const refs = { root: React.useRef(undefined) }; const init = React.useCallback(() => { setTimeout(() => { if (sticky) { const parent = refs.root.current?.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(clamp(offset_, 0)); } } }, 140); }, [sticky, stickyPosition]); React.useEffect(() => { // init init(); }, []); React.useEffect(() => { if (sortedBy !== sortedBy_) setSortedBy(sortedBy_); }, [sortedBy_]); React.useEffect(() => { if (sticky) { if (root) { const parentOverflow = 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.useCallback(() => { let valueNew; setSortedBy(item => { valueNew = item === 'asc' ? 'desc' : 'asc'; return valueNew; }); if (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 /*#__PURE__*/React.createElement(Component, _extends({ ref: item => { if (ref) { if (is('function', ref)) ref(item); ref.current = item; } refs.root.current = item; setRoot(item); }, role: "cell", className: classNames([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: _objectSpread(_objectSpread({}, style), stylesOther) }, other), /*#__PURE__*/React.createElement(Line, { gap: 0, direction: "row", align: align, justify: justify, className: classNames([staticClassName('TableCell', theme) && [`amaui-TableCell-value`], classes.value, classes[`size_${size}`], sort !== undefined && classes.sort]) }, is('simple', children) ? /*#__PURE__*/React.createElement(Type, _extends({ version: "l2", tabIndex: sort !== undefined ? 0 : undefined }, TypeProps, { className: classNames([staticClassName('TableCell', theme) && [`amaui-TableCell-value-type`], TypeProps?.className]) }), children) : children && /*#__PURE__*/React.cloneElement(children, { tabIndex: sort !== undefined ? 0 : undefined }), sort && /*#__PURE__*/React.createElement(Tooltip, { name: sortedBy === 'asc' ? 'Ascending' : 'Descending' }, /*#__PURE__*/React.createElement(IconButton, { size: "small", onClick: onSort, disabled: disabled, className: classNames([staticClassName('TableCell', theme) && [`amaui-TableCell-sort-icon-button`], classes.sortedBy, classes[`sortedBy_${sortedBy}`]]) }, /*#__PURE__*/React.createElement(IconArrow, null)))), /*#__PURE__*/React.createElement(Divider, _extends({ color: "inherit" }, DividerProps, { className: classNames([DividerProps?.className, classes.divider]) }))); }); TableCell.displayName = 'amaui-TableCell'; export default TableCell;