@spaced-out/ui-design-system
Version:
Sense UI components library
177 lines (172 loc) • 6.25 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.AvatarGroup = void 0;
var React = _interopRequireWildcard(require("react"));
var _color = require("../../styles/variables/_color");
var _space = require("../../styles/variables/_space");
var _classify = _interopRequireDefault(require("../../utils/classify"));
var _qa = require("../../utils/qa");
var _Avatar = require("../Avatar");
var _Tooltip = require("../Tooltip");
var _AvatarGroupModule = _interopRequireDefault(require("./AvatarGroup.module.css"));
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
const COLOR_SEQUENCE = ['blue', 'green', 'orange', 'red', 'gray'];
// Define the expected props for avatar elements
// Type for an avatar element that can be cloned
const AvatarGroup = exports.AvatarGroup = /*#__PURE__*/React.forwardRef((_ref, ref) => {
let {
children,
size = 'medium',
borderColor = _color.colorBackgroundTertiary,
maxTooltipLines = 7,
placement = 'top',
maxAvatars = 5,
tooltipElevation,
testId
} = _ref;
const childAvatarCount = React.Children.count(children);
const leftOverlap = {
small: _space.spaceXXSmall,
medium: `${parseInt(_space.spaceSmall) / 2}px`,
large: _space.spaceXSmall,
extraLarge: _space.spaceSmall
};
let colorIndex = -1;
const avatarInGroup = (child, index, color) => {
const avatarProps = child.props;
const {
classNames,
text
} = avatarProps;
const {
wrapper,
...restClassNames
} = classNames || {};
const avatar = /*#__PURE__*/React.cloneElement(child, {
size,
classNames: {
wrapper: (0, _classify.default)(_AvatarGroupModule.default.avatarInGroup, wrapper),
...restClassNames
},
style: {
borderColor,
marginLeft: `-${index !== 0 ? leftOverlap[size] : 0}`
},
color,
testId: (0, _qa.generateTestId)({
base: testId,
slot: 'avatar',
index: index.toString()
})
});
return text ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_Tooltip.Tooltip, {
body: text,
placement: placement,
elevation: tooltipElevation,
testId: (0, _qa.generateTestId)({
base: testId,
slot: 'avatar-tooltip',
index: index.toString()
}),
children: avatar
}) : avatar;
};
const childrenArray = React.Children.toArray(children);
const totalAvatarCount = childrenArray.length;
const plusAvatar = () => {
const nameList = [];
for (let i = maxAvatars - 1; i < totalAvatarCount; i++) {
const child = childrenArray[i];
// Type guard to ensure child is a ReactElement
if (/*#__PURE__*/React.isValidElement(child)) {
const childElement = child;
const childProps = childElement.props;
const {
text
} = childProps;
if (text) {
nameList.push(text);
}
}
}
const NameListNode = () => /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
children: nameList.map(name => /*#__PURE__*/(0, _jsxRuntime.jsxs)(React.Fragment, {
children: [name, /*#__PURE__*/(0, _jsxRuntime.jsx)("br", {})]
}, name))
});
return /*#__PURE__*/(0, _jsxRuntime.jsx)(_Tooltip.Tooltip, {
body: /*#__PURE__*/(0, _jsxRuntime.jsx)(NameListNode, {}),
bodyMaxLines: maxTooltipLines,
placement: placement,
elevation: tooltipElevation,
testId: (0, _qa.generateTestId)({
base: testId,
slot: 'plus-avatar-tooltip'
}),
children: /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
className: _AvatarGroupModule.default.plusAvatar,
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Avatar.BaseAvatar, {
size: size,
color: "gray",
text: `+${childAvatarCount - maxAvatars + 1}`,
classNames: {
wrapper: _AvatarGroupModule.default.avatarInGroup
},
style: {
borderColor,
marginLeft: `-${leftOverlap[size]}`
},
testId: (0, _qa.generateTestId)({
base: testId,
slot: 'plus-avatar'
})
})
})
});
};
const childrenWithProps = React.Children.map(children, (child, index) => {
// Type guard to ensure child is a ReactElement
if (! /*#__PURE__*/React.isValidElement(child)) {
return child;
}
const childElement = child;
const childProps = childElement.props;
const {
imageSrc
} = childProps;
if (!imageSrc) {
colorIndex++;
if (colorIndex === COLOR_SEQUENCE.length) {
colorIndex = 0;
}
}
const color = COLOR_SEQUENCE[colorIndex];
if (childAvatarCount <= maxAvatars) {
return avatarInGroup(childElement, index, color);
} else {
if (index < maxAvatars - 1) {
return avatarInGroup(childElement, index, color);
} else if (index === maxAvatars) {
return plusAvatar();
}
}
});
return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
className: (0, _classify.default)(_AvatarGroupModule.default.avatarGroupContainer, {
[_AvatarGroupModule.default.extraLargeSize]: size === 'extraLarge',
[_AvatarGroupModule.default.largeSize]: size === 'large',
[_AvatarGroupModule.default.mediumSize]: size === 'medium',
[_AvatarGroupModule.default.smallSize]: size === 'small'
}),
ref: ref,
"data-testid": (0, _qa.generateTestId)({
base: testId,
slot: 'wrapper'
}),
children: childrenWithProps
});
});