ant-design/components/avatar/index.tsx

171 lines
4.4 KiB
TypeScript
Raw Normal View History

import * as React from 'react';
import * as ReactDOM from 'react-dom';
2017-05-28 14:20:59 +08:00
import Icon from '../icon';
import classNames from 'classnames';
export interface AvatarProps {
/** Shape of avatar, options:`circle`, `square` */
shape?: 'circle' | 'square';
/*
* Size of avatar, options: `large`, `small`, `default`
* or a custom number size
* */
size?: 'large' | 'small' | 'default' | number;
2017-05-28 14:20:59 +08:00
/** Src of image avatar */
src?: string;
/** Type of the Icon to be used in avatar */
icon?: string;
style?: React.CSSProperties;
prefixCls?: string;
className?: string;
children?: any;
2018-06-07 11:36:16 +08:00
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;
2017-05-28 14:20:59 +08:00
}
2017-07-31 00:43:33 +08:00
export interface AvatarState {
scale: number;
isImgExist: boolean;
}
export default class Avatar extends React.Component<AvatarProps, AvatarState> {
2017-06-07 11:18:31 +08:00
static defaultProps = {
prefixCls: 'ant-avatar',
shape: 'circle',
size: 'default',
};
2017-06-08 11:44:04 +08:00
private avatarChildren: any;
2017-05-28 14:20:59 +08:00
2017-07-31 00:43:33 +08:00
constructor(props: AvatarProps) {
2017-06-07 11:18:31 +08:00
super(props);
this.state = {
scale: 1,
isImgExist: true,
2017-06-07 11:18:31 +08:00
};
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();
}
2017-07-31 00:43:33 +08:00
componentDidUpdate(prevProps: AvatarProps, prevState: AvatarState) {
2017-06-08 11:44:04 +08:00
if (prevProps.children !== this.props.children
2018-04-22 13:09:42 +08:00
|| (prevState.scale !== this.state.scale && this.state.scale === 1)
|| (prevState.isImgExist !== this.state.isImgExist)) {
2017-06-08 11:44:04 +08:00
this.setScale();
}
}
setScale = () => {
const childrenNode = this.avatarChildren;
2017-06-07 11:18:31 +08:00
if (childrenNode) {
const childrenWidth = childrenNode.offsetWidth;
2018-04-11 12:01:28 +08:00
const avatarNode = ReactDOM.findDOMNode(this) as Element;
const avatarWidth = avatarNode.getBoundingClientRect().width;
2017-06-07 11:18:31 +08:00
// add 4px gap for each side to get better performance
if (avatarWidth - 8 < childrenWidth) {
this.setState({
scale: (avatarWidth - 8) / childrenWidth,
});
2017-06-08 11:44:04 +08:00
} else {
this.setState({
scale: 1,
});
2017-06-07 11:18:31 +08:00
}
}
2017-05-28 14:20:59 +08:00
}
handleImgLoadError = () => {
const { onError } = this.props;
const errorFlag = onError ? onError() : undefined;
if (errorFlag !== false) {
this.setState({ isImgExist: false });
}
}
2017-06-07 11:18:31 +08:00
render() {
const {
2018-06-07 11:36:16 +08:00
prefixCls, shape, size, src, icon, className, alt, ...others
2017-06-07 11:18:31 +08:00
} = this.props;
const { isImgExist, scale } = this.state;
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,
[`${prefixCls}-image`]: src && isImgExist,
2017-06-07 11:18:31 +08:00
[`${prefixCls}-icon`]: icon,
});
const sizeStyle: React.CSSProperties = typeof size === 'number' ? {
width: size,
height: size,
lineHeight: `${size}px`,
fontSize: icon ? size / 2 : 18,
} : {};
2017-06-07 11:18:31 +08:00
let children = this.props.children;
if (src && isImgExist) {
children = (
<img
src={src}
onError={this.handleImgLoadError}
2018-06-07 11:36:16 +08:00
alt={alt}
/>
);
2017-06-07 11:18:31 +08:00
} else if (icon) {
children = <Icon type={icon} />;
} else {
2017-06-08 11:44:04 +08:00
const childrenNode = this.avatarChildren;
if (childrenNode || scale !== 1) {
2017-06-07 11:18:31 +08:00
const childrenStyle: React.CSSProperties = {
msTransform: `scale(${scale})`,
WebkitTransform: `scale(${scale})`,
transform: `scale(${scale})`,
2017-06-07 11:18:31 +08:00
position: 'absolute',
display: 'inline-block',
2017-06-08 11:44:04 +08:00
left: `calc(50% - ${Math.round(childrenNode.offsetWidth / 2)}px)`,
2017-06-07 11:18:31 +08:00
};
const sizeChildrenStyle: React.CSSProperties =
typeof size === 'number' ? {
lineHeight: `${size}px`,
} : {};
2017-06-19 12:14:16 +08:00
children = (
<span
className={`${prefixCls}-string`}
ref={span => this.avatarChildren = span}
style={{ ...sizeChildrenStyle, ...childrenStyle }}
2017-06-19 12:14:16 +08:00
>
{children}
</span>
);
2017-06-07 11:18:31 +08:00
} else {
2017-06-19 12:14:16 +08:00
children = (
<span
className={`${prefixCls}-string`}
ref={span => this.avatarChildren = span}
>
{children}
</span>
);
2017-06-07 11:18:31 +08:00
}
}
return (
<span
{...others}
style={{ ...sizeStyle, ...others.style }}
className={classString}
>
2017-06-07 11:18:31 +08:00
{children}
</span>
);
}
}