@amaui/ui-react
Version:
UI for React
125 lines (124 loc) • 7.28 kB
JavaScript
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 Surface_1 = __importDefault(require("../Surface"));
const utils_2 = require("../utils");
const TableRow_1 = __importDefault(require("../TableRow"));
const LinearProgress_1 = __importDefault(require("../LinearProgress"));
const useStyle = (0, style_react_1.style)(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 = 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.amauiTableHead) === null || _g === void 0 ? void 0 : _g.props) === null || _h === void 0 ? void 0 : _h.default), props_)); }, [props_]);
const Surface = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.Surface) || Surface_1.default; }, [theme]);
const TableRow = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.TableRow) || TableRow_1.default; }, [theme]);
const LinearProgress = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.LinearProgress) || LinearProgress_1.default; }, [theme]);
const { tonal = true, color = 'themed', size = 'regular', sticky, stickyOffset = 0, loading, LinearGradientProps, TableRowLoaderProps, Component = 'thead', className, style, children } = props, other = __rest(props, ["tonal", "color", "size", "sticky", "stickyOffset", "loading", "LinearGradientProps", "TableRowLoaderProps", "Component", "className", "style", "children"]);
const { classes } = useStyle();
const [root, setRoot] = react_1.default.useState();
const [stickyActive, setStickyActive] = react_1.default.useState(false);
const refs = {
root: react_1.default.useRef(undefined)
};
const styleOther = {};
if (sticky && stickyOffset !== undefined)
styleOther.top = stickyOffset;
react_1.default.useEffect(() => {
if (sticky) {
if (root) {
const parentOverflow = (0, utils_2.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 ((0, jsx_runtime_1.jsxs)(Surface, Object.assign({ ref: (item) => {
if (ref) {
if ((0, utils_1.is)('function', ref))
ref(item);
ref.current = item;
}
refs.root.current = item;
setRoot(item);
}, tonal: tonal, color: color, role: 'rowgroup', Component: Component, className: (0, style_react_1.classNames)([
(0, utils_2.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: Object.assign(Object.assign({}, style), styleOther) }, other, { children: [react_1.default.Children.toArray(children).map((item, index) => (react_1.default.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 && ((0, jsx_runtime_1.jsx)(TableRow, Object.assign({}, TableRowLoaderProps, { className: (0, style_react_1.classNames)([
(0, utils_2.staticClassName)('TableHead', theme) && [
`amaui-TableHead-row-loader`
],
TableRowLoaderProps === null || TableRowLoaderProps === void 0 ? void 0 : TableRowLoaderProps.className,
classes.rowLoader
]) }, { children: (0, jsx_runtime_1.jsx)(LinearProgress, Object.assign({ tonal: true, color: 'primary', colspan: '100%', Component: 'td' }, LinearGradientProps, { className: (0, style_react_1.classNames)([
(0, utils_2.staticClassName)('TableHead', theme) && [
`amaui-TableHead-td-loader`
],
LinearGradientProps === null || LinearGradientProps === void 0 ? void 0 : LinearGradientProps.className,
classes.tdLoader
]) })) })))] })));
});
TableHead.displayName = 'amaui-TableHead';
exports.default = TableHead;
;