@amaui/ui-react
Version:
UI for React
115 lines • 4.78 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", "onMoreShowAll", "AdditionalChip", "AdditionalChipProps", "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 { classNames, style as styleMethod, useAmauiTheme } from '@amaui/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: 'amaui-Chips'
});
const Chips = /*#__PURE__*/React.forwardRef((props_, ref) => {
const theme = useAmauiTheme();
const props = React.useMemo(() => _objectSpread(_objectSpread(_objectSpread({}, theme?.ui?.elements?.all?.props?.default), theme?.ui?.elements?.amauiChips?.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,
onMoreShowAll = 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: onMoreShowAll ? 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) && ['amaui-Chips-root', `amaui-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 = 'amaui-Chips';
export default Chips;