1
0

index.tsx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. foundation!: AvatarFoundation;
  46. constructor(props: AvatarProps) {
  47. super(props);
  48. this.state = {
  49. isImgExist: true,
  50. hoverContent: '',
  51. };
  52. this.onEnter = this.onEnter.bind(this);
  53. this.onLeave = this.onLeave.bind(this);
  54. this.handleError = this.handleError.bind(this);
  55. }
  56. get adapter(): AvatarAdapter<AvatarProps, AvatarState> {
  57. return {
  58. ...super.adapter,
  59. notifyImgState: (isImgExist: boolean) => {
  60. this.setState({ isImgExist });
  61. },
  62. notifyEnter: (e: React.MouseEvent) => {
  63. const { hoverMask } = this.props;
  64. const hoverContent = hoverMask;
  65. this.setState({ hoverContent }, () => {
  66. const { onMouseEnter } = this.props;
  67. onMouseEnter && onMouseEnter(e);
  68. });
  69. },
  70. notifyLeave: (e: React.MouseEvent) => {
  71. this.setState({ hoverContent: '' }, () => {
  72. const { onMouseLeave } = this.props;
  73. onMouseLeave && onMouseLeave(e);
  74. });
  75. }
  76. };
  77. }
  78. componentDidMount() {
  79. this.foundation = new AvatarFoundation<AvatarProps, AvatarState>(this.adapter);
  80. this.foundation.init();
  81. }
  82. componentDidUpdate(prevProps: AvatarProps) {
  83. if (this.props.src && this.props.src !== prevProps.src) {
  84. const image = new Image(0, 0);
  85. image.src = this.props.src;
  86. image.onload = () => {
  87. this.setState({ isImgExist: true });
  88. };
  89. image.onerror = () => {
  90. this.setState({ isImgExist: false });
  91. };
  92. image.onabort = () => {
  93. this.setState({ isImgExist: false });
  94. };
  95. }
  96. }
  97. componentWillUnmount() {
  98. this.foundation.destroy();
  99. }
  100. onEnter(e: React.MouseEvent) {
  101. this.foundation.handleEnter(e);
  102. }
  103. onLeave(e: React.MouseEvent) {
  104. this.foundation.handleLeave(e);
  105. }
  106. handleError() {
  107. this.foundation.handleImgLoadError();
  108. }
  109. render() {
  110. // eslint-disable-next-line max-len, no-unused-vars
  111. const { shape, children, size, color, className, hoverMask, onClick, imgAttr, src, srcSet, style, alt, ...others } = this.props;
  112. const { isImgExist, hoverContent } = this.state;
  113. const isImg = src && isImgExist;
  114. const avatarCls = cls(
  115. prefixCls,
  116. {
  117. [`${prefixCls}-${shape}`]: shape,
  118. [`${prefixCls}-${size}`]: size,
  119. [`${prefixCls}-${color}`]: color && !isImg,
  120. [`${prefixCls}-img`]: isImg,
  121. },
  122. className
  123. );
  124. let content = children;
  125. const hoverRender = hoverContent ? (<div className={`${prefixCls}-hover`}>{hoverContent}</div>) : null;
  126. if (isImg) {
  127. content = (
  128. <img src={src} srcSet={srcSet} onError={this.handleError} alt={alt} {...imgAttr} />
  129. );
  130. } else if (typeof children === 'string') {
  131. content = (
  132. <span className={`${prefixCls}-content`}>
  133. <span className={`${prefixCls}-label`}>{children}</span>
  134. </span>
  135. );
  136. }
  137. return (
  138. // eslint-disable-next-line jsx-a11y/no-static-element-interactions,jsx-a11y/click-events-have-key-events
  139. <span
  140. {...(others as any)}
  141. style={style}
  142. className={avatarCls}
  143. onClick={onClick as any}
  144. onMouseEnter={this.onEnter as any}
  145. onMouseLeave={this.onLeave as any}
  146. >
  147. {content}
  148. {hoverRender}
  149. </span>
  150. );
  151. }
  152. }