UNPKG

@amaui/ui-react

Version:
124 lines 5.7 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", "sticky", "stickyOffset", "loading", "LinearGradientProps", "TableRowLoaderProps", "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 { is } from '@amaui/utils'; import { classNames, style as styleMethod, useAmauiTheme } from '@amaui/style-react'; import SurfaceElement from '../Surface'; import { getOverflowParent, staticClassName } from '../utils'; import TableRowElement from '../TableRow'; import LinearProgressElement from '../LinearProgress'; const useStyle = styleMethod(theme => ({ root: { display: 'table-header-group', background: 'inherit', '& .amaui-TableCell-root': { whiteSpace: 'nowrap', wordBreak: 'normal' } }, sticky: { position: 'sticky', top: '0', zIndex: '114', transition: theme.methods.transitions.make('box-shadow') }, stickyActive: { boxShadow: `0px 0px 16px ${theme.palette.light ? 'rgba(0, 0, 0, 0.04)' : 'rgba(255, 255, 255, 0.14)'}` }, rowLoader: {}, tdLoader: { '&.amaui-LinearProgress-root': { position: 'absolute', inset: 0 } } }), { name: 'amaui-TableHead' }); const TableHead = /*#__PURE__*/React.forwardRef((props_, ref) => { const theme = useAmauiTheme(); const props = React.useMemo(() => _objectSpread(_objectSpread(_objectSpread({}, theme?.ui?.elements?.all?.props?.default), theme?.ui?.elements?.amauiTableHead?.props?.default), props_), [props_]); const Surface = React.useMemo(() => theme?.elements?.Surface || SurfaceElement, [theme]); const TableRow = React.useMemo(() => theme?.elements?.TableRow || TableRowElement, [theme]); const LinearProgress = React.useMemo(() => theme?.elements?.LinearProgress || LinearProgressElement, [theme]); const { tonal = true, color = 'themed', size = 'regular', sticky, stickyOffset = 0, loading, LinearGradientProps, TableRowLoaderProps, Component = 'thead', className, style, children } = props, other = _objectWithoutProperties(props, _excluded); const { classes } = useStyle(); const [root, setRoot] = React.useState(); const [stickyActive, setStickyActive] = React.useState(false); const refs = { root: React.useRef(undefined) }; const styleOther = {}; if (sticky && stickyOffset !== undefined) styleOther.top = stickyOffset; React.useEffect(() => { if (sticky) { if (root) { const parentOverflow = getOverflowParent(root); const offsetPrevious = root.offsetTop; const method = () => { const offsetNew = root.offsetTop; setStickyActive(offsetPrevious !== offsetNew); }; if (parentOverflow) parentOverflow.addEventListener('scroll', method, { passive: false }); return () => { parentOverflow.removeEventListener('scroll', method); }; } } }, [sticky, root]); return /*#__PURE__*/React.createElement(Surface, _extends({ ref: item => { if (ref) { if (is('function', ref)) ref(item); ref.current = item; } refs.root.current = item; setRoot(item); }, tonal: tonal, color: color, role: "rowgroup", Component: Component, className: classNames([staticClassName('TableHead', theme) && [`amaui-TableHead-root`, `amaui-TableHead-size-${size}`, sticky && `amaui-TableHead-sticky`, stickyActive && `amaui-TableHead-sticky-active`], className, classes.root, sticky && classes.sticky, stickyActive && classes.stickyActive]), style: _objectSpread(_objectSpread({}, style), styleOther) }, other), React.Children.toArray(children).map((item, index) => /*#__PURE__*/React.cloneElement(item, { key: index, tonal: item.props.tonal !== undefined ? item.props.tonal : tonal, color: item.props.color !== undefined ? item.props.color : color, size: item.props.size !== undefined ? item.props.size : size, position: 'head' })), loading && /*#__PURE__*/React.createElement(TableRow, _extends({}, TableRowLoaderProps, { className: classNames([staticClassName('TableHead', theme) && [`amaui-TableHead-row-loader`], TableRowLoaderProps?.className, classes.rowLoader]) }), /*#__PURE__*/React.createElement(LinearProgress, _extends({ tonal: true, color: "primary", colspan: "100%", Component: "td" }, LinearGradientProps, { className: classNames([staticClassName('TableHead', theme) && [`amaui-TableHead-td-loader`], LinearGradientProps?.className, classes.tdLoader]) })))); }); TableHead.displayName = 'amaui-TableHead'; export default TableHead;