|
1 | 1 | import { tuple, VueNode } from '../_util/type';
|
2 |
| -import { CSSProperties, defineComponent, inject, nextTick, PropType } from 'vue'; |
| 2 | +import { |
| 3 | + CSSProperties, |
| 4 | + defineComponent, |
| 5 | + ExtractPropTypes, |
| 6 | + inject, |
| 7 | + nextTick, |
| 8 | + onMounted, |
| 9 | + onUpdated, |
| 10 | + PropType, |
| 11 | + ref, |
| 12 | + watch, |
| 13 | +} from 'vue'; |
3 | 14 | import { defaultConfigProvider } from '../config-provider';
|
4 |
| -import { getComponent } from '../_util/props-util'; |
| 15 | +import { getPropsSlot } from '../_util/props-util'; |
5 | 16 | import PropTypes from '../_util/vue-types';
|
6 | 17 |
|
7 |
| -export default defineComponent({ |
8 |
| - name: 'AAvatar', |
9 |
| - props: { |
10 |
| - prefixCls: PropTypes.string, |
11 |
| - shape: PropTypes.oneOf(tuple('circle', 'square')), |
12 |
| - size: { |
13 |
| - type: [Number, String] as PropType<'large' | 'small' | 'default' | number>, |
14 |
| - default: 'default', |
15 |
| - }, |
16 |
| - src: PropTypes.string, |
17 |
| - /** Srcset of image avatar */ |
18 |
| - srcset: PropTypes.string, |
19 |
| - /** @deprecated please use `srcset` instead `srcSet` */ |
20 |
| - srcSet: PropTypes.string, |
21 |
| - icon: PropTypes.VNodeChild, |
22 |
| - alt: PropTypes.string, |
23 |
| - loadError: { |
24 |
| - type: Function as PropType<() => boolean>, |
25 |
| - }, |
26 |
| - }, |
27 |
| - setup() { |
28 |
| - return { |
29 |
| - configProvider: inject('configProvider', defaultConfigProvider), |
30 |
| - }; |
31 |
| - }, |
32 |
| - data() { |
33 |
| - return { |
34 |
| - isImgExist: true, |
35 |
| - isMounted: false, |
36 |
| - scale: 1, |
37 |
| - lastChildrenWidth: undefined, |
38 |
| - lastNodeWidth: undefined, |
39 |
| - }; |
40 |
| - }, |
41 |
| - watch: { |
42 |
| - src() { |
43 |
| - nextTick(() => { |
44 |
| - this.isImgExist = true; |
45 |
| - this.scale = 1; |
46 |
| - // force uodate for position |
47 |
| - this.$forceUpdate(); |
48 |
| - }); |
49 |
| - }, |
50 |
| - }, |
51 |
| - mounted() { |
52 |
| - nextTick(() => { |
53 |
| - this.setScale(); |
54 |
| - this.isMounted = true; |
55 |
| - }); |
| 18 | +const avatarProps = { |
| 19 | + prefixCls: PropTypes.string, |
| 20 | + shape: PropTypes.oneOf(tuple('circle', 'square')), |
| 21 | + size: { |
| 22 | + type: [Number, String] as PropType<'large' | 'small' | 'default' | number>, |
| 23 | + default: 'default', |
56 | 24 | },
|
57 |
| - updated() { |
58 |
| - nextTick(() => { |
59 |
| - this.setScale(); |
60 |
| - }); |
| 25 | + src: PropTypes.string, |
| 26 | + /** Srcset of image avatar */ |
| 27 | + srcset: PropTypes.string, |
| 28 | + icon: PropTypes.VNodeChild, |
| 29 | + alt: PropTypes.string, |
| 30 | + loadError: { |
| 31 | + type: Function as PropType<() => boolean>, |
61 | 32 | },
|
62 |
| - methods: { |
63 |
| - setScale() { |
64 |
| - if (!this.$refs.avatarChildren || !this.$refs.avatarNode) { |
| 33 | +}; |
| 34 | + |
| 35 | +export type AvatarProps = Partial<ExtractPropTypes<typeof avatarProps>>; |
| 36 | + |
| 37 | +const Avatar = defineComponent({ |
| 38 | + name: 'AAvatar', |
| 39 | + props: avatarProps, |
| 40 | + setup(props, { slots }) { |
| 41 | + const isImgExist = ref(true); |
| 42 | + const isMounted = ref(false); |
| 43 | + const scale = ref(1); |
| 44 | + const lastChildrenWidth = ref<number>(undefined); |
| 45 | + const lastNodeWidth = ref<number>(undefined); |
| 46 | + |
| 47 | + const avatarChildrenRef = ref<HTMLElement>(null); |
| 48 | + const avatarNodeRef = ref<HTMLElement>(null); |
| 49 | + |
| 50 | + const configProvider = inject('configProvider', defaultConfigProvider); |
| 51 | + |
| 52 | + const setScale = () => { |
| 53 | + if (!avatarChildrenRef.value || !avatarNodeRef.value) { |
65 | 54 | return;
|
66 | 55 | }
|
67 |
| - const childrenWidth = (this.$refs.avatarChildren as HTMLElement).offsetWidth; // offsetWidth avoid affecting be transform scale |
68 |
| - const nodeWidth = (this.$refs.avatarNode as HTMLElement).offsetWidth; |
| 56 | + const childrenWidth = avatarChildrenRef.value.offsetWidth; // offsetWidth avoid affecting be transform scale |
| 57 | + const nodeWidth = avatarNodeRef.value.offsetWidth; |
69 | 58 | // denominator is 0 is no meaning
|
70 | 59 | if (
|
71 | 60 | childrenWidth === 0 ||
|
72 | 61 | nodeWidth === 0 ||
|
73 |
| - (this.lastChildrenWidth === childrenWidth && this.lastNodeWidth === nodeWidth) |
| 62 | + (lastChildrenWidth.value === childrenWidth && lastNodeWidth.value === nodeWidth) |
74 | 63 | ) {
|
75 | 64 | return;
|
76 | 65 | }
|
77 |
| - this.lastChildrenWidth = childrenWidth; |
78 |
| - this.lastNodeWidth = nodeWidth; |
| 66 | + lastChildrenWidth.value = childrenWidth; |
| 67 | + lastNodeWidth.value = nodeWidth; |
79 | 68 | // add 4px gap for each side to get better performance
|
80 |
| - this.scale = nodeWidth - 8 < childrenWidth ? (nodeWidth - 8) / childrenWidth : 1; |
81 |
| - }, |
82 |
| - handleImgLoadError() { |
83 |
| - const { loadError } = this.$props; |
84 |
| - const errorFlag = loadError ? loadError() : undefined; |
| 69 | + scale.value = nodeWidth - 8 < childrenWidth ? (nodeWidth - 8) / childrenWidth : 1; |
| 70 | + }; |
| 71 | + |
| 72 | + const handleImgLoadError = () => { |
| 73 | + const { loadError } = props; |
| 74 | + const errorFlag = loadError?.(); |
85 | 75 | if (errorFlag !== false) {
|
86 |
| - this.isImgExist = false; |
| 76 | + isImgExist.value = false; |
87 | 77 | }
|
88 |
| - }, |
89 |
| - }, |
90 |
| - render() { |
91 |
| - const { prefixCls: customizePrefixCls, shape, size, src, alt, srcset, srcSet } = this.$props; |
92 |
| - const icon = getComponent(this, 'icon'); |
93 |
| - const getPrefixCls = this.configProvider.getPrefixCls; |
94 |
| - const prefixCls = getPrefixCls('avatar', customizePrefixCls); |
| 78 | + }; |
95 | 79 |
|
96 |
| - const { isImgExist, scale, isMounted } = this.$data; |
| 80 | + watch( |
| 81 | + () => props.src, |
| 82 | + () => { |
| 83 | + nextTick(() => { |
| 84 | + isImgExist.value = true; |
| 85 | + scale.value = 1; |
| 86 | + }); |
| 87 | + }, |
| 88 | + ); |
97 | 89 |
|
98 |
| - const sizeCls = { |
99 |
| - [`${prefixCls}-lg`]: size === 'large', |
100 |
| - [`${prefixCls}-sm`]: size === 'small', |
101 |
| - }; |
| 90 | + onMounted(() => { |
| 91 | + nextTick(() => { |
| 92 | + setScale(); |
| 93 | + isMounted.value = true; |
| 94 | + }); |
| 95 | + }); |
102 | 96 |
|
103 |
| - const classString = { |
104 |
| - [prefixCls]: true, |
105 |
| - ...sizeCls, |
106 |
| - [`${prefixCls}-${shape}`]: shape, |
107 |
| - [`${prefixCls}-image`]: src && isImgExist, |
108 |
| - [`${prefixCls}-icon`]: icon, |
109 |
| - }; |
| 97 | + onUpdated(() => { |
| 98 | + nextTick(() => { |
| 99 | + setScale(); |
| 100 | + }); |
| 101 | + }); |
110 | 102 |
|
111 |
| - const sizeStyle: CSSProperties = |
112 |
| - typeof size === 'number' |
113 |
| - ? { |
114 |
| - width: `${size}px`, |
115 |
| - height: `${size}px`, |
116 |
| - lineHeight: `${size}px`, |
117 |
| - fontSize: icon ? `${size / 2}px` : '18px', |
118 |
| - } |
119 |
| - : {}; |
120 |
| - |
121 |
| - let children: VueNode = this.$slots.default?.(); |
122 |
| - if (src && isImgExist) { |
123 |
| - children = ( |
124 |
| - <img src={src} srcset={srcset || srcSet} onError={this.handleImgLoadError} alt={alt} /> |
125 |
| - ); |
126 |
| - } else if (icon) { |
127 |
| - children = icon; |
128 |
| - } else { |
129 |
| - const childrenNode = this.$refs.avatarChildren; |
130 |
| - if (childrenNode || scale !== 1) { |
131 |
| - const transformString = `scale(${scale}) translateX(-50%)`; |
132 |
| - const childrenStyle: CSSProperties = { |
133 |
| - msTransform: transformString, |
134 |
| - WebkitTransform: transformString, |
135 |
| - transform: transformString, |
136 |
| - }; |
137 |
| - const sizeChildrenStyle = |
138 |
| - typeof size === 'number' |
139 |
| - ? { |
140 |
| - lineHeight: `${size}px`, |
141 |
| - } |
142 |
| - : {}; |
143 |
| - children = ( |
144 |
| - <span |
145 |
| - class={`${prefixCls}-string`} |
146 |
| - ref="avatarChildren" |
147 |
| - style={{ ...sizeChildrenStyle, ...childrenStyle }} |
148 |
| - > |
149 |
| - {children} |
150 |
| - </span> |
151 |
| - ); |
| 103 | + return () => { |
| 104 | + const { prefixCls: customizePrefixCls, shape, size, src, alt, srcset } = props; |
| 105 | + const icon = getPropsSlot(slots, props, 'icon'); |
| 106 | + const getPrefixCls = configProvider.getPrefixCls; |
| 107 | + const prefixCls = getPrefixCls('avatar', customizePrefixCls); |
| 108 | + |
| 109 | + const classString = { |
| 110 | + [prefixCls]: true, |
| 111 | + [`${prefixCls}-lg`]: size === 'large', |
| 112 | + [`${prefixCls}-sm`]: size === 'small', |
| 113 | + [`${prefixCls}-${shape}`]: shape, |
| 114 | + [`${prefixCls}-image`]: src && isImgExist.value, |
| 115 | + [`${prefixCls}-icon`]: icon, |
| 116 | + }; |
| 117 | + |
| 118 | + const sizeStyle: CSSProperties = |
| 119 | + typeof size === 'number' |
| 120 | + ? { |
| 121 | + width: `${size}px`, |
| 122 | + height: `${size}px`, |
| 123 | + lineHeight: `${size}px`, |
| 124 | + fontSize: icon ? `${size / 2}px` : '18px', |
| 125 | + } |
| 126 | + : {}; |
| 127 | + |
| 128 | + let children: VueNode = slots.default?.(); |
| 129 | + if (src && isImgExist.value) { |
| 130 | + children = <img src={src} srcset={srcset} onError={handleImgLoadError} alt={alt} />; |
| 131 | + } else if (icon) { |
| 132 | + children = icon; |
152 | 133 | } else {
|
153 |
| - const childrenStyle: CSSProperties = {}; |
154 |
| - if (!isMounted) { |
155 |
| - childrenStyle.opacity = 0; |
| 134 | + const childrenNode = avatarChildrenRef.value; |
| 135 | + |
| 136 | + if (childrenNode || scale.value !== 1) { |
| 137 | + const transformString = `scale(${scale.value}) translateX(-50%)`; |
| 138 | + const childrenStyle: CSSProperties = { |
| 139 | + msTransform: transformString, |
| 140 | + WebkitTransform: transformString, |
| 141 | + transform: transformString, |
| 142 | + }; |
| 143 | + const sizeChildrenStyle = |
| 144 | + typeof size === 'number' |
| 145 | + ? { |
| 146 | + lineHeight: `${size}px`, |
| 147 | + } |
| 148 | + : {}; |
| 149 | + children = ( |
| 150 | + <span |
| 151 | + class={`${prefixCls}-string`} |
| 152 | + ref={avatarChildrenRef} |
| 153 | + style={{ ...sizeChildrenStyle, ...childrenStyle }} |
| 154 | + > |
| 155 | + {children} |
| 156 | + </span> |
| 157 | + ); |
| 158 | + } else { |
| 159 | + children = ( |
| 160 | + <span class={`${prefixCls}-string`} ref={avatarChildrenRef} style={{ opacity: 0 }}> |
| 161 | + {children} |
| 162 | + </span> |
| 163 | + ); |
156 | 164 | }
|
157 |
| - children = ( |
158 |
| - <span class={`${prefixCls}-string`} ref="avatarChildren" style={{ opacity: 0 }}> |
159 |
| - {children} |
160 |
| - </span> |
161 |
| - ); |
162 | 165 | }
|
163 |
| - } |
164 |
| - return ( |
165 |
| - <span ref="avatarNode" class={classString} style={sizeStyle}> |
166 |
| - {children} |
167 |
| - </span> |
168 |
| - ); |
| 166 | + return ( |
| 167 | + <span ref={avatarNodeRef} class={classString} style={sizeStyle}> |
| 168 | + {children} |
| 169 | + </span> |
| 170 | + ); |
| 171 | + }; |
169 | 172 | },
|
170 | 173 | });
|
| 174 | + |
| 175 | +export default Avatar; |
0 commit comments