index.tsx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import React from 'react';
  2. import cls from 'classnames';
  3. import PropTypes from 'prop-types';
  4. import { cssClasses, strings } from '@douyinfe/semi-foundation/avatar/constants';
  5. import AvatarFoundation, { AvatarAdapter } from '@douyinfe/semi-foundation/avatar/foundation';
  6. import '@douyinfe/semi-foundation/avatar/avatar.scss';
  7. import { noop } from '@douyinfe/semi-foundation/utils/function';
  8. import BaseComponent from '../_base/baseComponent';
  9. import { AvatarProps } from './interface';
  10. const sizeSet = strings.SIZE;
  11. const shapeSet = strings.SHAPE;
  12. const colorSet = strings.COLOR;
  13. const prefixCls = cssClasses.PREFIX;
  14. export * from './interface';
  15. export interface AvatarState {
  16. isImgExist: boolean;
  17. hoverContent: React.ReactNode;
  18. }
  19. export default class Avatar extends BaseComponent<AvatarProps, AvatarState> {
  20. static defaultProps = {
  21. size: 'medium',
  22. color: 'grey',
  23. shape: 'circle',
  24. onClick: noop,
  25. onMouseEnter: noop,
  26. onMouseLeave: noop,
  27. };
  28. static propTypes = {
  29. children: PropTypes.node,
  30. color: PropTypes.oneOf(colorSet),
  31. shape: PropTypes.oneOf(shapeSet),
  32. size: PropTypes.oneOf(sizeSet),
  33. hoverMask: PropTypes.node,
  34. className: PropTypes.string,
  35. style: PropTypes.object,
  36. imgAttr: PropTypes.object,
  37. src: PropTypes.string,
  38. srcSet: PropTypes.string,
  39. alt: PropTypes.string,
  40. onError: PropTypes.func,
  41. onClick: PropTypes.func,
  42. onMouseEnter: PropTypes.func,
  43. onMouseLeave: PropTypes.func,
  44. };
  45. constructor(props: AvatarProps) {
  46. super(props);
  47. this.state = {
  48. isImgExist: true,
  49. hoverContent: '',
  50. };
  51. this.onEnter = this.onEnter.bind(this);
  52. this.onLeave = this.onLeave.bind(this);
  53. this.handleError = this.handleError.bind(this);
  54. }
  55. get adapter(): AvatarAdapter<AvatarProps, AvatarState> {
  56. return {
  57. ...super.adapter,
  58. notifyImgState: (isImgExist: boolean) => {
  59. this.setState({ isImgExist });
  60. },
  61. notifyEnter: (e: React.MouseEvent) => {
  62. const { hoverMask } = this.props;
  63. const hoverContent = hoverMask;
  64. this.setState({ hoverContent }, () => {
  65. const { onMouseEnter } = this.props;
  66. onMouseEnter && onMouseEnter(e);
  67. });
  68. },
  69. notifyLeave: (e: React.MouseEvent) => {
  70. this.setState({ hoverContent: '' }, () => {
  71. const { onMouseLeave } = this.props;
  72. onMouseLeave && onMouseLeave(e);
  73. });
  74. }
  75. };
  76. }
  77. componentDidMount() {
  78. this.foundation = new AvatarFoundation<AvatarProps, AvatarState>(this.adapter);
  79. this.foundation.init();
  80. }
  81. componentDidUpdate(prevProps: AvatarProps) {
  82. if (this.props.src && this.props.src !== prevProps.src) {
  83. const image = new Image(0, 0);
  84. image.src = this.props.src;
  85. image.onload = () => {
  86. this.setState({ isImgExist: true });
  87. };
  88. image.onerror = () => {
  89. this.setState({ isImgExist: false });
  90. };
  91. image.onabort = () => {
  92. this.setState({ isImgExist: false });
  93. };
  94. }
  95. }
  96. componentWillUnmount() {
  97. this.foundation.destroy();
  98. }
  99. onEnter() {
  100. this.foundation.handleEnter();
  101. }
  102. onLeave() {
  103. this.foundation.handleLeave();
  104. }
  105. handleError() {
  106. this.foundation.handleImgLoadError();
  107. }
  108. render() {
  109. // eslint-disable-next-line max-len, no-unused-vars
  110. const { shape, children, size, color, className, hoverMask, onClick, imgAttr, src, srcSet, style, alt, ...others } = this.props;
  111. const { isImgExist, hoverContent } = this.state;
  112. const isImg = src && isImgExist;
  113. const avtarCls = cls(
  114. prefixCls,
  115. {
  116. [`${prefixCls}-${shape}`]: shape,
  117. [`${prefixCls}-${size}`]: size,
  118. [`${prefixCls}-${color}`]: color && !isImg,
  119. [`${prefixCls}-img`]: isImg,
  120. },
  121. className
  122. );
  123. let content = children;
  124. const hoverRender = hoverContent ? (<div className={`${prefixCls}-hover`}>{hoverContent}</div>) : null;
  125. if (isImg) {
  126. content = (
  127. <img src={src} srcSet={srcSet} onError={this.handleError} alt={alt} {...imgAttr} />
  128. );
  129. } else if (typeof children === 'string') {
  130. content = (
  131. <span className={`${prefixCls}-content`}>
  132. <span className={`${prefixCls}-label`}>{children}</span>
  133. </span>
  134. );
  135. }
  136. return (
  137. <span
  138. {...(others as any)}
  139. style={style}
  140. className={avtarCls}
  141. onClick={onClick as any}
  142. onMouseEnter={this.onEnter as any}
  143. onMouseLeave={this.onLeave as any}
  144. >
  145. {content}
  146. {hoverRender}
  147. </span>
  148. );
  149. }
  150. }