You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
3.2 KiB

8 years ago
import React from 'react';
8 years ago
import ReactDOM from 'react-dom';
8 years ago
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` */
size?: 'large' | 'small' | 'default';
/** 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;
}
8 years ago
export default class Avatar extends React.Component<AvatarProps, any> {
static defaultProps = {
prefixCls: 'ant-avatar',
shape: 'circle',
size: 'default',
};
8 years ago
private avatarChildren: any;
8 years ago
8 years ago
constructor(props) {
super(props);
this.state = {
scale: 1,
};
8 years ago
}
8 years ago
componentDidMount() {
8 years ago
this.setScale();
}
componentDidUpdate(prevProps, prevState) {
if (prevProps.children !== this.props.children
|| (prevState.scale !== this.state.scale && this.state.scale === 1)) {
this.setScale();
}
}
setScale = () => {
const childrenNode = this.avatarChildren;
8 years ago
if (childrenNode) {
const childrenWidth = childrenNode.offsetWidth;
const avatarWidth = ReactDOM.findDOMNode(this).getBoundingClientRect().width;
// add 4px gap for each side to get better performance
if (avatarWidth - 8 < childrenWidth) {
this.setState({
scale: (avatarWidth - 8) / childrenWidth,
});
8 years ago
} else {
this.setState({
scale: 1,
});
8 years ago
}
}
8 years ago
}
8 years ago
render() {
const {
prefixCls, shape, size, src, icon, className, ...others,
} = this.props;
8 years ago
const sizeCls = classNames({
[`${prefixCls}-lg`]: size === 'large',
[`${prefixCls}-sm`]: size === 'small',
});
8 years ago
8 years ago
const classString = classNames(prefixCls, className, sizeCls, {
8 years ago
[`${prefixCls}-${shape}`]: shape,
[`${prefixCls}-image`]: src,
[`${prefixCls}-icon`]: icon,
});
let children = this.props.children;
if (src) {
children = <img src={src} />;
} else if (icon) {
children = <Icon type={icon} />;
} else {
8 years ago
const childrenNode = this.avatarChildren;
if (childrenNode || this.state.scale !== 1) {
8 years ago
const childrenStyle: React.CSSProperties = {
8 years ago
msTransform: `scale(${this.state.scale})`,
WebkitTransform: `scale(${this.state.scale})`,
8 years ago
transform: `scale(${this.state.scale})`,
position: 'absolute',
display: 'inline-block',
8 years ago
left: `calc(50% - ${Math.round(childrenNode.offsetWidth / 2)}px)`,
8 years ago
};
children = (
<span
className={`${prefixCls}-string`}
ref={span => this.avatarChildren = span}
style={childrenStyle}
>
{children}
</span>
);
8 years ago
} else {
children = (
<span
className={`${prefixCls}-string`}
ref={span => this.avatarChildren = span}
>
{children}
</span>
);
8 years ago
}
}
return (
<span {...others} className={classString}>
{children}
</span>
);
}
}