@onesy/ui-react
Version:
UI for React
126 lines (125 loc) • 6.35 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _react = _interopRequireDefault(require("react"));
var _styleReact = require("@onesy/style-react");
var _Line = _interopRequireDefault(require("../Line"));
var _Avatar = _interopRequireDefault(require("../Avatar"));
var _utils = require("../utils");
var _jsxRuntime = require("react/jsx-runtime");
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) { (0, _defineProperty2.default)(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; }
const useStyle = (0, _styleReact.style)(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_ => {
var _theme$ui, _theme$ui2, _theme$elements, _theme$elements2;
const theme = (0, _styleReact.useOnesyTheme)();
const props = _objectSpread(_objectSpread(_objectSpread({}, theme === null || theme === void 0 || (_theme$ui = theme.ui) === null || _theme$ui === void 0 || (_theme$ui = _theme$ui.elements) === null || _theme$ui === void 0 || (_theme$ui = _theme$ui.all) === null || _theme$ui === void 0 || (_theme$ui = _theme$ui.props) === null || _theme$ui === void 0 ? void 0 : _theme$ui.default), theme === null || theme === void 0 || (_theme$ui2 = theme.ui) === null || _theme$ui2 === void 0 || (_theme$ui2 = _theme$ui2.elements) === null || _theme$ui2 === void 0 || (_theme$ui2 = _theme$ui2.onesyAvatarGroup) === null || _theme$ui2 === void 0 || (_theme$ui2 = _theme$ui2.props) === null || _theme$ui2 === void 0 ? void 0 : _theme$ui2.default), props_);
const Line = (theme === null || theme === void 0 || (_theme$elements = theme.elements) === null || _theme$elements === void 0 ? void 0 : _theme$elements.Line) || _Line.default;
const Avatar = (theme === null || theme === void 0 || (_theme$elements2 = theme.elements) === null || _theme$elements2 === void 0 ? void 0 : _theme$elements2.Avatar) || _Avatar.default;
const {
tonal: t0,
color: t1,
size: t2,
total,
max,
disabled,
AdditionalAvatar,
AdditionalAvatarProps: t3,
Component: t4,
className,
style,
children: children_
} = props,
other = (0, _objectWithoutProperties2.default)(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.default.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__*/(0, _jsxRuntime.jsx)(Avatar, _objectSpread(_objectSpread(_objectSpread({
tonal: tonal,
color: "neutral"
}, other), AdditionalAvatarProps), {}, {
noIconRootFontSize: true,
children: value
})));
}
if ((AdditionalAvatarProps === null || AdditionalAvatarProps === void 0 ? void 0 : AdditionalAvatarProps.size) !== undefined) {
styles.root.fontSize = AdditionalAvatarProps.size;
}
return /*#__PURE__*/(0, _jsxRuntime.jsx)(Line, {
gap: 0,
direction: "row-reverse",
Component: Component,
className: (0, _styleReact.classNames)([(0, _utils.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) => {
var _item$props;
return /*#__PURE__*/_react.default.cloneElement(item, _objectSpread(_objectSpread({
key: index,
className: (0, _styleReact.classNames)([item === null || item === void 0 || (_item$props = item.props) === null || _item$props === void 0 ? void 0 : _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';
var _default = exports.default = AvatarGroup;