@onesy/ui-react
Version:
UI for React
115 lines • 4.77 kB
JavaScript
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(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 AvatarElement from '../Avatar';
import { staticClassName } from '../utils';
import { jsx as _jsx } from "react/jsx-runtime";
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: 'onesy-AvatarGroup'
});
const AvatarGroup = props_ => {
const theme = useOnesyTheme();
const props = _objectSpread(_objectSpread(_objectSpread({}, theme?.ui?.elements?.all?.props?.default), theme?.ui?.elements?.onesyAvatarGroup?.props?.default), props_);
const Line = theme?.elements?.Line || LineElement;
const Avatar = theme?.elements?.Avatar || AvatarElement;
const {
tonal: t0,
color: t1,
size: t2,
total,
max,
disabled,
AdditionalAvatar,
AdditionalAvatarProps: t3,
Component: t4,
className,
style,
children: children_
} = props,
other = _objectWithoutProperties(props, _excluded);
const tonal = t0 === undefined ? true : t0;
const color = t1 === undefined ? "primary" : t1;
const size = t2 === undefined ? "regular" : t2;
const AdditionalAvatarProps = t3 === undefined ? {} : t3;
const Component = t4 === undefined ? "div" : t4;
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__*/_jsx(Avatar, _objectSpread(_objectSpread(_objectSpread({
tonal: tonal,
color: "neutral"
}, other), AdditionalAvatarProps), {}, {
noIconRootFontSize: true,
children: value
})));
}
if (AdditionalAvatarProps?.size !== undefined) {
styles.root.fontSize = AdditionalAvatarProps.size;
}
return /*#__PURE__*/_jsx(Line, {
gap: 0,
direction: "row-reverse",
Component: Component,
className: classNames([staticClassName("AvatarGroup", theme) && ["onesy-AvatarGroup-root", `onesy-AvatarGroup-size-${size}`], className, classes.root, disabled && classes.disabled]),
style: _objectSpread(_objectSpread({}, style), styles.root),
children: 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 = 'onesy-AvatarGroup';
export default AvatarGroup;