|
|
@ -6,8 +6,11 @@ import classNames from 'classnames';
|
|
|
|
export interface AvatarProps {
|
|
|
|
export interface AvatarProps {
|
|
|
|
/** Shape of avatar, options:`circle`, `square` */
|
|
|
|
/** Shape of avatar, options:`circle`, `square` */
|
|
|
|
shape?: 'circle' | 'square';
|
|
|
|
shape?: 'circle' | 'square';
|
|
|
|
/** Size of avatar, options:`large`, `small`, `default` */
|
|
|
|
/*
|
|
|
|
size?: 'large' | 'small' | 'default';
|
|
|
|
* Size of avatar, options: `large`, `small`, `default`
|
|
|
|
|
|
|
|
* or a custom number size
|
|
|
|
|
|
|
|
* */
|
|
|
|
|
|
|
|
size?: 'large' | 'small' | 'default' | number;
|
|
|
|
/** Src of image avatar */
|
|
|
|
/** Src of image avatar */
|
|
|
|
src?: string;
|
|
|
|
src?: string;
|
|
|
|
/** Type of the Icon to be used in avatar */
|
|
|
|
/** Type of the Icon to be used in avatar */
|
|
|
@ -88,6 +91,8 @@ export default class Avatar extends React.Component<AvatarProps, AvatarState> {
|
|
|
|
prefixCls, shape, size, src, icon, className, alt, ...others
|
|
|
|
prefixCls, shape, size, src, icon, className, alt, ...others
|
|
|
|
} = this.props;
|
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const { isImgExist, scale } = this.state;
|
|
|
|
|
|
|
|
|
|
|
|
const sizeCls = classNames({
|
|
|
|
const sizeCls = classNames({
|
|
|
|
[`${prefixCls}-lg`]: size === 'large',
|
|
|
|
[`${prefixCls}-lg`]: size === 'large',
|
|
|
|
[`${prefixCls}-sm`]: size === 'small',
|
|
|
|
[`${prefixCls}-sm`]: size === 'small',
|
|
|
@ -95,12 +100,19 @@ export default class Avatar extends React.Component<AvatarProps, AvatarState> {
|
|
|
|
|
|
|
|
|
|
|
|
const classString = classNames(prefixCls, className, sizeCls, {
|
|
|
|
const classString = classNames(prefixCls, className, sizeCls, {
|
|
|
|
[`${prefixCls}-${shape}`]: shape,
|
|
|
|
[`${prefixCls}-${shape}`]: shape,
|
|
|
|
[`${prefixCls}-image`]: src && this.state.isImgExist,
|
|
|
|
[`${prefixCls}-image`]: src && isImgExist,
|
|
|
|
[`${prefixCls}-icon`]: icon,
|
|
|
|
[`${prefixCls}-icon`]: icon,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const sizeStyle: React.CSSProperties = typeof size === 'number' ? {
|
|
|
|
|
|
|
|
width: size,
|
|
|
|
|
|
|
|
height: size,
|
|
|
|
|
|
|
|
lineHeight: `${size}px`,
|
|
|
|
|
|
|
|
fontSize: icon ? size / 2 : 18,
|
|
|
|
|
|
|
|
} : {};
|
|
|
|
|
|
|
|
|
|
|
|
let children = this.props.children;
|
|
|
|
let children = this.props.children;
|
|
|
|
if (src && this.state.isImgExist) {
|
|
|
|
if (src && isImgExist) {
|
|
|
|
children = (
|
|
|
|
children = (
|
|
|
|
<img
|
|
|
|
<img
|
|
|
|
src={src}
|
|
|
|
src={src}
|
|
|
@ -112,20 +124,24 @@ export default class Avatar extends React.Component<AvatarProps, AvatarState> {
|
|
|
|
children = <Icon type={icon} />;
|
|
|
|
children = <Icon type={icon} />;
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
const childrenNode = this.avatarChildren;
|
|
|
|
const childrenNode = this.avatarChildren;
|
|
|
|
if (childrenNode || this.state.scale !== 1) {
|
|
|
|
if (childrenNode || scale !== 1) {
|
|
|
|
const childrenStyle: React.CSSProperties = {
|
|
|
|
const childrenStyle: React.CSSProperties = {
|
|
|
|
msTransform: `scale(${this.state.scale})`,
|
|
|
|
msTransform: `scale(${scale})`,
|
|
|
|
WebkitTransform: `scale(${this.state.scale})`,
|
|
|
|
WebkitTransform: `scale(${scale})`,
|
|
|
|
transform: `scale(${this.state.scale})`,
|
|
|
|
transform: `scale(${scale})`,
|
|
|
|
position: 'absolute',
|
|
|
|
position: 'absolute',
|
|
|
|
display: 'inline-block',
|
|
|
|
display: 'inline-block',
|
|
|
|
left: `calc(50% - ${Math.round(childrenNode.offsetWidth / 2)}px)`,
|
|
|
|
left: `calc(50% - ${Math.round(childrenNode.offsetWidth / 2)}px)`,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const sizeChildrenStyle: React.CSSProperties =
|
|
|
|
|
|
|
|
typeof size === 'number' ? {
|
|
|
|
|
|
|
|
lineHeight: `${size}px`,
|
|
|
|
|
|
|
|
} : {};
|
|
|
|
children = (
|
|
|
|
children = (
|
|
|
|
<span
|
|
|
|
<span
|
|
|
|
className={`${prefixCls}-string`}
|
|
|
|
className={`${prefixCls}-string`}
|
|
|
|
ref={span => this.avatarChildren = span}
|
|
|
|
ref={span => this.avatarChildren = span}
|
|
|
|
style={childrenStyle}
|
|
|
|
style={{ ...sizeChildrenStyle, ...childrenStyle }}
|
|
|
|
>
|
|
|
|
>
|
|
|
|
{children}
|
|
|
|
{children}
|
|
|
|
</span>
|
|
|
|
</span>
|
|
|
@ -142,7 +158,11 @@ export default class Avatar extends React.Component<AvatarProps, AvatarState> {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
return (
|
|
|
|
<span {...others} className={classString}>
|
|
|
|
<span
|
|
|
|
|
|
|
|
{...others}
|
|
|
|
|
|
|
|
style={{ ...sizeStyle, ...others.style }}
|
|
|
|
|
|
|
|
className={classString}
|
|
|
|
|
|
|
|
>
|
|
|
|
{children}
|
|
|
|
{children}
|
|
|
|
</span>
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
);
|
|
|
|