index.tsx 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. import { handlePrevent } from '@douyinfe/semi-foundation/utils/a11y';
  11. const sizeSet = strings.SIZE;
  12. const shapeSet = strings.SHAPE;
  13. const colorSet = strings.COLOR;
  14. const prefixCls = cssClasses.PREFIX;
  15. export * from './interface';
  16. export interface AvatarState {
  17. isImgExist: boolean;
  18. hoverContent: React.ReactNode;
  19. focusVisible: boolean;
  20. }
  21. export default class Avatar extends BaseComponent<AvatarProps, AvatarState> {
  22. static defaultProps = {
  23. size: 'medium',
  24. color: 'grey',
  25. shape: 'circle',
  26. onClick: noop,
  27. onMouseEnter: noop,
  28. onMouseLeave: noop,
  29. };
  30. static propTypes = {
  31. children: PropTypes.node,
  32. color: PropTypes.oneOf(colorSet),
  33. shape: PropTypes.oneOf(shapeSet),
  34. size: PropTypes.oneOf(sizeSet),
  35. hoverMask: PropTypes.node,
  36. className: PropTypes.string,
  37. style: PropTypes.object,
  38. imgAttr: PropTypes.object,
  39. src: PropTypes.string,
  40. srcSet: PropTypes.string,
  41. alt: PropTypes.string,
  42. onError: PropTypes.func,
  43. onClick: PropTypes.func,
  44. onMouseEnter: PropTypes.func,
  45. onMouseLeave: PropTypes.func,
  46. };
  47. foundation!: AvatarFoundation;
  48. constructor(props: AvatarProps) {
  49. super(props);
  50. this.state = {
  51. isImgExist: true,
  52. hoverContent: '',
  53. focusVisible: false,
  54. };
  55. this.onEnter = this.onEnter.bind(this);
  56. this.onLeave = this.onLeave.bind(this);
  57. this.handleError = this.handleError.bind(this);
  58. this.handleKeyDown = this.handleKeyDown.bind(this);
  59. this.getContent = this.getContent.bind(this);
  60. }
  61. get adapter(): AvatarAdapter<AvatarProps, AvatarState> {
  62. return {
  63. ...super.adapter,
  64. notifyImgState: (isImgExist: boolean) => {
  65. this.setState({ isImgExist });
  66. },
  67. notifyEnter: (e: React.MouseEvent) => {
  68. const { hoverMask } = this.props;
  69. const hoverContent = hoverMask;
  70. this.setState({ hoverContent }, () => {
  71. const { onMouseEnter } = this.props;
  72. onMouseEnter && onMouseEnter(e);
  73. });
  74. },
  75. notifyLeave: (e: React.MouseEvent) => {
  76. this.setState({ hoverContent: '' }, () => {
  77. const { onMouseLeave } = this.props;
  78. onMouseLeave && onMouseLeave(e);
  79. });
  80. },
  81. setFocusVisible: (focusVisible: boolean): void => {
  82. this.setState({ focusVisible });
  83. },
  84. };
  85. }
  86. componentDidMount() {
  87. this.foundation = new AvatarFoundation<AvatarProps, AvatarState>(this.adapter);
  88. this.foundation.init();
  89. }
  90. componentDidUpdate(prevProps: AvatarProps) {
  91. if (this.props.src && this.props.src !== prevProps.src) {
  92. const image = new Image(0, 0);
  93. image.src = this.props.src;
  94. image.onload = () => {
  95. this.setState({ isImgExist: true });
  96. };
  97. image.onerror = () => {
  98. this.setState({ isImgExist: false });
  99. };
  100. image.onabort = () => {
  101. this.setState({ isImgExist: false });
  102. };
  103. }
  104. }
  105. componentWillUnmount() {
  106. this.foundation.destroy();
  107. }
  108. onEnter(e: React.MouseEvent) {
  109. this.foundation.handleEnter(e);
  110. }
  111. onLeave(e: React.MouseEvent) {
  112. this.foundation.handleLeave(e);
  113. }
  114. handleError() {
  115. this.foundation.handleImgLoadError();
  116. }
  117. handleKeyDown(event: any) {
  118. const { onClick } = this.props;
  119. switch (event.key) {
  120. case "Enter":
  121. onClick(event);
  122. handlePrevent(event);
  123. break;
  124. case 'Escape':
  125. event.target.blur();
  126. break;
  127. default:
  128. break;
  129. }
  130. }
  131. handleFocusVisible = (event: React.FocusEvent) => {
  132. this.foundation.handleFocusVisible(event);
  133. }
  134. handleBlur = (event: React.FocusEvent) => {
  135. this.foundation.handleBlur();
  136. }
  137. getContent = () => {
  138. const { children, onClick, imgAttr, src, srcSet, alt } = this.props;
  139. const { isImgExist } = this.state;
  140. let content = children;
  141. const clickable = onClick !== noop;
  142. const isImg = src && isImgExist;
  143. const a11yFocusProps = {
  144. tabIndex: 0,
  145. onKeyDown: this.handleKeyDown,
  146. onFocus: this.handleFocusVisible,
  147. onBlur: this.handleBlur,
  148. };
  149. if (isImg) {
  150. const finalAlt = clickable ? `clickable Avatar: ${alt}` : alt;
  151. const imgBasicProps = {
  152. src,
  153. srcSet,
  154. onError: this.handleError,
  155. ...imgAttr,
  156. className: cls({
  157. [`${prefixCls}-no-focus-visible`]: clickable,
  158. }),
  159. };
  160. const imgProps = clickable ? { ...imgBasicProps, ...a11yFocusProps } : imgBasicProps;
  161. content = (
  162. <img alt={finalAlt} {...imgProps}/>
  163. );
  164. } else if (typeof children === 'string') {
  165. const tempAlt = alt ?? children;
  166. const finalAlt = clickable ? `clickable Avatar: ${tempAlt}` : tempAlt;
  167. const props = {
  168. role: 'img',
  169. 'aria-label': finalAlt,
  170. className: cls(`${prefixCls}-label`,
  171. {
  172. [`${prefixCls}-no-focus-visible`]: clickable,
  173. }
  174. ),
  175. };
  176. const finalProps = clickable ? { ...props, ...a11yFocusProps } : props;
  177. content = (
  178. <span className={`${prefixCls}-content`}>
  179. <span {...finalProps} x-semi-prop="children">{children}</span>
  180. </span>
  181. );
  182. }
  183. return content;
  184. }
  185. render() {
  186. // eslint-disable-next-line max-len, no-unused-vars
  187. const { shape, children, size, color, className, hoverMask, onClick, imgAttr, src, srcSet, style, alt, ...others } = this.props;
  188. const { isImgExist, hoverContent, focusVisible } = this.state;
  189. const isImg = src && isImgExist;
  190. const avatarCls = cls(
  191. prefixCls,
  192. {
  193. [`${prefixCls}-${shape}`]: shape,
  194. [`${prefixCls}-${size}`]: size,
  195. [`${prefixCls}-${color}`]: color && !isImg,
  196. [`${prefixCls}-img`]: isImg,
  197. [`${prefixCls}-focus`]: focusVisible,
  198. },
  199. className
  200. );
  201. const hoverRender = hoverContent ? (<div className={`${prefixCls}-hover`} x-semi-prop="hoverContent">{hoverContent}</div>) : null;
  202. return (
  203. <span
  204. {...(others as any)}
  205. style={style}
  206. className={avatarCls}
  207. onClick={onClick as any}
  208. onMouseEnter={this.onEnter as any}
  209. onMouseLeave={this.onLeave as any}
  210. role='listitem'
  211. >
  212. {this.getContent()}
  213. {hoverRender}
  214. </span>
  215. );
  216. }
  217. }