|
| 1 | +import React, { forwardRef, useRef, useState, useEffect, useContext } from 'react'; |
| 2 | +import useResizeObserver from 'use-resize-observer'; |
| 3 | +import classNames from 'classnames'; |
| 4 | +import useConfig from '../_util/useConfig'; |
| 5 | +import useCommonClassName from '../_util/useCommonClassName'; |
| 6 | +import composeRefs from '../_util/composeRefs'; |
| 7 | +import { TdAvatarProps } from './type'; |
| 8 | +import { StyledProps } from '../common'; |
| 9 | +import AvatarContext from './AvatarContext'; |
| 10 | + |
| 11 | +export interface AvatarProps extends TdAvatarProps, StyledProps { |
| 12 | + children?: React.ReactNode; |
| 13 | +} |
| 14 | +const Avatar = forwardRef<HTMLElement, AvatarProps>((props, ref) => { |
| 15 | + const { |
| 16 | + alt, |
| 17 | + hideOnLoadFailed = false, |
| 18 | + icon, |
| 19 | + image, |
| 20 | + shape = 'circle', |
| 21 | + size: avatarSize = 'default', |
| 22 | + onError, |
| 23 | + children, |
| 24 | + style, |
| 25 | + className, |
| 26 | + ...avatarProps |
| 27 | + } = props; |
| 28 | + const groupSize = useContext(AvatarContext); |
| 29 | + const { classPrefix } = useConfig(); |
| 30 | + const [scale, setScale] = useState(1); |
| 31 | + const [isImgExist, setIsImgExist] = useState(true); |
| 32 | + const avatarRef = useRef<HTMLElement>(null); |
| 33 | + const avatarChildrenRef = useRef<HTMLElement>(null); |
| 34 | + const size = avatarSize === 'default' ? groupSize : avatarSize; |
| 35 | + const gap = 4; |
| 36 | + const handleScale = () => { |
| 37 | + if (!avatarChildrenRef.current || !avatarRef.current) { |
| 38 | + return; |
| 39 | + } |
| 40 | + const childrenWidth = avatarChildrenRef.current.offsetWidth; |
| 41 | + const avatarWidth = avatarRef.current.offsetWidth; |
| 42 | + |
| 43 | + if (childrenWidth !== 0 && avatarWidth !== 0) { |
| 44 | + if (gap * 2 < avatarWidth) { |
| 45 | + setScale(avatarWidth - gap * 2 < childrenWidth ? (avatarWidth - gap * 2) / childrenWidth : 1); |
| 46 | + } |
| 47 | + } |
| 48 | + }; |
| 49 | + const { ref: observerRef } = useResizeObserver<HTMLDivElement>({ |
| 50 | + onResize: handleScale, |
| 51 | + }); |
| 52 | + |
| 53 | + const handleImgLoadError = () => { |
| 54 | + onError && onError(); |
| 55 | + !hideOnLoadFailed && setIsImgExist(false); |
| 56 | + }; |
| 57 | + |
| 58 | + useEffect(() => { |
| 59 | + setIsImgExist(true); |
| 60 | + setScale(1); |
| 61 | + }, [props.image]); |
| 62 | + |
| 63 | + useEffect(() => { |
| 64 | + handleScale(); |
| 65 | + }, []); |
| 66 | + |
| 67 | + const { SIZE } = useCommonClassName(); |
| 68 | + const numSizeStyle: React.CSSProperties = |
| 69 | + size && !SIZE[size] |
| 70 | + ? { |
| 71 | + width: size, |
| 72 | + height: size, |
| 73 | + fontSize: `${Number.parseInt(size, 10) / 2}px`, |
| 74 | + } |
| 75 | + : {}; |
| 76 | + const imageStyle: React.CSSProperties = |
| 77 | + size && !SIZE[size] |
| 78 | + ? { |
| 79 | + width: size, |
| 80 | + height: size, |
| 81 | + } |
| 82 | + : {}; |
| 83 | + |
| 84 | + const preClass = `${classPrefix}-avatar`; |
| 85 | + |
| 86 | + const avatarClass = classNames(preClass, className, { |
| 87 | + [SIZE[size]]: !!SIZE[size], |
| 88 | + [`${preClass}--${shape}`]: !!shape, |
| 89 | + [`${preClass}-icon`]: !!icon, |
| 90 | + }); |
| 91 | + |
| 92 | + let content; |
| 93 | + if (image && isImgExist) { |
| 94 | + content = <img src={image} alt={alt} style={imageStyle} onError={handleImgLoadError} />; |
| 95 | + } else if (icon) { |
| 96 | + content = icon; |
| 97 | + } else { |
| 98 | + const childrenStyle: React.CSSProperties = { |
| 99 | + transform: `scale(${scale})`, |
| 100 | + }; |
| 101 | + content = ( |
| 102 | + <span ref={composeRefs(ref, avatarChildrenRef, observerRef) as any} style={childrenStyle}> |
| 103 | + {children} |
| 104 | + </span> |
| 105 | + ); |
| 106 | + } |
| 107 | + return ( |
| 108 | + <div |
| 109 | + ref={composeRefs(ref, avatarRef) as any} |
| 110 | + className={avatarClass} |
| 111 | + style={{ ...numSizeStyle, ...style }} |
| 112 | + {...avatarProps} |
| 113 | + > |
| 114 | + {content} |
| 115 | + </div> |
| 116 | + ); |
| 117 | +}); |
| 118 | + |
| 119 | +Avatar.displayName = 'Avatar'; |
| 120 | + |
| 121 | +export default Avatar; |
0 commit comments