Group.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
  2. import { createVNode as _createVNode } from "vue";
  3. import { cloneElement } from '../_util/vnode';
  4. import Avatar from './Avatar';
  5. import Popover from '../popover';
  6. import { computed, defineComponent, watchEffect } from 'vue';
  7. import { flattenChildren, getPropsSlot } from '../_util/props-util';
  8. import useConfigInject from '../config-provider/hooks/useConfigInject';
  9. import useStyle from './style';
  10. import { useAvatarProviderContext } from './AvatarContext';
  11. export const groupProps = () => ({
  12. prefixCls: String,
  13. maxCount: Number,
  14. maxStyle: {
  15. type: Object,
  16. default: undefined
  17. },
  18. maxPopoverPlacement: {
  19. type: String,
  20. default: 'top'
  21. },
  22. maxPopoverTrigger: String,
  23. /*
  24. * Size of avatar, options: `large`, `small`, `default`
  25. * or a custom number size
  26. * */
  27. size: {
  28. type: [Number, String, Object],
  29. default: 'default'
  30. },
  31. shape: {
  32. type: String,
  33. default: 'circle'
  34. }
  35. });
  36. const Group = defineComponent({
  37. compatConfig: {
  38. MODE: 3
  39. },
  40. name: 'AAvatarGroup',
  41. inheritAttrs: false,
  42. props: groupProps(),
  43. setup(props, _ref) {
  44. let {
  45. slots,
  46. attrs
  47. } = _ref;
  48. const {
  49. prefixCls,
  50. direction
  51. } = useConfigInject('avatar', props);
  52. const groupPrefixCls = computed(() => `${prefixCls.value}-group`);
  53. const [wrapSSR, hashId] = useStyle(prefixCls);
  54. watchEffect(() => {
  55. const context = {
  56. size: props.size,
  57. shape: props.shape
  58. };
  59. useAvatarProviderContext(context);
  60. });
  61. return () => {
  62. const {
  63. maxPopoverPlacement = 'top',
  64. maxCount,
  65. maxStyle,
  66. maxPopoverTrigger = 'hover',
  67. shape
  68. } = props;
  69. const cls = {
  70. [groupPrefixCls.value]: true,
  71. [`${groupPrefixCls.value}-rtl`]: direction.value === 'rtl',
  72. [`${attrs.class}`]: !!attrs.class,
  73. [hashId.value]: true
  74. };
  75. const children = getPropsSlot(slots, props);
  76. const childrenWithProps = flattenChildren(children).map((child, index) => cloneElement(child, {
  77. key: `avatar-key-${index}`
  78. }));
  79. const numOfChildren = childrenWithProps.length;
  80. if (maxCount && maxCount < numOfChildren) {
  81. const childrenShow = childrenWithProps.slice(0, maxCount);
  82. const childrenHidden = childrenWithProps.slice(maxCount, numOfChildren);
  83. childrenShow.push(_createVNode(Popover, {
  84. "key": "avatar-popover-key",
  85. "content": childrenHidden,
  86. "trigger": maxPopoverTrigger,
  87. "placement": maxPopoverPlacement,
  88. "overlayClassName": `${groupPrefixCls.value}-popover`
  89. }, {
  90. default: () => [_createVNode(Avatar, {
  91. "style": maxStyle,
  92. "shape": shape
  93. }, {
  94. default: () => [`+${numOfChildren - maxCount}`]
  95. })]
  96. }));
  97. return wrapSSR(_createVNode("div", _objectSpread(_objectSpread({}, attrs), {}, {
  98. "class": cls,
  99. "style": attrs.style
  100. }), [childrenShow]));
  101. }
  102. return wrapSSR(_createVNode("div", _objectSpread(_objectSpread({}, attrs), {}, {
  103. "class": cls,
  104. "style": attrs.style
  105. }), [childrenWithProps]));
  106. };
  107. }
  108. });
  109. export default Group;