ant-design/components/avatar/index.tsx

234 lines
6.0 KiB
TypeScript
Raw Normal View History

import * as React from 'react';
2017-05-28 14:20:59 +08:00
import classNames from 'classnames';
use ant design icons 4.0 (#18217) * feat: use @ant-design/icons@4.0 * feat: use createFromIconfontCN to make site works * feat: update doc for Icon * feat: use icon in component Alert * feat: use icon in component Avatar * feat: use icon in component Breadcrumb * feat: use icon in component Button * feat: use icon in component Cascader * feat: use icon in component Collapse * feat: use icon in component Datepicker * feat: use icon in component Dropdown * feat: use icon in component Form * feat: use icon in component Input * feat: use icon in component InputNumber * feat: use icon in component Layout * feat: use icon in component Mention * feat: use icon in component Message * feat: use icon in component Modal * feat: use icon in component Notification * feat: use icon in component PageHeader * feat: use icon in component Pagination * feat: use icon in component Popconfirm * feat: use icon in component Progress * feat: use icon in component Rate * feat: use icon in component Result * feat: use icon in component Select * feat: use icon in component Step * feat: use icon in component Switch * feat: use icon in component Table * feat: use icon in component Tab * feat: use icon in component Tag * feat: handle rest component which using Icon * fix: remove unused vars * feat: use latest alpha ant design icons * fix: failed test in uploadlist.test.js * test: update snapshot for icons * doc: add Icon for site * doc: use @ant-design/icons in site * chore: use latest icons * fix: tslint issue * fix: test cases * fix: types for react * fix: lint rules for import orders * fix: use @ant-design/icons@4.0.0-alpha.5 to avoid insert css in server render * fix: eslint error in demo/**.md * inject antd icons * update snapshot * fix site * doc: update docs * fix: code snippets icon in site * feat: use latest @ant-design/icons * fix: icon props in message
2019-08-13 14:07:17 +08:00
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
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';
/*
* Size of avatar, options: `large`, `small`, `default`
* or a custom number size
* */
size?: 'large' | 'small' | 'default' | number;
gap?: number;
2017-05-28 14:20:59 +08:00
/** Src of image avatar */
src?: string;
/** Srcset of image avatar */
srcSet?: string;
draggable?: boolean;
use ant design icons 4.0 (#18217) * feat: use @ant-design/icons@4.0 * feat: use createFromIconfontCN to make site works * feat: update doc for Icon * feat: use icon in component Alert * feat: use icon in component Avatar * feat: use icon in component Breadcrumb * feat: use icon in component Button * feat: use icon in component Cascader * feat: use icon in component Collapse * feat: use icon in component Datepicker * feat: use icon in component Dropdown * feat: use icon in component Form * feat: use icon in component Input * feat: use icon in component InputNumber * feat: use icon in component Layout * feat: use icon in component Mention * feat: use icon in component Message * feat: use icon in component Modal * feat: use icon in component Notification * feat: use icon in component PageHeader * feat: use icon in component Pagination * feat: use icon in component Popconfirm * feat: use icon in component Progress * feat: use icon in component Rate * feat: use icon in component Result * feat: use icon in component Select * feat: use icon in component Step * feat: use icon in component Switch * feat: use icon in component Table * feat: use icon in component Tab * feat: use icon in component Tag * feat: handle rest component which using Icon * fix: remove unused vars * feat: use latest alpha ant design icons * fix: failed test in uploadlist.test.js * test: update snapshot for icons * doc: add Icon for site * doc: use @ant-design/icons in site * chore: use latest icons * fix: tslint issue * fix: test cases * fix: types for react * fix: lint rules for import orders * fix: use @ant-design/icons@4.0.0-alpha.5 to avoid insert css in server render * fix: eslint error in demo/**.md * inject antd icons * update snapshot * fix site * doc: update docs * fix: code snippets icon in site * feat: use latest @ant-design/icons * fix: icon props in message
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;
children?: React.ReactNode;
2018-06-07 11:36:16 +08:00
alt?: string;
/* 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 */
onError?: () => boolean;
2017-05-28 14:20:59 +08:00
}
2017-07-31 00:43:33 +08:00
export interface AvatarState {
scale: number;
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 = {
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,
mounted: false,
2018-11-06 16:23:53 +08:00
isImgExist: true,
};
2017-05-28 14:20:59 +08:00
private avatarNode: HTMLElement;
2019-08-05 18:38:10 +08:00
private avatarChildren: HTMLElement;
2019-08-05 18:38:10 +08:00
private lastChildrenWidth: number;
2019-08-05 18:38:10 +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();
this.setState({ mounted: true });
2017-06-08 11:44:04 +08:00
}
componentDidUpdate(prevProps: AvatarProps) {
if (prevProps.src !== this.props.src) {
// eslint-disable-next-line react/no-did-update-set-state
this.setState({ isImgExist: true, scale: 1 });
}
if (prevProps.children !== this.props.children || prevProps.gap !== this.props.gap) {
this.setScale();
}
2017-06-08 11:44:04 +08:00
}
setScale = () => {
if (!this.avatarChildren || !this.avatarNode) {
return;
}
const childrenWidth = this.avatarChildren.offsetWidth; // offsetWidth avoid affecting be transform scale
const nodeWidth = this.avatarNode.offsetWidth;
const { gap = 4 } = this.props;
// denominator is 0 is no meaning
if (
childrenWidth !== 0 &&
nodeWidth !== 0 &&
(this.lastChildrenWidth !== childrenWidth || this.lastNodeWidth !== nodeWidth)
) {
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
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
};
renderAvatar = ({ getPrefixCls }: ConfigConsumerProps) => {
2017-06-07 11:18:31 +08:00
const {
prefixCls: customizePrefixCls,
2018-12-07 20:02:01 +08:00
shape,
size,
src,
srcSet,
icon,
className,
alt,
draggable,
2018-12-07 20:02:01 +08:00
...others
2017-06-07 11:18:31 +08:00
} = this.props;
devWarning(
!(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`,
);
const { isImgExist, scale, mounted } = this.state;
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,
[`${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,
}
: {};
2019-08-05 18:38:10 +08:00
let { children } = this.props;
if (src && isImgExist) {
children = (
<img
src={src}
draggable={draggable}
srcSet={srcSet}
onError={this.handleImgLoadError}
alt={alt}
/>
);
2017-06-07 11:18:31 +08:00
} else if (icon) {
use ant design icons 4.0 (#18217) * feat: use @ant-design/icons@4.0 * feat: use createFromIconfontCN to make site works * feat: update doc for Icon * feat: use icon in component Alert * feat: use icon in component Avatar * feat: use icon in component Breadcrumb * feat: use icon in component Button * feat: use icon in component Cascader * feat: use icon in component Collapse * feat: use icon in component Datepicker * feat: use icon in component Dropdown * feat: use icon in component Form * feat: use icon in component Input * feat: use icon in component InputNumber * feat: use icon in component Layout * feat: use icon in component Mention * feat: use icon in component Message * feat: use icon in component Modal * feat: use icon in component Notification * feat: use icon in component PageHeader * feat: use icon in component Pagination * feat: use icon in component Popconfirm * feat: use icon in component Progress * feat: use icon in component Rate * feat: use icon in component Result * feat: use icon in component Select * feat: use icon in component Step * feat: use icon in component Switch * feat: use icon in component Table * feat: use icon in component Tab * feat: use icon in component Tag * feat: handle rest component which using Icon * fix: remove unused vars * feat: use latest alpha ant design icons * fix: failed test in uploadlist.test.js * test: update snapshot for icons * doc: add Icon for site * doc: use @ant-design/icons in site * chore: use latest icons * fix: tslint issue * fix: test cases * fix: types for react * fix: lint rules for import orders * fix: use @ant-design/icons@4.0.0-alpha.5 to avoid insert css in server render * fix: eslint error in demo/**.md * inject antd icons * update snapshot * fix site * doc: update docs * fix: code snippets icon in site * feat: use latest @ant-design/icons * fix: icon props in message
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;
if (childrenNode || scale !== 1) {
const transformString = `scale(${scale}) translateX(-50%)`;
2017-06-07 11:18:31 +08:00
const childrenStyle: React.CSSProperties = {
msTransform: transformString,
WebkitTransform: transformString,
transform: transformString,
2017-06-07 11:18:31 +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`}
ref={(node: HTMLElement) => {
this.avatarChildren = node;
}}
style={{ ...sizeChildrenStyle, ...childrenStyle }}
2017-06-19 12:14:16 +08:00
>
{children}
</span>
);
2017-06-07 11:18:31 +08:00
} else {
const childrenStyle: React.CSSProperties = {};
if (!mounted) {
childrenStyle.opacity = 0;
}
2017-06-19 12:14:16 +08:00
children = (
<span
className={`${prefixCls}-string`}
style={{ opacity: 0 }}
ref={(node: HTMLElement) => {
this.avatarChildren = node;
}}
>
2017-06-19 12:14:16 +08:00
{children}
</span>
);
2017-06-07 11:18:31 +08:00
}
}
// The event is triggered twice from bubbling up the DOM tree.
// see https://codesandbox.io/s/kind-snow-9lidz
delete others.onError;
delete others.gap;
2017-06-07 11:18:31 +08:00
return (
<span
{...others}
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
};
render() {
2018-12-07 20:02:01 +08:00
return <ConfigConsumer>{this.renderAvatar}</ConfigConsumer>;
}
2017-06-07 11:18:31 +08:00
}