toast.tsx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import React, { CSSProperties } from 'react';
  2. import cls from 'classnames';
  3. import PropTypes from 'prop-types';
  4. import ConfigContext, { ContextValue } from '../configProvider/context';
  5. import ToastFoundation, { ToastAdapter, ToastState, ToastProps } from '@douyinfe/semi-foundation/toast/toastFoundation';
  6. import { numbers, cssClasses, strings } from '@douyinfe/semi-foundation/toast/constants';
  7. import BaseComponent from '../_base/baseComponent';
  8. import Button from '../iconButton/index';
  9. import { IconClose, IconAlertTriangle, IconInfoCircle, IconTickCircle, IconAlertCircle } from '@douyinfe/semi-icons';
  10. import { noop } from 'lodash';
  11. import { getDefaultPropsFromGlobalConfig, isSemiIcon } from '../_utils';
  12. const prefixCls = cssClasses.PREFIX;
  13. export interface ToastReactProps extends ToastProps {
  14. style?: CSSProperties;
  15. icon?: React.ReactNode;
  16. content: React.ReactNode;
  17. stack?: boolean;
  18. stackExpanded?: boolean;
  19. onAnimationEnd?: (e: React.AnimationEvent) => void;
  20. onAnimationStart?: (e: React.AnimationEvent) => void;
  21. positionInList?: {
  22. index: number;
  23. length: number
  24. }
  25. }
  26. class Toast extends BaseComponent<ToastReactProps, ToastState> {
  27. toastEle: React.RefObject<HTMLDivElement> = React.createRef();
  28. static contextType = ConfigContext;
  29. static propTypes = {
  30. onClose: PropTypes.func,
  31. content: PropTypes.node,
  32. close: PropTypes.func,
  33. duration: PropTypes.number,
  34. theme: PropTypes.oneOf(strings.themes),
  35. type: PropTypes.oneOf(strings.types),
  36. textMaxWidth: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
  37. style: PropTypes.object,
  38. className: PropTypes.string,
  39. showClose: PropTypes.bool,
  40. stack: PropTypes.bool,
  41. stackExpanded: PropTypes.bool,
  42. icon: PropTypes.node,
  43. direction: PropTypes.oneOf(strings.directions),
  44. };
  45. static __SemiComponentName__ = "Toast";
  46. static defaultProps = getDefaultPropsFromGlobalConfig(Toast.__SemiComponentName__, {
  47. onClose: noop,
  48. content: '',
  49. close: noop,
  50. duration: numbers.duration,
  51. textMaxWidth: 450,
  52. showClose: true,
  53. stack: false,
  54. stackExpanded: false,
  55. theme: 'normal'
  56. })
  57. constructor(props: ToastReactProps) {
  58. super(props);
  59. this.state = {};
  60. this.foundation = new ToastFoundation(this.adapter);
  61. }
  62. context: ContextValue;
  63. get adapter(): ToastAdapter {
  64. return {
  65. ...super.adapter,
  66. notifyWrapperToRemove: (id: string) => {
  67. this.props.close(id);
  68. },
  69. notifyClose: () => {
  70. this.props.onClose();
  71. },
  72. };
  73. }
  74. componentDidMount() {
  75. this.foundation.init();
  76. }
  77. componentWillUnmount() {
  78. this.foundation.destroy();
  79. }
  80. close(e: React.MouseEvent) {
  81. this.foundation.close(e);
  82. }
  83. clearCloseTimer = () => {
  84. this.foundation.clearCloseTimer_();
  85. };
  86. startCloseTimer = () => {
  87. this.foundation.startCloseTimer_();
  88. };
  89. restartCloseTimer = () => {
  90. this.foundation.restartCloseTimer();
  91. }
  92. renderIcon() {
  93. const { type, icon } = this.props;
  94. const iconMap = {
  95. warning: <IconAlertTriangle />,
  96. success: <IconTickCircle />,
  97. info: <IconInfoCircle />,
  98. error: <IconAlertCircle />
  99. };
  100. const iconType = iconMap[type];
  101. const iconSize = 'large';
  102. const iconCls = cls(`${prefixCls}-icon`, `${prefixCls}-icon-${type}`);
  103. if (icon) {
  104. return isSemiIcon(icon) ? React.cloneElement((icon as React.ReactElement), { size: iconSize, className: `${prefixCls}-icon` }) : icon;
  105. }
  106. if (type && iconType) {
  107. return React.cloneElement(iconType, { size: iconSize, className: iconCls });
  108. }
  109. return null;
  110. }
  111. render() {
  112. const { content, type, theme, showClose, textMaxWidth, className, style } = this.props;
  113. const direction = this.props.direction || this.context.direction;
  114. const toastCls = cls(prefixCls, className, {
  115. [`${prefixCls}-${type}`]: true,
  116. [`${prefixCls}-${theme}`]: theme === 'light',
  117. [`${prefixCls}-rtl`]: direction === 'rtl',
  118. });
  119. const textStyle: CSSProperties = {};
  120. textStyle.maxWidth = textMaxWidth;
  121. const btnTheme = 'borderless';
  122. const btnSize = 'small';
  123. const reservedIndex = this.props.positionInList ? ( this.props.positionInList.length - this.props.positionInList.index - 1) : 0;
  124. const toastEle = <div
  125. ref={this.toastEle}
  126. role="alert"
  127. aria-label={`${type ? type : 'default'} type`}
  128. className={toastCls}
  129. style={{
  130. ...style,
  131. transform: `translate3d(0,0,${reservedIndex*-10}px)`,
  132. }}
  133. onMouseEnter={this.clearCloseTimer}
  134. onMouseLeave={this.startCloseTimer}
  135. onAnimationStart={this.props.onAnimationStart}
  136. onAnimationEnd={this.props.onAnimationEnd}
  137. >
  138. <div className={`${prefixCls}-content`}>
  139. {this.renderIcon()}
  140. <span className={`${prefixCls}-content-text`} style={textStyle} x-semi-prop="content">
  141. {content}
  142. </span>
  143. {showClose && (
  144. <div className={`${prefixCls}-close-button`}>
  145. <Button
  146. onClick={e => this.close(e)}
  147. type="tertiary"
  148. icon={<IconClose x-semi-prop="icon" />}
  149. theme={btnTheme}
  150. size={btnSize}
  151. />
  152. </div>
  153. )}
  154. </div>
  155. </div>;
  156. if (this.props.stack) {
  157. const height = this.props.stackExpanded && this.toastEle.current && getComputedStyle(this.toastEle.current).height || 0;
  158. return <div className={`${prefixCls}-zero-height-wrapper`} style={{ height }}>
  159. {toastEle}
  160. </div>;
  161. } else {
  162. return toastEle;
  163. }
  164. }
  165. }
  166. export default Toast;