|
1 |
| -import { Text, View } from '@tarojs/components' |
2 |
| -import Taro, { pxTransform, useEffect, useState } from '@tarojs/taro' |
3 |
| -import { isNumber, generateId } from '../utils' |
4 |
| -import { BG_COLOR_LIST } from '../utils/model' |
5 |
| -import { IProps, THeaderArray } from '../../../@types/avatar' |
| 1 | +import { Text, View, Image } from '@tarojs/components'; |
| 2 | +import Taro, { pxTransform, useEffect, useState } from '@tarojs/taro'; |
| 3 | +import { isNumber, generateId } from '../utils'; |
| 4 | +import { BG_COLOR_LIST } from '../utils/model'; |
| 5 | +import { IProps, THeaderArray } from '../../../@types/avatar'; |
6 | 6 |
|
7 | 7 | function ClAvatar(props: IProps) {
|
8 |
| - const [headList, setHeadList] = useState(props.headerArray) |
| 8 | + const [headList, setHeadList] = useState(props.headerArray); |
9 | 9 | useEffect(() => {
|
10 |
| - const list = props.headerArray || [] |
11 |
| - setHeadList(list.map((item: any) => { |
12 |
| - item.cu_avatar_id = generateId() |
13 |
| - return item |
14 |
| - })) |
15 |
| - }, [props.headerArray]) |
| 10 | + const list = props.headerArray || []; |
| 11 | + setHeadList( |
| 12 | + list.map((item: any) => { |
| 13 | + item.cu_avatar_id = generateId(); |
| 14 | + return item; |
| 15 | + }) |
| 16 | + ); |
| 17 | + }, [props.headerArray]); |
16 | 18 | const onClick = () => {
|
17 |
| - props.onClick && props.onClick() |
18 |
| - } |
| 19 | + props.onClick && props.onClick(); |
| 20 | + }; |
19 | 21 | const customSize = {
|
20 |
| - 'small': 48, |
21 |
| - 'normal': 64, |
22 |
| - 'large': 96, |
23 |
| - 'xlarge': 128 |
24 |
| - } |
25 |
| - const width = isNumber(props.size) ? pxTransform(props.size) : pxTransform(customSize[props.size || 'normal']) |
26 |
| - const height = isNumber(props.size) ? pxTransform(props.size) : pxTransform(customSize[props.size || 'normal']) |
27 |
| - const em = isNumber(props.size) ? props.size / 48 : customSize[props.size || 'normal'] / 48 |
28 |
| - const avatarArray = headList ? ( |
29 |
| - headList.map(((item: any) => ( |
30 |
| - <View |
31 |
| - key={item.cu_avatar_id} |
32 |
| - className={`${props.shape} ${BG_COLOR_LIST[item.bgColor || 'black']} ${ |
33 |
| - props.shadow ? 'shadow' : '' |
34 |
| - } cu-avatar`} |
35 |
| - style={item.url ? { |
36 |
| - backgroundImage: `url(${item.url})`, |
| 22 | + small: 48, |
| 23 | + normal: 64, |
| 24 | + large: 96, |
| 25 | + xlarge: 128 |
| 26 | + }; |
| 27 | + const width = isNumber(props.size) |
| 28 | + ? pxTransform(props.size as number) |
| 29 | + : pxTransform(customSize[props.size || 'normal']); |
| 30 | + const height = isNumber(props.size) |
| 31 | + ? pxTransform(props.size as number) |
| 32 | + : pxTransform(customSize[props.size || 'normal']); |
| 33 | + const em = isNumber(props.size) |
| 34 | + ? (props.size as number) / 48 |
| 35 | + : customSize[props.size || 'normal'] / 48; |
| 36 | + const avatarArray = (headList as THeaderArray[]).map((item: any) => ( |
| 37 | + <View |
| 38 | + key={item.cu_avatar_id} |
| 39 | + className={`${props.shape} ${BG_COLOR_LIST[item.bgColor || 'black']} ${ |
| 40 | + props.shadow ? 'shadow' : '' |
| 41 | + } cu-avatar`} |
| 42 | + style={{ |
| 43 | + width, |
| 44 | + height, |
| 45 | + fontSize: `${em}em` |
| 46 | + }} |
| 47 | + > |
| 48 | + <Image |
| 49 | + className={`${props.shape}`} |
| 50 | + src={item.url} |
| 51 | + style={{ |
37 | 52 | width,
|
38 | 53 | height,
|
39 |
| - fontSize: `${em}em` |
40 |
| - } : { width, height, fontSize: `${em}em` }} |
41 |
| - > |
42 |
| - <Text className={`cuIcon-${item.icon}`}> |
43 |
| - {item.text ? item.text.slice(0, 1) : ''} |
44 |
| - </Text> |
45 |
| - {item.tag ? ( |
46 |
| - <View |
47 |
| - className={`cu-tag badge cuIcon-${item.tag} ${ |
48 |
| - item.tagColor ? BG_COLOR_LIST[item.tagColor] : '' |
49 |
| - }`} |
50 |
| - /> |
51 |
| - ) : ( |
52 |
| - '' |
53 |
| - )} |
54 |
| - </View> |
55 |
| - )) |
56 |
| - ) : ( |
57 |
| - <View /> |
58 |
| - ) |
| 54 | + position: 'absolute', |
| 55 | + left: 0, |
| 56 | + right: 0, |
| 57 | + bottom: 0, |
| 58 | + top: 0 |
| 59 | + }} |
| 60 | + mode='aspectFill' |
| 61 | + /> |
| 62 | + <Text className={`cuIcon-${item.icon}`}> |
| 63 | + {item.text ? item.text.slice(0, 1) : ''} |
| 64 | + </Text> |
| 65 | + {item.tag ? ( |
| 66 | + <View |
| 67 | + className={`cu-tag badge cuIcon-${item.tag} ${ |
| 68 | + item.tagColor ? BG_COLOR_LIST[item.tagColor] : '' |
| 69 | + }`} |
| 70 | + /> |
| 71 | + ) : ( |
| 72 | + '' |
| 73 | + )} |
| 74 | + </View> |
| 75 | + )); |
59 | 76 | const avatarArrayComponent = (
|
60 | 77 | <View
|
61 | 78 | className='cu-avatar-group'
|
62 | 79 | onClick={() => {
|
63 |
| - onClick() |
| 80 | + onClick(); |
64 | 81 | }}
|
65 | 82 | >
|
66 | 83 | {avatarArray}
|
67 | 84 | </View>
|
68 |
| - ) |
| 85 | + ); |
69 | 86 | return props.headerArray && props.headerArray.length > 1 ? (
|
70 | 87 | avatarArrayComponent
|
71 | 88 | ) : (
|
72 |
| - <View |
73 |
| - onClick={() => { |
74 |
| - onClick() |
75 |
| - }} |
76 |
| - > |
77 |
| - {avatarArray} |
78 |
| - </View> |
79 |
| - ) |
| 89 | + <View |
| 90 | + onClick={() => { |
| 91 | + onClick(); |
| 92 | + }} |
| 93 | + > |
| 94 | + {avatarArray} |
| 95 | + </View> |
| 96 | + ); |
80 | 97 | }
|
81 | 98 |
|
82 | 99 | ClAvatar.options = {
|
83 | 100 | addGlobalClass: true
|
84 |
| -} |
| 101 | +}; |
85 | 102 |
|
86 | 103 | ClAvatar.defaultProps = {
|
87 | 104 | size: 'normal',
|
88 | 105 | shape: 'radius',
|
89 | 106 | type: 'normal',
|
90 | 107 | headerArray: [],
|
91 | 108 | shadow: true
|
92 |
| -} as IProps |
| 109 | +} as IProps; |
93 | 110 |
|
94 |
| -export default ClAvatar |
| 111 | +export default ClAvatar; |
0 commit comments