UNPKG

@amaui/ui-react

Version:
89 lines 4.62 kB
import _extends from "@babel/runtime/helpers/extends"; import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; const _excluded = ["tonal", "color", "size", "total", "max", "disabled", "AdditionalAvatar", "AdditionalAvatarProps", "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 AvatarElement from '../Avatar'; import { staticClassName } from '../utils'; const useStyle = styleMethod(theme => ({ root: {}, disabled: { pointerEvents: 'none', opacity: theme.palette.visual_contrast.default.opacity.disabled, filter: 'grayscale(1)' }, item: { outline: `2px solid ${theme.palette.background.default.primary}`, '&:not(:last-child)': { marginInlineStart: `-0.24em` } } }), { name: 'amaui-AvatarGroup' }); const AvatarGroup = /*#__PURE__*/React.forwardRef((props_, ref) => { const theme = useAmauiTheme(); const props = React.useMemo(() => _objectSpread(_objectSpread(_objectSpread({}, theme?.ui?.elements?.all?.props?.default), theme?.ui?.elements?.amauiAvatarGroup?.props?.default), props_), [props_]); const Line = React.useMemo(() => theme?.elements?.Line || LineElement, [theme]); const Avatar = React.useMemo(() => theme?.elements?.Avatar || AvatarElement, [theme]); const { tonal = true, color = 'primary', size = 'regular', total, max, disabled, AdditionalAvatar, AdditionalAvatarProps = {}, Component = 'div', className, style, children: children_ } = props, other = _objectWithoutProperties(props, _excluded); const { classes } = useStyle(); const styles = { root: {}, item: {} }; const children = React.Children.toArray(children_).slice(0, max || children_.length).reverse(); if (size !== undefined) { if (size === 'small') styles.item.outlineWidth = 2;else if (size === 'regular') styles.item.outlineWidth = 3;else if (size === 'large') styles.item.outlineWidth = 4;else styles.item.outlineWidth = Math.ceil(size / 20); } other.style = styles.item; 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 (!AdditionalAvatarProps.TypeProps) AdditionalAvatarProps.TypeProps = {}; AdditionalAvatarProps.TypeProps.size = '0.44em'; children.unshift(AdditionalAvatar || /*#__PURE__*/React.createElement(Avatar, _extends({ tonal: tonal, color: "neutral" }, other, AdditionalAvatarProps, { noIconRootFontSize: true }), value)); } if (AdditionalAvatarProps?.size !== undefined) styles.root.fontSize = AdditionalAvatarProps.size; return /*#__PURE__*/React.createElement(Line, { ref: ref, gap: 0, direction: "row-reverse", Component: Component, className: classNames([staticClassName('AvatarGroup', theme) && ['amaui-AvatarGroup-root', `amaui-AvatarGroup-size-${size}`], className, classes.root, disabled && classes.disabled]), style: _objectSpread(_objectSpread({}, style), styles.root) }, children.map((item, index) => /*#__PURE__*/React.cloneElement(item, _objectSpread(_objectSpread({ key: index, className: classNames([item?.props?.className, classes.item]), tonal: item.props.tonal || tonal, color: item.props.color || color, size: item.props.size || size }, other), item.props)))); }); AvatarGroup.displayName = 'amaui-AvatarGroup'; export default AvatarGroup;