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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
| const InternalAvatar: React.ForwardRefRenderFunction<unknown, AvatarProps> = (props, ref) => { const groupSize = React.useContext(SizeContext); const [scale, setScale] = React.useState(1); const [mounted, setMounted] = React.useState(false); const [isImgExist, setIsImgExist] = React.useState(true);
const avatarNodeRef = React.useRef<HTMLElement>(); const avatarChildrenRef = React.useRef<HTMLElement>(); const avatarNodeMergeRef = composeRef(ref, avatarNodeRef);
const { getPrefixCls } = React.useContext(ConfigContext);
const setScaleParam = () => { if (!avatarChildrenRef.current || !avatarNodeRef.current) { return; } const childrenWidth = avatarChildrenRef.current.offsetWidth; const nodeWidth = avatarNodeRef.current.offsetWidth; if (childrenWidth !== 0 && nodeWidth !== 0) { const { gap = 4 } = props; if (gap * 2 < nodeWidth) { setScale(nodeWidth - gap * 2 < childrenWidth ? (nodeWidth - gap * 2) / childrenWidth : 1); } } };
React.useEffect(() => { setMounted(true); }, []);
React.useEffect(() => { setIsImgExist(true); setScale(1); }, [props.src]);
React.useEffect(() => { setScaleParam(); }, [props.gap]);
const handleImgLoadError = () => { const { onError } = props; const errorFlag = onError ? onError() : undefined; if (errorFlag !== false) { setIsImgExist(false); } };
const { prefixCls: customizePrefixCls, shape, size: customSize, src, srcSet, icon, className, alt, draggable, children, ...others } = props;
const size = customSize === 'default' ? groupSize : customSize;
const screens = useBreakpoint(); const hasImageElement = React.isValidElement(src);
let childrenToRender; if (typeof src === 'string' && isImgExist) { childrenToRender = ( <img src={src} draggable={draggable} srcSet={srcSet} onError={handleImgLoadError} alt={alt} /> ); } else if (hasImageElement) { childrenToRender = src; } else if (icon) { childrenToRender = icon; } else if (mounted || scale !== 1) { const transformString = `scale(${scale}) translateX(-50%)`; const childrenStyle: React.CSSProperties = { msTransform: transformString, WebkitTransform: transformString, transform: transformString, };
const sizeChildrenStyle: React.CSSProperties = typeof size === 'number' ? { lineHeight: `${size}px`, } : {};
childrenToRender = ( <ResizeObserver onResize={setScaleParam}> <span className={`${prefixCls}-string`} ref={(node: HTMLElement) => { avatarChildrenRef.current = node; }} style={{ ...sizeChildrenStyle, ...childrenStyle }} > {children} </span> </ResizeObserver> ); } else { childrenToRender = ( <span className={`${prefixCls}-string`} style={{ opacity: 0 }} ref={(node: HTMLElement) => { avatarChildrenRef.current = node; }} > {children} </span> ); }
delete others.onError; delete others.gap;
return ( <span {...others} style={{ ...sizeStyle, ...responsiveSizeStyle, ...others.style }} className={classString} ref={avatarNodeMergeRef as any} > {childrenToRender} </span> ); };
const Avatar = React.forwardRef<unknown, AvatarProps>(InternalAvatar); Avatar.displayName = 'Avatar';
Avatar.defaultProps = { shape: 'circle' as AvatarProps['shape'], size: 'default' as AvatarProps['size'], };
export default Avatar;
|