-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathgroup.tsx
69 lines (64 loc) · 2.01 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
import React, {
forwardRef,
Ref,
useImperativeHandle,
useRef,
useContext,
ReactElement,
} from 'react';
import { cls } from '@arco-design/mobile-utils';
import { GlobalContext } from '../context-provider';
import { AvatarProps, AvatarGroupContextParams, AvatarGroupProps, AvatarGroupRef } from './type';
export const AvatarGroupContext = React.createContext<AvatarGroupContextParams>({
isGroup: false,
shape: 'circle',
size: 'small',
});
/**
* 头像叠层
* @en Avatar group
*/
export const Group = forwardRef((props: AvatarGroupProps, ref: Ref<AvatarGroupRef | null>) => {
const {
style = {},
className = '',
shape = 'circle',
size = 'medium',
zIndexOrder = 'desc',
children,
} = props;
const { prefixCls } = useContext(GlobalContext);
const childrenArr = React.Children.toArray(children);
const { length } = childrenArr;
const domRef = useRef<HTMLDivElement | null>(null);
useImperativeHandle(ref, () => ({
dom: domRef.current,
}));
return (
<div
ref={domRef}
style={style}
className={cls(
className,
`${prefixCls}-avatar-group`,
`${prefixCls}-avatar-group-size-${size}`,
`group-${size}`,
)}
>
<AvatarGroupContext.Provider value={{ isGroup: true, shape, size }}>
{childrenArr.map((child, i) => {
const childProps: AvatarProps = ((child as ReactElement) || {}).props;
const avatarStyle = {
zIndex: zIndexOrder === 'asc' ? i + 1 : length - i,
...childProps.style,
};
return React.cloneElement(child as ReactElement, {
...childProps,
style: avatarStyle,
});
})}
</AvatarGroupContext.Provider>
</div>
);
});
export default Group;