2020-07-13 12:45:37 +08:00
|
|
|
import * as React from 'react';
|
|
|
|
import classNames from 'classnames';
|
2020-09-02 15:28:23 +08:00
|
|
|
import ResizeObserver from 'rc-resize-observer';
|
2020-11-21 15:40:06 +08:00
|
|
|
import { composeRef } from 'rc-util/lib/ref';
|
2020-07-13 12:45:37 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
|
|
|
import devWarning from '../_util/devWarning';
|
2020-11-05 16:22:21 +08:00
|
|
|
import { Breakpoint, responsiveArray } from '../_util/responsiveObserve';
|
2020-08-29 13:43:52 +08:00
|
|
|
import useBreakpoint from '../grid/hooks/useBreakpoint';
|
2020-11-05 16:22:21 +08:00
|
|
|
import SizeContext, { AvatarSize } from './SizeContext';
|
2020-10-24 22:00:43 +08:00
|
|
|
|
2020-07-13 12:45:37 +08:00
|
|
|
export interface AvatarProps {
|
|
|
|
/** Shape of avatar, options:`circle`, `square` */
|
|
|
|
shape?: 'circle' | 'square';
|
|
|
|
/*
|
|
|
|
* Size of avatar, options: `large`, `small`, `default`
|
|
|
|
* or a custom number size
|
|
|
|
* */
|
2020-10-24 22:00:43 +08:00
|
|
|
size?: AvatarSize;
|
2020-07-13 12:45:37 +08:00
|
|
|
gap?: number;
|
|
|
|
/** Src of image avatar */
|
2020-11-04 10:38:27 +08:00
|
|
|
src?: React.ReactNode;
|
2020-07-13 12:45:37 +08:00
|
|
|
/** Srcset of image avatar */
|
|
|
|
srcSet?: string;
|
|
|
|
draggable?: boolean;
|
2020-12-28 15:30:18 +08:00
|
|
|
/** Icon to be used in avatar */
|
2020-07-13 12:45:37 +08:00
|
|
|
icon?: React.ReactNode;
|
|
|
|
style?: React.CSSProperties;
|
|
|
|
prefixCls?: string;
|
|
|
|
className?: string;
|
|
|
|
children?: React.ReactNode;
|
|
|
|
alt?: string;
|
|
|
|
/* callback when img load error */
|
|
|
|
/* return false to prevent Avatar show default fallback behavior, then you can do fallback by your self */
|
|
|
|
onError?: () => boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
const InternalAvatar: React.ForwardRefRenderFunction<unknown, AvatarProps> = (props, ref) => {
|
2020-11-05 16:22:21 +08:00
|
|
|
const groupSize = React.useContext(SizeContext);
|
|
|
|
|
2020-07-13 12:45:37 +08:00
|
|
|
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; // offsetWidth avoid affecting be transform scale
|
|
|
|
const nodeWidth = avatarNodeRef.current.offsetWidth;
|
|
|
|
// denominator is 0 is no meaning
|
2020-09-02 15:28:23 +08:00
|
|
|
if (childrenWidth !== 0 && nodeWidth !== 0) {
|
|
|
|
const { gap = 4 } = props;
|
|
|
|
if (gap * 2 < nodeWidth) {
|
|
|
|
setScale(nodeWidth - gap * 2 < childrenWidth ? (nodeWidth - gap * 2) / childrenWidth : 1);
|
|
|
|
}
|
2020-07-13 12:45:37 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
setMounted(true);
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
setIsImgExist(true);
|
|
|
|
setScale(1);
|
|
|
|
}, [props.src]);
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
setScaleParam();
|
2020-09-02 15:28:23 +08:00
|
|
|
}, [props.gap]);
|
2020-07-13 12:45:37 +08:00
|
|
|
|
|
|
|
const handleImgLoadError = () => {
|
|
|
|
const { onError } = props;
|
|
|
|
const errorFlag = onError ? onError() : undefined;
|
|
|
|
if (errorFlag !== false) {
|
|
|
|
setIsImgExist(false);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const {
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
shape,
|
2020-11-05 16:22:21 +08:00
|
|
|
size: customSize,
|
2020-07-13 12:45:37 +08:00
|
|
|
src,
|
|
|
|
srcSet,
|
|
|
|
icon,
|
|
|
|
className,
|
|
|
|
alt,
|
|
|
|
draggable,
|
2020-07-26 20:30:56 +08:00
|
|
|
children,
|
2020-07-13 12:45:37 +08:00
|
|
|
...others
|
|
|
|
} = props;
|
|
|
|
|
2020-11-05 16:22:21 +08:00
|
|
|
const size = customSize === 'default' ? groupSize : customSize;
|
|
|
|
|
2020-08-29 13:43:52 +08:00
|
|
|
const screens = useBreakpoint();
|
|
|
|
const responsiveSizeStyle: React.CSSProperties = React.useMemo(() => {
|
|
|
|
if (typeof size !== 'object') {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
const currentBreakpoint: Breakpoint = responsiveArray.find(screen => screens[screen])!;
|
|
|
|
const currentSize = size[currentBreakpoint];
|
|
|
|
|
|
|
|
return currentSize
|
|
|
|
? {
|
|
|
|
width: currentSize,
|
|
|
|
height: currentSize,
|
|
|
|
lineHeight: `${currentSize}px`,
|
|
|
|
fontSize: icon ? currentSize / 2 : 18,
|
|
|
|
}
|
|
|
|
: {};
|
|
|
|
}, [screens, size]);
|
|
|
|
|
2020-07-13 12:45:37 +08:00
|
|
|
devWarning(
|
|
|
|
!(typeof icon === 'string' && icon.length > 2),
|
|
|
|
'Avatar',
|
|
|
|
`\`icon\` is using ReactNode instead of string naming in v4. Please check \`${icon}\` at https://ant.design/components/icon`,
|
|
|
|
);
|
|
|
|
|
|
|
|
const prefixCls = getPrefixCls('avatar', customizePrefixCls);
|
|
|
|
|
|
|
|
const sizeCls = classNames({
|
|
|
|
[`${prefixCls}-lg`]: size === 'large',
|
|
|
|
[`${prefixCls}-sm`]: size === 'small',
|
|
|
|
});
|
|
|
|
|
2020-10-31 16:15:55 +08:00
|
|
|
const hasImageElement = React.isValidElement(src);
|
|
|
|
|
2020-09-06 13:07:39 +08:00
|
|
|
const classString = classNames(
|
|
|
|
prefixCls,
|
|
|
|
sizeCls,
|
|
|
|
{
|
|
|
|
[`${prefixCls}-${shape}`]: shape,
|
2020-10-31 16:15:55 +08:00
|
|
|
[`${prefixCls}-image`]: hasImageElement || (src && isImgExist),
|
2020-09-06 13:07:39 +08:00
|
|
|
[`${prefixCls}-icon`]: icon,
|
|
|
|
},
|
|
|
|
className,
|
|
|
|
);
|
2020-07-13 12:45:37 +08:00
|
|
|
|
|
|
|
const sizeStyle: React.CSSProperties =
|
|
|
|
typeof size === 'number'
|
|
|
|
? {
|
|
|
|
width: size,
|
|
|
|
height: size,
|
|
|
|
lineHeight: `${size}px`,
|
|
|
|
fontSize: icon ? size / 2 : 18,
|
|
|
|
}
|
|
|
|
: {};
|
|
|
|
|
2020-07-26 20:30:56 +08:00
|
|
|
let childrenToRender;
|
2020-10-31 16:15:55 +08:00
|
|
|
if (typeof src === 'string' && isImgExist) {
|
2020-07-26 20:30:56 +08:00
|
|
|
childrenToRender = (
|
2020-07-13 12:45:37 +08:00
|
|
|
<img src={src} draggable={draggable} srcSet={srcSet} onError={handleImgLoadError} alt={alt} />
|
|
|
|
);
|
2020-10-31 16:15:55 +08:00
|
|
|
} else if (hasImageElement) {
|
|
|
|
childrenToRender = src;
|
2020-07-13 12:45:37 +08:00
|
|
|
} else if (icon) {
|
2020-07-26 20:30:56 +08:00
|
|
|
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 = (
|
2020-09-02 15:28:23 +08:00
|
|
|
<ResizeObserver onResize={setScaleParam}>
|
|
|
|
<span
|
|
|
|
className={`${prefixCls}-string`}
|
|
|
|
ref={(node: HTMLElement) => {
|
|
|
|
avatarChildrenRef.current = node;
|
|
|
|
}}
|
|
|
|
style={{ ...sizeChildrenStyle, ...childrenStyle }}
|
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</span>
|
|
|
|
</ResizeObserver>
|
2020-07-26 20:30:56 +08:00
|
|
|
);
|
2020-07-13 12:45:37 +08:00
|
|
|
} else {
|
2020-07-26 20:30:56 +08:00
|
|
|
childrenToRender = (
|
|
|
|
<span
|
|
|
|
className={`${prefixCls}-string`}
|
|
|
|
style={{ opacity: 0 }}
|
|
|
|
ref={(node: HTMLElement) => {
|
|
|
|
avatarChildrenRef.current = node;
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</span>
|
|
|
|
);
|
2020-07-13 12:45:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// The event is triggered twice from bubbling up the DOM tree.
|
|
|
|
// see https://codesandbox.io/s/kind-snow-9lidz
|
|
|
|
delete others.onError;
|
|
|
|
delete others.gap;
|
2020-07-26 20:30:56 +08:00
|
|
|
|
2020-07-13 12:45:37 +08:00
|
|
|
return (
|
|
|
|
<span
|
|
|
|
{...others}
|
2020-08-29 13:43:52 +08:00
|
|
|
style={{ ...sizeStyle, ...responsiveSizeStyle, ...others.style }}
|
2020-07-13 12:45:37 +08:00
|
|
|
className={classString}
|
|
|
|
ref={avatarNodeMergeRef as any}
|
|
|
|
>
|
2020-07-26 20:30:56 +08:00
|
|
|
{childrenToRender}
|
2020-07-13 12:45:37 +08:00
|
|
|
</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;
|