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