2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
2017-05-28 14:20:59 +08:00
|
|
|
import classNames from 'classnames';
|
2019-08-13 14:07:17 +08:00
|
|
|
|
2018-12-05 19:12:18 +08:00
|
|
|
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
|
2020-05-14 15:57:04 +08:00
|
|
|
import devWarning from '../_util/devWarning';
|
2017-05-28 14:20:59 +08:00
|
|
|
|
|
|
|
export interface AvatarProps {
|
|
|
|
/** Shape of avatar, options:`circle`, `square` */
|
|
|
|
shape?: 'circle' | 'square';
|
2018-08-02 20:25:14 +08:00
|
|
|
/*
|
|
|
|
* Size of avatar, options: `large`, `small`, `default`
|
|
|
|
* or a custom number size
|
|
|
|
* */
|
|
|
|
size?: 'large' | 'small' | 'default' | number;
|
2020-05-25 17:56:48 +08:00
|
|
|
gap?: number;
|
2017-05-28 14:20:59 +08:00
|
|
|
/** Src of image avatar */
|
|
|
|
src?: string;
|
2018-10-06 18:25:45 +08:00
|
|
|
/** Srcset of image avatar */
|
|
|
|
srcSet?: string;
|
2020-05-20 19:30:39 +08:00
|
|
|
draggable?: boolean;
|
2019-08-13 14:07:17 +08:00
|
|
|
/** icon to be used in avatar */
|
|
|
|
icon?: React.ReactNode;
|
2017-05-28 14:20:59 +08:00
|
|
|
style?: React.CSSProperties;
|
|
|
|
prefixCls?: string;
|
|
|
|
className?: string;
|
2019-06-16 20:51:47 +08:00
|
|
|
children?: React.ReactNode;
|
2018-06-07 11:36:16 +08:00
|
|
|
alt?: string;
|
2018-07-24 12:14:34 +08:00
|
|
|
/* callback when img load error */
|
2019-08-05 18:38:10 +08:00
|
|
|
/* return false to prevent Avatar show default fallback behavior, then you can do fallback by your self */
|
2018-07-24 12:14:34 +08:00
|
|
|
onError?: () => boolean;
|
2017-05-28 14:20:59 +08:00
|
|
|
}
|
|
|
|
|
2017-07-31 00:43:33 +08:00
|
|
|
export interface AvatarState {
|
|
|
|
scale: number;
|
2019-09-27 14:17:42 +08:00
|
|
|
mounted: boolean;
|
2017-07-31 00:43:33 +08:00
|
|
|
isImgExist: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class Avatar extends React.Component<AvatarProps, AvatarState> {
|
2017-06-07 11:18:31 +08:00
|
|
|
static defaultProps = {
|
2018-12-22 21:21:42 +08:00
|
|
|
shape: 'circle' as AvatarProps['shape'],
|
|
|
|
size: 'default' as AvatarProps['size'],
|
2017-06-07 11:18:31 +08:00
|
|
|
};
|
2017-06-08 11:44:04 +08:00
|
|
|
|
2018-11-06 16:23:53 +08:00
|
|
|
state = {
|
|
|
|
scale: 1,
|
2019-09-27 14:17:42 +08:00
|
|
|
mounted: false,
|
2018-11-06 16:23:53 +08:00
|
|
|
isImgExist: true,
|
|
|
|
};
|
2017-05-28 14:20:59 +08:00
|
|
|
|
2019-03-21 10:07:56 +08:00
|
|
|
private avatarNode: HTMLElement;
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2019-03-21 10:07:56 +08:00
|
|
|
private avatarChildren: HTMLElement;
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2019-03-21 10:07:56 +08:00
|
|
|
private lastChildrenWidth: number;
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2019-03-21 10:07:56 +08:00
|
|
|
private lastNodeWidth: number;
|
2017-05-28 14:20:59 +08:00
|
|
|
|
2017-06-07 11:18:31 +08:00
|
|
|
componentDidMount() {
|
2017-06-08 11:44:04 +08:00
|
|
|
this.setScale();
|
2019-09-27 14:17:42 +08:00
|
|
|
this.setState({ mounted: true });
|
2017-06-08 11:44:04 +08:00
|
|
|
}
|
|
|
|
|
2019-03-21 10:07:56 +08:00
|
|
|
componentDidUpdate(prevProps: AvatarProps) {
|
2019-01-29 21:11:46 +08:00
|
|
|
if (prevProps.src !== this.props.src) {
|
2019-01-31 06:32:44 +08:00
|
|
|
this.setState({ isImgExist: true, scale: 1 });
|
2019-01-29 21:11:46 +08:00
|
|
|
}
|
2020-05-25 17:56:48 +08:00
|
|
|
if (prevProps.children !== this.props.children || prevProps.gap !== this.props.gap) {
|
|
|
|
this.setScale();
|
|
|
|
}
|
2017-06-08 11:44:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
setScale = () => {
|
2019-03-21 10:07:56 +08:00
|
|
|
if (!this.avatarChildren || !this.avatarNode) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const childrenWidth = this.avatarChildren.offsetWidth; // offsetWidth avoid affecting be transform scale
|
|
|
|
const nodeWidth = this.avatarNode.offsetWidth;
|
2020-05-25 17:56:48 +08:00
|
|
|
const { gap = 4 } = this.props;
|
2019-03-13 19:30:47 +08:00
|
|
|
// denominator is 0 is no meaning
|
2019-03-21 10:07:56 +08:00
|
|
|
if (
|
2020-05-25 17:56:48 +08:00
|
|
|
childrenWidth !== 0 &&
|
|
|
|
nodeWidth !== 0 &&
|
|
|
|
(this.lastChildrenWidth !== childrenWidth || this.lastNodeWidth !== nodeWidth)
|
2019-03-21 10:07:56 +08:00
|
|
|
) {
|
2020-05-25 17:56:48 +08:00
|
|
|
this.lastChildrenWidth = childrenWidth;
|
|
|
|
this.lastNodeWidth = nodeWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gap * 2 < nodeWidth) {
|
|
|
|
this.setState({
|
|
|
|
scale: nodeWidth - gap * 2 < childrenWidth ? (nodeWidth - gap * 2) / childrenWidth : 1,
|
|
|
|
});
|
2017-06-07 11:18:31 +08:00
|
|
|
}
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2017-05-28 14:20:59 +08:00
|
|
|
|
2018-07-24 12:14:34 +08:00
|
|
|
handleImgLoadError = () => {
|
|
|
|
const { onError } = this.props;
|
|
|
|
const errorFlag = onError ? onError() : undefined;
|
|
|
|
if (errorFlag !== false) {
|
|
|
|
this.setState({ isImgExist: false });
|
|
|
|
}
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2017-07-14 16:10:52 +08:00
|
|
|
|
2018-12-05 19:12:18 +08:00
|
|
|
renderAvatar = ({ getPrefixCls }: ConfigConsumerProps) => {
|
2017-06-07 11:18:31 +08:00
|
|
|
const {
|
2018-12-05 19:12:18 +08:00
|
|
|
prefixCls: customizePrefixCls,
|
2018-12-07 20:02:01 +08:00
|
|
|
shape,
|
|
|
|
size,
|
|
|
|
src,
|
|
|
|
srcSet,
|
|
|
|
icon,
|
|
|
|
className,
|
|
|
|
alt,
|
2020-05-20 19:30:39 +08:00
|
|
|
draggable,
|
2018-12-07 20:02:01 +08:00
|
|
|
...others
|
2017-06-07 11:18:31 +08:00
|
|
|
} = this.props;
|
|
|
|
|
2020-05-14 15:57:04 +08:00
|
|
|
devWarning(
|
2019-12-13 10:18:12 +08:00
|
|
|
!(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`,
|
|
|
|
);
|
|
|
|
|
2019-09-27 14:17:42 +08:00
|
|
|
const { isImgExist, scale, mounted } = this.state;
|
2018-08-02 20:25:14 +08:00
|
|
|
|
2018-12-05 19:12:18 +08:00
|
|
|
const prefixCls = getPrefixCls('avatar', customizePrefixCls);
|
|
|
|
|
2017-06-08 11:44:04 +08:00
|
|
|
const sizeCls = classNames({
|
|
|
|
[`${prefixCls}-lg`]: size === 'large',
|
|
|
|
[`${prefixCls}-sm`]: size === 'small',
|
|
|
|
});
|
2017-06-07 11:18:31 +08:00
|
|
|
|
2017-06-08 11:44:04 +08:00
|
|
|
const classString = classNames(prefixCls, className, sizeCls, {
|
2017-06-07 11:18:31 +08:00
|
|
|
[`${prefixCls}-${shape}`]: shape,
|
2018-08-02 20:25:14 +08:00
|
|
|
[`${prefixCls}-image`]: src && isImgExist,
|
2017-06-07 11:18:31 +08:00
|
|
|
[`${prefixCls}-icon`]: icon,
|
|
|
|
});
|
|
|
|
|
2018-12-07 20:02:01 +08:00
|
|
|
const sizeStyle: React.CSSProperties =
|
|
|
|
typeof size === 'number'
|
|
|
|
? {
|
|
|
|
width: size,
|
|
|
|
height: size,
|
|
|
|
lineHeight: `${size}px`,
|
|
|
|
fontSize: icon ? size / 2 : 18,
|
|
|
|
}
|
|
|
|
: {};
|
2018-08-02 20:25:14 +08:00
|
|
|
|
2019-08-05 18:38:10 +08:00
|
|
|
let { children } = this.props;
|
2018-08-02 20:25:14 +08:00
|
|
|
if (src && isImgExist) {
|
2020-05-20 19:30:39 +08:00
|
|
|
children = (
|
|
|
|
<img
|
|
|
|
src={src}
|
|
|
|
draggable={draggable}
|
|
|
|
srcSet={srcSet}
|
|
|
|
onError={this.handleImgLoadError}
|
|
|
|
alt={alt}
|
|
|
|
/>
|
|
|
|
);
|
2017-06-07 11:18:31 +08:00
|
|
|
} else if (icon) {
|
2019-08-13 14:07:17 +08:00
|
|
|
children = icon;
|
2017-06-07 11:18:31 +08:00
|
|
|
} else {
|
2017-06-08 11:44:04 +08:00
|
|
|
const childrenNode = this.avatarChildren;
|
2018-08-02 20:25:14 +08:00
|
|
|
if (childrenNode || scale !== 1) {
|
2018-10-28 23:45:36 +08:00
|
|
|
const transformString = `scale(${scale}) translateX(-50%)`;
|
2017-06-07 11:18:31 +08:00
|
|
|
const childrenStyle: React.CSSProperties = {
|
2018-10-28 23:45:36 +08:00
|
|
|
msTransform: transformString,
|
|
|
|
WebkitTransform: transformString,
|
|
|
|
transform: transformString,
|
2017-06-07 11:18:31 +08:00
|
|
|
};
|
2019-09-27 14:17:42 +08:00
|
|
|
|
2018-08-02 20:25:14 +08:00
|
|
|
const sizeChildrenStyle: React.CSSProperties =
|
2018-12-07 20:02:01 +08:00
|
|
|
typeof size === 'number'
|
|
|
|
? {
|
|
|
|
lineHeight: `${size}px`,
|
|
|
|
}
|
|
|
|
: {};
|
2017-06-19 12:14:16 +08:00
|
|
|
children = (
|
|
|
|
<span
|
|
|
|
className={`${prefixCls}-string`}
|
2019-03-21 10:07:56 +08:00
|
|
|
ref={(node: HTMLElement) => (this.avatarChildren = node)}
|
2018-08-02 20:25:14 +08:00
|
|
|
style={{ ...sizeChildrenStyle, ...childrenStyle }}
|
2017-06-19 12:14:16 +08:00
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</span>
|
|
|
|
);
|
2017-06-07 11:18:31 +08:00
|
|
|
} else {
|
2019-09-27 14:17:42 +08:00
|
|
|
const childrenStyle: React.CSSProperties = {};
|
|
|
|
if (!mounted) {
|
|
|
|
childrenStyle.opacity = 0;
|
|
|
|
}
|
|
|
|
|
2017-06-19 12:14:16 +08:00
|
|
|
children = (
|
2019-03-21 10:07:56 +08:00
|
|
|
<span
|
|
|
|
className={`${prefixCls}-string`}
|
2019-09-27 14:17:42 +08:00
|
|
|
style={{ opacity: 0 }}
|
2019-03-21 10:07:56 +08:00
|
|
|
ref={(node: HTMLElement) => (this.avatarChildren = node)}
|
|
|
|
>
|
2017-06-19 12:14:16 +08:00
|
|
|
{children}
|
|
|
|
</span>
|
|
|
|
);
|
2017-06-07 11:18:31 +08:00
|
|
|
}
|
|
|
|
}
|
2020-05-27 19:25:28 +08:00
|
|
|
|
|
|
|
// The event is triggered twice from bubbling up the DOM tree.
|
|
|
|
// see https://codesandbox.io/s/kind-snow-9lidz
|
|
|
|
delete others.onError;
|
2020-05-29 18:05:58 +08:00
|
|
|
delete others.gap;
|
2020-05-27 19:25:28 +08:00
|
|
|
|
2017-06-07 11:18:31 +08:00
|
|
|
return (
|
2019-03-21 10:07:56 +08:00
|
|
|
<span
|
2020-05-29 18:05:58 +08:00
|
|
|
{...others}
|
2019-03-21 10:07:56 +08:00
|
|
|
style={{ ...sizeStyle, ...others.style }}
|
|
|
|
className={classString}
|
|
|
|
ref={(node: HTMLElement) => (this.avatarNode = node)}
|
|
|
|
>
|
2017-06-07 11:18:31 +08:00
|
|
|
{children}
|
|
|
|
</span>
|
|
|
|
);
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2018-12-05 19:12:18 +08:00
|
|
|
|
|
|
|
render() {
|
2018-12-07 20:02:01 +08:00
|
|
|
return <ConfigConsumer>{this.renderAvatar}</ConfigConsumer>;
|
2018-12-05 19:12:18 +08:00
|
|
|
}
|
2017-06-07 11:18:31 +08:00
|
|
|
}
|