forked from vueComponent/ant-design-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGroup.tsx
104 lines (92 loc) · 2.77 KB
/
Group.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import toArray from 'lodash/toArray';
import { cloneElement } from '../_util/vnode';
import { defaultConfigProvider } from '../config-provider';
import Avatar, { avatarProps } from './Avatar';
import Popover from '../popover';
import {
computed,
defineComponent,
inject,
provide,
PropType,
ExtractPropTypes,
CSSProperties,
} from 'vue';
import PropTypes from '../_util/vue-types';
import { getPropsSlot } from '../_util/props-util';
import { tuple } from '../_util/type';
const groupProps = {
prefixCls: PropTypes.string,
maxCount: PropTypes.number,
maxStyle: {
type: Object as PropType<CSSProperties>,
default: () => ({} as CSSProperties),
},
maxPopoverPlacement: PropTypes.oneOf(tuple('top', 'bottom')).def('top'),
/*
* Size of avatar, options: `large`, `small`, `default`
* or a custom number size
* */
size: avatarProps.size,
};
export type AvatarGroupProps = Partial<ExtractPropTypes<typeof groupProps>>;
const Group = defineComponent({
name: 'AAvatarGroup',
props: groupProps,
inheritAttrs: false,
setup(props, { slots, attrs }) {
const configProvider = inject('configProvider', defaultConfigProvider);
provide(
'SizeProvider',
computed(() => props.size || configProvider.componentSize),
);
return () => {
const {
prefixCls: customizePrefixCls,
maxPopoverPlacement = 'top',
maxCount,
maxStyle,
} = props;
const { getPrefixCls } = configProvider;
const prefixCls = getPrefixCls('avatar-group', customizePrefixCls);
const className = attrs.class as string;
const cls = {
[prefixCls]: true,
[className]: className !== undefined,
};
const children = getPropsSlot(slots, props);
const childrenWithProps = toArray(children).map((child, index) =>
cloneElement(child, {
key: `avatar-key-${index}`,
}),
);
const numOfChildren = childrenWithProps.length;
if (maxCount && maxCount < numOfChildren) {
const childrenShow = childrenWithProps.slice(0, maxCount);
const childrenHidden = childrenWithProps.slice(maxCount, numOfChildren);
childrenShow.push(
<Popover
key="avatar-popover-key"
content={childrenHidden}
trigger="hover"
placement={maxPopoverPlacement}
overlayClassName={`${prefixCls}-popover`}
>
<Avatar style={maxStyle}>{`+${numOfChildren - maxCount}`}</Avatar>
</Popover>,
);
return (
<div class={cls} style={attrs.style}>
{childrenShow}
</div>
);
}
return (
<div class={cls} style={attrs.style}>
{childrenWithProps}
</div>
);
};
},
});
export default Group;