@onesy/ui-react
Version:
UI for React
115 lines • 4.62 kB
JavaScript
import _extends from "@babel/runtime/helpers/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
const _excluded = ["size", "gap", "align", "justify", "direction", "wrap", "total", "max", "showAllDefault", "moreShowAll", "AdditionalChip", "AdditionalChipProps", "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 { classNames, style as styleMethod, useOnesyTheme } from '@onesy/style-react';
import LineElement from '../Line';
import ChipElement from '../Chip/Chip';
import { staticClassName } from '../utils';
const useStyle = styleMethod(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 = /*#__PURE__*/React.forwardRef((props_, ref) => {
const theme = useOnesyTheme();
const props = React.useMemo(() => _objectSpread(_objectSpread(_objectSpread({}, theme?.ui?.elements?.all?.props?.default), theme?.ui?.elements?.onesyChips?.props?.default), props_), [props_]);
const Line = React.useMemo(() => theme?.elements?.Line || LineElement, [theme]);
const Chip = React.useMemo(() => theme?.elements?.Chip || ChipElement, [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 = _objectWithoutProperties(props, _excluded);
const {
classes
} = useStyle();
const [showAll, setShowAll] = React.useState(showAllDefault !== undefined ? showAllDefault : false);
const onClickMore = React.useCallback(() => {
setShowAll(true);
}, []);
let children = React.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 || /*#__PURE__*/React.createElement(Chip, _extends({
color: "default"
}, other, AdditionalChipProps, {
onClick: moreShowAll ? onClickMore : undefined
}), value));
}
}
return /*#__PURE__*/React.createElement(Line, {
ref: ref,
gap: gap,
justify: justify,
align: align,
direction: direction,
wrap: wrap,
Component: Component,
className: classNames([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
}, React.Children.toArray(children).map((item, index) => /*#__PURE__*/React.cloneElement(item, _objectSpread(_objectSpread({
key: index,
size
}, other), item.props))));
});
Chips.displayName = 'onesy-Chips';
export default Chips;