@@ -4,13 +4,14 @@ import React, {
4
4
useRef ,
5
5
FunctionComponent ,
6
6
useContext ,
7
+ useCallback ,
7
8
} from 'react'
8
9
import type { MouseEvent } from 'react'
9
10
import Taro , { getEnv } from '@tarojs/taro'
10
11
import classNames from 'classnames'
11
12
import { User } from '@nutui/icons-react-taro'
12
- import Image from '@/packages/image/index.taro'
13
13
import { AvatarContext } from '@/packages/avatargroup/context'
14
+ import Image from '@/packages/image/index.taro'
14
15
import { BasicComponent , ComponentDefaults } from '@/utils/typings'
15
16
import AvatarGroup from '@/packages/avatargroup/index.taro'
16
17
@@ -34,9 +35,9 @@ const defaultProps = {
34
35
size : '' ,
35
36
shape : 'round' ,
36
37
icon : '' ,
37
- fit : 'cover' ,
38
38
background : '#eee' ,
39
39
color : '#666' ,
40
+ fit : 'cover' ,
40
41
src : '' ,
41
42
alt : '' ,
42
43
} as AvatarProps
@@ -52,9 +53,9 @@ export const Avatar: FunctionComponent<
52
53
background,
53
54
color,
54
55
src,
56
+ alt,
55
57
icon,
56
58
fit,
57
- alt,
58
59
className,
59
60
style,
60
61
onClick,
@@ -67,14 +68,15 @@ export const Avatar: FunctionComponent<
67
68
68
69
const [ maxSum , setMaxSum ] = useState ( 0 ) // avatarGroup里的avatar的个数
69
70
const [ showMax , setShowMax ] = useState ( false ) // 是否显示的最大头像个数
70
- const [ avatarIndex , setAvatarIndex ] = useState ( 1 ) // avatar的索引
71
+ const [ avatarIndex , setAvatarIndex ] = useState ( 1 )
71
72
const avatarRef = useRef < any > ( null )
72
73
const parent : any = useContext ( AvatarContext )
73
74
const sizeValue = [ 'large' , 'normal' , 'small' ]
75
+ const { propAvatarGroup, avatarGroupRef } = parent
74
76
75
77
const classes = classNames ( {
76
- [ `nut-avatar-${ parent ?. propAvatarGroup ?. size || size || 'normal' } ` ] : true ,
77
- [ `nut-avatar-${ parent ?. propAvatarGroup ?. shape || shape } ` ] : true ,
78
+ [ `nut-avatar-${ propAvatarGroup ?. size || size || 'normal' } ` ] : true ,
79
+ [ `nut-avatar-${ propAvatarGroup ?. shape || shape } ` ] : true ,
78
80
} )
79
81
const cls = classNames ( classPrefix , classes , className )
80
82
@@ -84,27 +86,53 @@ export const Avatar: FunctionComponent<
84
86
backgroundColor : `${ background } ` ,
85
87
color,
86
88
marginLeft :
87
- avatarIndex !== 1 && parent ?. propAvatarGroup ?. gap
88
- ? `${ parent ?. propAvatarGroup ?. gap } px`
89
+ avatarIndex !== 1 && propAvatarGroup ?. gap
90
+ ? `${ propAvatarGroup ?. gap } px`
89
91
: '' ,
90
92
zIndex :
91
- parent ?. propAvatarGroup ?. level === 'right'
93
+ propAvatarGroup ?. level === 'right'
92
94
? `${ Math . abs ( maxSum - avatarIndex ) } `
93
95
: '' ,
94
96
...style ,
95
97
}
96
98
97
99
const maxStyles : React . CSSProperties = {
98
- backgroundColor : `${ parent ?. propAvatarGroup ?. maxBackground } ` ,
99
- color : `${ parent ?. propAvatarGroup ?. maxColor } ` ,
100
+ backgroundColor : `${ propAvatarGroup ?. maxBackground } ` ,
101
+ color : `${ propAvatarGroup ?. maxColor } ` ,
100
102
}
101
103
104
+ const avatarLength = useCallback (
105
+ ( children : any ) => {
106
+ for ( let i = 0 ; i < children . length ; i ++ ) {
107
+ if (
108
+ children [ i ] &&
109
+ children [ i ] . classList &&
110
+ isAvatarInClassList ( children [ i ] )
111
+ ) {
112
+ children [ i ] . setAttribute ( 'data-index' , i + 1 )
113
+ }
114
+ }
115
+ const index = Number ( avatarRef ?. current ?. dataset ?. index )
116
+ const maxCount = propAvatarGroup ?. max
117
+ setMaxSum ( children . length )
118
+ setAvatarIndex ( index )
119
+ if (
120
+ index === children . length &&
121
+ index !== maxCount &&
122
+ children . length > maxCount
123
+ ) {
124
+ setShowMax ( true )
125
+ }
126
+ } ,
127
+ [ propAvatarGroup ?. max ]
128
+ )
129
+
102
130
useEffect ( ( ) => {
103
- const avatarChildren = parent ?. avatarGroupRef ?. current . children
131
+ const avatarChildren = avatarGroupRef ?. current . children
104
132
if ( avatarChildren ) {
105
133
avatarLength ( avatarChildren )
106
134
}
107
- } , [ ] )
135
+ } , [ avatarLength , avatarGroupRef ] )
108
136
109
137
const isAvatarInClassList = ( element : any ) => {
110
138
if ( getEnv ( ) === Taro . ENV_TYPE . WEB ) {
@@ -120,33 +148,8 @@ export const Avatar: FunctionComponent<
120
148
)
121
149
}
122
150
123
- const avatarLength = ( children : any ) => {
124
- for ( let i = 0 ; i < children . length ; i ++ ) {
125
- if (
126
- children [ i ] &&
127
- children [ i ] . classList &&
128
- isAvatarInClassList ( children [ i ] )
129
- ) {
130
- children [ i ] . setAttribute ( 'data-index' , i + 1 )
131
- }
132
- }
133
- const index = avatarRef ?. current ?. dataset ?. index
134
- const maxCount = parent ?. propAvatarGroup ?. max
135
- setMaxSum ( children . length )
136
- setAvatarIndex ( index )
137
- if (
138
- index === children . length &&
139
- index !== maxCount &&
140
- children . length > maxCount
141
- ) {
142
- setShowMax ( true )
143
- }
144
- }
145
-
146
151
const errorEvent = ( ) => {
147
- if ( onError ) {
148
- onError ( )
149
- }
152
+ onError && onError ( )
150
153
}
151
154
152
155
const clickAvatar = ( e : MouseEvent < HTMLDivElement > ) => {
@@ -156,17 +159,16 @@ export const Avatar: FunctionComponent<
156
159
return (
157
160
< >
158
161
{ ( showMax ||
159
- ! parent ?. propAvatarGroup ?. max ||
160
- avatarIndex <= parent ?. propAvatarGroup ?. max ) && (
162
+ ! propAvatarGroup ?. max ||
163
+ avatarIndex <= propAvatarGroup ?. max ) && (
161
164
< div
162
165
className = { cls }
163
166
{ ...rest }
164
167
style = { ! showMax ? styles : maxStyles }
165
168
onClick = { clickAvatar }
166
169
ref = { avatarRef }
167
170
>
168
- { ( ! parent ?. propAvatarGroup ?. max ||
169
- avatarIndex <= parent ?. propAvatarGroup ?. max ) && (
171
+ { ( ! propAvatarGroup ?. max || avatarIndex <= propAvatarGroup ?. max ) && (
170
172
< >
171
173
{ src && (
172
174
< Image
@@ -186,14 +188,11 @@ export const Avatar: FunctionComponent<
186
188
{ ! src && ! icon && ! children && < User className = "icon" /> }
187
189
</ >
188
190
) }
189
- { /* 折叠头像 */ }
190
191
{ showMax && (
191
192
< div className = "text" >
192
- { parent ?. propAvatarGroup ?. maxContent
193
- ? parent ?. propAvatarGroup ?. maxContent
194
- : `+ ${
195
- avatarIndex - Number ( parent ?. propAvatarGroup ?. max || 0 )
196
- } `}
193
+ { propAvatarGroup ?. maxContent
194
+ ? propAvatarGroup ?. maxContent
195
+ : `+ ${ avatarIndex - Number ( propAvatarGroup ?. max || 0 ) } ` }
197
196
</ div >
198
197
) }
199
198
</ div >
0 commit comments