UNPKG

@onesy/ui-react

Version:
320 lines (316 loc) 11.3 kB
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; const _excluded = ["ref", "tonal", "color", "size", "position", "align", "justify", "noWeight", "sort", "sortedBy", "sortedByDefault", "onSort", "sticky", "stickyPosition", "stickyOffset", "timeout", "disabled", "IconArrow", "IconButtonSortProps", "TypeProps", "DividerProps", "Component", "className", "style", "children"]; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } import React from 'react'; import { clamp, is } from '@onesy/utils'; import { classNames, style as styleMethod, useOnesyTheme } from '@onesy/style-react'; import IconMaterialArrowDownwardAlt from '@onesy/icons-material-rounded-react/IconMaterialArrowDownwardAltW100'; import LineElement from '../Line'; import IconButtonElement from '../IconButton'; import TypeElement from '../Type'; import DividerElement from '../Divider'; import { staticClassName } from '../utils'; import Tooltip from '../Tooltip'; import useResize from '../useResize'; import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; 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: { '&.onesy-Divider-root': { position: 'absolute', left: '0px', bottom: '0px', margin: '0px' } }, noWeight: { fontWeight: '400' }, sticky: { position: 'sticky', zIndex: '14' }, sticky_left: { '&::after': { content: "''", position: 'absolute', top: '0', right: '-24px', bottom: '0', width: '24px', transition: theme.methods.transitions.make('box-shadow'), pointerEvents: 'none' } }, sticky_right: { '&::after': { content: "''", position: 'absolute', top: '0', left: '-24px', bottom: '0', width: '24px', transition: theme.methods.transitions.make('box-shadow'), pointerEvents: 'none' } }, stickyActive_left: { '&::after': { boxShadow: `inset 11px 0 7px -7px ${theme.palette.light ? 'rgba(0, 0, 0, 0.04)' : 'rgba(255, 255, 255, 0.14)'}` } }, stickyActive_right: { '&::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: { '& .onesy-Icon-root': { transition: theme.methods.transitions.make('transform') } }, sortedBy_asc: { '& .onesy-Icon-root': { transform: 'rotate(180deg)' } } }), { name: 'onesy-TableCell' }); const TableCell = props_ => { const theme = useOnesyTheme(); const l = theme.l; const props = _objectSpread(_objectSpread(_objectSpread({}, theme?.ui?.elements?.all?.props?.default), theme?.ui?.elements?.onesyTableCell?.props?.default), props_); const Line = theme?.elements?.Line || LineElement; const Type = theme?.elements?.Type || TypeElement; const IconButton = theme?.elements?.IconButton || IconButtonElement; const Divider = theme?.elements?.Divider || DividerElement; const { ref, tonal = true, color, size = 'regular', position = 'body', align = 'center', justify = 'flex-end', noWeight, sort, sortedBy: sortedBy_, sortedByDefault, onSort: onSort_, sticky, stickyPosition = 'left', stickyOffset, timeout = 150, disabled, IconArrow = IconMaterialArrowDownwardAlt, IconButtonSortProps, TypeProps, DividerProps, Component = props.position === 'head' ? 'th' : 'td', className, style, children } = props, other = _objectWithoutProperties(props, _excluded); const { classes } = useStyle(); const windowWidth = useResize(); 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), offset: React.useRef(null), observer: React.useRef(null), sticky: React.useRef(sticky) }; refs.root.current = root; refs.sticky.current = sticky; const init = () => { 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_0 => item_0.classList.contains('onesy-TableCell-sticky')); const offset_ = elementsOffset.reduce((result, item_1) => { result += item_1.clientWidth; return result; }, 0); setOffset(clamp(offset_, 0)); } } }, 140); }; React.useEffect(() => { // init init(); }, [windowWidth]); React.useEffect(() => { if (sortedBy !== sortedBy_) setSortedBy(sortedBy_); }, [sortedBy_]); const onStickyMove = () => { const offsetNew = refs.root.current.offsetLeft; setStickyActive(offsetNew < refs.offset.current); }; const onStickyInit = () => { if (refs.sticky.current && refs.root.current) { refs.root.current.style.position = 'unset'; refs.offset.current = refs.root.current.offsetLeft; refs.root.current.style.position = 'sticky'; onStickyMove(); } }; const onObserve = () => { const element = refs.root.current?.closest('table'); // clean up if (refs.observer.current) refs.observer.current.disconnect(); if (!element) return; // init const config = { childList: true, subtree: true, attributes: true, attributeFilter: ['data-*'], characterData: true }; const method = mutations => { for (const mutation of mutations) { switch (mutation.type) { case 'childList': case 'attributes': case 'characterData': onStickyInit(); break; } } }; refs.observer.current = new MutationObserver(method); refs.observer.current.observe(element, config); return refs.observer.current; }; React.useEffect(() => { if (root && sticky) { onStickyInit(); setTimeout(() => { onStickyInit(); }, 250); // observer onObserve(); // scroll const parentOverflow = window.document.querySelector('.onesy-Table-wrapper'); if (parentOverflow) parentOverflow.addEventListener('scroll', onStickyMove, { passive: false }); return () => { if (parentOverflow) parentOverflow.removeEventListener('scroll', onStickyMove); }; } }, [timeout, windowWidth, sticky, stickyPosition, root]); const onSort = () => { let valueNew; setSortedBy(item_2 => { valueNew = item_2 === 'asc' ? 'desc' : 'asc'; return valueNew; }); if (is('function', onSort_)) onSort_(valueNew); }; const stylesOther = {}; if (sticky) { stylesOther[stickyPosition === 'left' ? 'left' : 'right'] = offset + (stickyOffset !== undefined ? stickyOffset : 0); if (position === 'head') stylesOther.zIndex = '17'; } return /*#__PURE__*/_jsxs(Component, _objectSpread(_objectSpread({ ref: item_3 => { if (ref) { if (is('function', ref)) ref(item_3); ref.current = item_3; } refs.root.current = item_3; setRoot(item_3); }, role: "cell", className: classNames([staticClassName('TableCell', theme) && [`onesy-TableCell-root`, `onesy-TableCell-size-${size}`, sticky && `onesy-TableCell-sticky`, stickyPosition && `onesy-TableCell-sticky-position-${stickyPosition}`, stickyActive && `onesy-TableHead-sticky-active`], className, classes.root, position === 'head' ? classes.head : classes.body, noWeight && classes.noWeight, sticky && [classes.sticky, classes[`sticky_${stickyPosition}`]], stickyActive && classes[`stickyActive_${stickyPosition}`]]), style: _objectSpread(_objectSpread({}, style), stylesOther) }, other), {}, { children: [/*#__PURE__*/_jsxs(Line, { gap: 0, direction: "row", align: align, justify: justify, className: classNames([staticClassName('TableCell', theme) && [`onesy-TableCell-value`], classes.value, classes[`size_${size}`], sort !== undefined && classes.sort]), children: [is('simple', children) ? /*#__PURE__*/_jsx(Type, _objectSpread(_objectSpread({ version: "l2", tabIndex: sort !== undefined ? 0 : undefined }, TypeProps), {}, { className: classNames([staticClassName('TableCell', theme) && [`onesy-TableCell-value-type`], TypeProps?.className]), children: children })) : children && /*#__PURE__*/React.cloneElement(children, { tabIndex: sort !== undefined ? 0 : undefined }), sort && /*#__PURE__*/_jsx(Tooltip, { name: sortedBy === 'asc' ? l('Ascending') : l('Descending'), children: /*#__PURE__*/_jsx(IconButton, { size: "small", onClick: onSort, disabled: disabled, className: classNames([staticClassName('TableCell', theme) && [`onesy-TableCell-sort-icon-button`], classes.sortedBy, classes[`sortedBy_${sortedBy}`]]), children: /*#__PURE__*/_jsx(IconArrow, {}) }) })] }), /*#__PURE__*/_jsx(Divider, _objectSpread(_objectSpread({ color: "inherit" }, DividerProps), {}, { className: classNames([DividerProps?.className, classes.divider]) }))] })); }; TableCell.displayName = 'onesy-TableCell'; export default TableCell;