UNPKG

@onesy/ui-react

Version:
105 lines (104 loc) 5.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 style_react_1 = require("@onesy/style-react"); const Line_1 = __importDefault(require("../Line")); const Chip_1 = __importDefault(require("../Chip/Chip")); const utils_1 = require("../utils"); const useStyle = (0, style_react_1.style)(theme => ({ root: { width: '100%', padding: '1px', '&:not($wrap)': { overflowX: 'auto', '&$small': { paddingRight: theme.methods.space.value(0.75, 'px') }, '&$regular': { paddingRight: theme.methods.space.value(1, 'px') }, '&$large': { paddingRight: theme.methods.space.value(1.25, 'px') }, } }, size_small: { // columnGap: '6px' }, size_regular: { // columnGap: '8px' }, size_large: { // columnGap: '10px' }, wrap: { flexWrap: 'wrap' }, wrap_small: { // rowGap: '12px' }, wrap_regular: { // rowGap: '16px' }, wrap_large: { // rowGap: '20px' } }), { name: 'onesy-Chips' }); const Chips = react_1.default.forwardRef((props_, ref) => { const theme = (0, style_react_1.useOnesyTheme)(); 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.onesyChips) === 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 Chip = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.Chip) || Chip_1.default; }, [theme]); const { size = 'regular', gap = 1, align = 'flex-start', justify = 'flex-start', direction = 'row', wrap, total, max, showAllDefault, moreShowAll = true, AdditionalChip, AdditionalChipProps = {}, Component = 'div', className, style, children: children_ } = props, other = __rest(props, ["size", "gap", "align", "justify", "direction", "wrap", "total", "max", "showAllDefault", "moreShowAll", "AdditionalChip", "AdditionalChipProps", "Component", "className", "style", "children"]); const { classes } = useStyle(); const [showAll, setShowAll] = react_1.default.useState(showAllDefault !== undefined ? showAllDefault : false); const onClickMore = react_1.default.useCallback(() => { setShowAll(true); }, []); let children = react_1.default.Children.toArray(children_); if (!showAll) { children = children.slice(0, max || children.length); if ((total !== undefined && total - children.length >= 1) || max < children.length) { let value; if ((total !== undefined && total - children.length >= 1)) value = `+${total - children.length}`; else value = `+${Math.abs(children.length - max)}`; if (!AdditionalChipProps.TypeProps) AdditionalChipProps.TypeProps = {}; AdditionalChipProps.TypeProps.size = '0.44em'; children.push(AdditionalChip || (0, jsx_runtime_1.jsx)(Chip, Object.assign({ color: 'default' }, other, AdditionalChipProps, { onClick: moreShowAll ? onClickMore : undefined }, { children: value }))); } } return ((0, jsx_runtime_1.jsx)(Line, Object.assign({ ref: ref, gap: gap, justify: justify, align: align, direction: direction, wrap: wrap, Component: Component, className: (0, style_react_1.classNames)([ (0, utils_1.staticClassName)('Chips', theme) && [ 'onesy-Chips-root', `onesy-Chips-size-${size}` ], className, classes.root, classes[`size_${size}`], wrap === 'wrap' && [ classes.wrap, classes[`wrap_${size}`] ] ]), style: style }, { children: react_1.default.Children.toArray(children).map((item, index) => react_1.default.cloneElement(item, Object.assign(Object.assign({ key: index, size }, other), item.props))) }))); }); Chips.displayName = 'onesy-Chips'; exports.default = Chips;