AvatarGroup.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. "use strict";
  2. "use client";
  3. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
  4. var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
  5. Object.defineProperty(exports, "__esModule", {
  6. value: true
  7. });
  8. exports.default = void 0;
  9. var React = _interopRequireWildcard(require("react"));
  10. var _classnames = _interopRequireDefault(require("classnames"));
  11. var _toArray = _interopRequireDefault(require("rc-util/lib/Children/toArray"));
  12. var _reactNode = require("../_util/reactNode");
  13. var _warning = require("../_util/warning");
  14. var _configProvider = require("../config-provider");
  15. var _useCSSVarCls = _interopRequireDefault(require("../config-provider/hooks/useCSSVarCls"));
  16. var _popover = _interopRequireDefault(require("../popover"));
  17. var _Avatar = _interopRequireDefault(require("./Avatar"));
  18. var _AvatarContext = _interopRequireDefault(require("./AvatarContext"));
  19. var _style = _interopRequireDefault(require("./style"));
  20. const AvatarContextProvider = props => {
  21. const {
  22. size,
  23. shape
  24. } = React.useContext(_AvatarContext.default);
  25. const avatarContextValue = React.useMemo(() => ({
  26. size: props.size || size,
  27. shape: props.shape || shape
  28. }), [props.size, props.shape, size, shape]);
  29. return /*#__PURE__*/React.createElement(_AvatarContext.default.Provider, {
  30. value: avatarContextValue
  31. }, props.children);
  32. };
  33. const AvatarGroup = props => {
  34. var _a, _b, _c, _d;
  35. const {
  36. getPrefixCls,
  37. direction
  38. } = React.useContext(_configProvider.ConfigContext);
  39. const {
  40. prefixCls: customizePrefixCls,
  41. className,
  42. rootClassName,
  43. style,
  44. maxCount,
  45. maxStyle,
  46. size,
  47. shape,
  48. maxPopoverPlacement,
  49. maxPopoverTrigger,
  50. children,
  51. max
  52. } = props;
  53. if (process.env.NODE_ENV !== 'production') {
  54. const warning = (0, _warning.devUseWarning)('Avatar.Group');
  55. [['maxCount', 'max={{ count: number }}'], ['maxStyle', 'max={{ style: CSSProperties }}'], ['maxPopoverPlacement', 'max={{ popover: PopoverProps }}'], ['maxPopoverTrigger', 'max={{ popover: PopoverProps }}']].forEach(([deprecatedName, newName]) => {
  56. warning.deprecated(!(deprecatedName in props), deprecatedName, newName);
  57. });
  58. }
  59. const prefixCls = getPrefixCls('avatar', customizePrefixCls);
  60. const groupPrefixCls = `${prefixCls}-group`;
  61. const rootCls = (0, _useCSSVarCls.default)(prefixCls);
  62. const [wrapCSSVar, hashId, cssVarCls] = (0, _style.default)(prefixCls, rootCls);
  63. const cls = (0, _classnames.default)(groupPrefixCls, {
  64. [`${groupPrefixCls}-rtl`]: direction === 'rtl'
  65. }, cssVarCls, rootCls, className, rootClassName, hashId);
  66. const childrenWithProps = (0, _toArray.default)(children).map((child, index) => (0, _reactNode.cloneElement)(child, {
  67. // eslint-disable-next-line react/no-array-index-key
  68. key: `avatar-key-${index}`
  69. }));
  70. const mergeCount = (max === null || max === void 0 ? void 0 : max.count) || maxCount;
  71. const numOfChildren = childrenWithProps.length;
  72. if (mergeCount && mergeCount < numOfChildren) {
  73. const childrenShow = childrenWithProps.slice(0, mergeCount);
  74. const childrenHidden = childrenWithProps.slice(mergeCount, numOfChildren);
  75. const mergeStyle = (max === null || max === void 0 ? void 0 : max.style) || maxStyle;
  76. const mergePopoverTrigger = ((_a = max === null || max === void 0 ? void 0 : max.popover) === null || _a === void 0 ? void 0 : _a.trigger) || maxPopoverTrigger || 'hover';
  77. const mergePopoverPlacement = ((_b = max === null || max === void 0 ? void 0 : max.popover) === null || _b === void 0 ? void 0 : _b.placement) || maxPopoverPlacement || 'top';
  78. const mergeProps = Object.assign(Object.assign({
  79. content: childrenHidden
  80. }, max === null || max === void 0 ? void 0 : max.popover), {
  81. classNames: {
  82. root: (0, _classnames.default)(`${groupPrefixCls}-popover`, (_d = (_c = max === null || max === void 0 ? void 0 : max.popover) === null || _c === void 0 ? void 0 : _c.classNames) === null || _d === void 0 ? void 0 : _d.root)
  83. },
  84. placement: mergePopoverPlacement,
  85. trigger: mergePopoverTrigger
  86. });
  87. childrenShow.push(/*#__PURE__*/React.createElement(_popover.default, Object.assign({
  88. key: "avatar-popover-key",
  89. destroyOnHidden: true
  90. }, mergeProps), /*#__PURE__*/React.createElement(_Avatar.default, {
  91. style: mergeStyle
  92. }, `+${numOfChildren - mergeCount}`)));
  93. return wrapCSSVar(/*#__PURE__*/React.createElement(AvatarContextProvider, {
  94. shape: shape,
  95. size: size
  96. }, /*#__PURE__*/React.createElement("div", {
  97. className: cls,
  98. style: style
  99. }, childrenShow)));
  100. }
  101. return wrapCSSVar(/*#__PURE__*/React.createElement(AvatarContextProvider, {
  102. shape: shape,
  103. size: size
  104. }, /*#__PURE__*/React.createElement("div", {
  105. className: cls,
  106. style: style
  107. }, childrenWithProps)));
  108. };
  109. var _default = exports.default = AvatarGroup;