UNPKG

@amaui/ui-react

Version:
150 lines (149 loc) 8.59 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 IconMaterialNavigateNextW100_1 = __importDefault(require("@amaui/icons-material-rounded-react/IconMaterialNavigateNextW100")); const IconMaterialMoreHorizW100_1 = __importDefault(require("@amaui/icons-material-rounded-react/IconMaterialMoreHorizW100")); const Line_1 = __importDefault(require("../Line")); const Type_1 = __importDefault(require("../Type")); const IconButton_1 = __importDefault(require("../IconButton")); const useMediaQuery_1 = __importDefault(require("../useMediaQuery")); const utils_2 = require("../utils"); const useStyle = (0, style_react_1.style)(theme => ({ root: { margin: 0, padding: 0 }, expandMore: { '&.amaui-IconButton-root': { padding: '0', width: 'unset', height: '100%', borderRadius: theme.methods.shape.radius.value(0.25, 'px') } } }), { name: 'amaui-Breadcrumbs' }); const Breadcrumbs = 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.amauiBreadcrumbs) === 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 { max: max_, separator = ((0, jsx_runtime_1.jsx)(IconMaterialNavigateNextW100_1.default, { size: 'small' })), renderMore, IconMore = IconMaterialMoreHorizW100_1.default, Component = 'nav', MoreProps, className, children: children_ } = props, other = __rest(props, ["max", "separator", "renderMore", "IconMore", "Component", "MoreProps", "className", "children"]); const { classes } = useStyle(); const refs = { root: react_1.default.useRef(undefined) }; const keys = react_1.default.useMemo(() => { const result = []; const items = [max_]; items.forEach(item => { if ((0, utils_1.is)('object', item)) Object.keys(item).filter(key => theme.breakpoints.media[key]).forEach(key => result.push(key)); }); return (0, utils_1.unique)(result); }, [max_]); const breakpoints = {}; keys.forEach(key => { breakpoints[key] = (0, useMediaQuery_1.default)(theme.breakpoints.media[key], { element: refs.root.current }); }); let max = (0, utils_2.valueBreakpoints)(max_, undefined, breakpoints, theme); max = (0, utils_1.clamp)(max, 2); const onExpandMore = () => { setChildren(resolve(children_, false)); }; const resolve = (items_ = children_, maxValue = max) => { let items = react_1.default.Children.toArray(items_).filter(Boolean); if (maxValue !== undefined && maxValue >= 2 && maxValue < items.length) { const start = []; const end = []; for (let i = 1; i <= maxValue; i++) { // start if (i % 2 > 0) start.push(items[Math.floor(i / 2)]); // end else end.unshift(items[items.length - (i / 2)]); } items = [ ...start, (0, utils_1.is)('function', renderMore) ? renderMore(onExpandMore) : (0, jsx_runtime_1.jsx)(IconButton, Object.assign({ color: 'default', size: 'small', onClick: onExpandMore, "aria-label": 'Expand more path' }, MoreProps, { className: (0, style_react_1.classNames)([ (0, utils_2.staticClassName)('Breadcrumbs', theme) && [ 'amaui-Breadcrumbs-expand-more' ], MoreProps === null || MoreProps === void 0 ? void 0 : MoreProps.className, classes.expandMore ]) }, { children: (0, jsx_runtime_1.jsx)(IconMore, { size: 'small' }) })), ...end ]; } return items; }; const [init, setInit] = react_1.default.useState(false); const [children, setChildren] = react_1.default.useState(resolve); react_1.default.useEffect(() => { setInit(true); }, []); react_1.default.useEffect(() => { if (init) setChildren(resolve(children_)); }, [children_, max]); const Separator = (0, utils_1.is)('simple', separator) ? ((0, jsx_runtime_1.jsx)(Type, Object.assign({ version: 'b2', "aria-hidden": true, Component: 'span' }, { children: separator }))) : react_1.default.cloneElement(separator, { 'aria-hidden': true }); const childrenToUse = children.flatMap((item, index) => [item, index !== children.length - 1 && Separator].filter(Boolean)); return ((0, jsx_runtime_1.jsx)(Line, Object.assign({ ref: item => { if (ref) { if ((0, utils_1.is)('function', ref)) ref(item); else ref.current = item; } refs.root.current = item; }, gap: 1, direction: 'row', align: 'center', wrap: 'wrap', "aria-label": 'Breadcrumb', Component: Component }, other, { className: (0, style_react_1.classNames)([ (0, utils_2.staticClassName)('Breadcrumbs', theme) && [ 'amaui-Breadcrumbs-root' ], className, classes.root ]) }, { children: childrenToUse.map((item, index) => { var _a; const itemProps = {}; if (index === childrenToUse.length - 1) { itemProps['aria-current'] = 'page'; } if ((0, utils_1.is)('simple', item)) { return ((0, jsx_runtime_1.jsx)(Type, Object.assign({ version: 'b2', Component: 'span' }, itemProps, { className: (0, style_react_1.classNames)([ (0, utils_2.staticClassName)('Breadcrumbs', theme) && [ 'amaui-Breadcrumbs-item' ], classes.item ]) }, { children: item }), index)); } return (react_1.default.cloneElement(item, Object.assign({ key: index, className: (0, style_react_1.classNames)([ (0, utils_2.staticClassName)('Breadcrumbs', theme) && [ 'amaui-Breadcrumbs-item' ], (_a = item === null || item === void 0 ? void 0 : item.props) === null || _a === void 0 ? void 0 : _a.className, classes.item ]) }, itemProps))); }) }))); }); Breadcrumbs.displayName = 'amaui-Breadcrumbs'; exports.default = Breadcrumbs;