toast.tsx 6.1 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 { 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 defaultProps = {
  46. onClose: noop,
  47. content: '',
  48. close: noop,
  49. duration: numbers.duration,
  50. textMaxWidth: 450,
  51. showClose: true,
  52. stack: false,
  53. stackExpanded: false,
  54. theme: 'normal'
  55. };
  56. constructor(props: ToastReactProps) {
  57. super(props);
  58. this.state = {};
  59. this.foundation = new ToastFoundation(this.adapter);
  60. }
  61. context: ContextValue;
  62. get adapter(): ToastAdapter {
  63. return {
  64. ...super.adapter,
  65. notifyWrapperToRemove: (id: string) => {
  66. this.props.close(id);
  67. },
  68. notifyClose: () => {
  69. this.props.onClose();
  70. },
  71. };
  72. }
  73. componentDidMount() {
  74. this.foundation.init();
  75. }
  76. componentWillUnmount() {
  77. this.foundation.destroy();
  78. }
  79. close(e: React.MouseEvent) {
  80. this.foundation.close(e);
  81. }
  82. clearCloseTimer = () => {
  83. this.foundation.clearCloseTimer_();
  84. };
  85. startCloseTimer = () => {
  86. this.foundation.startCloseTimer_();
  87. };
  88. restartCloseTimer = () => {
  89. this.foundation.restartCloseTimer();
  90. }
  91. renderIcon() {
  92. const { type, icon } = this.props;
  93. const iconMap = {
  94. warning: <IconAlertTriangle />,
  95. success: <IconTickCircle />,
  96. info: <IconInfoCircle />,
  97. error: <IconAlertCircle />
  98. };
  99. const iconType = iconMap[type];
  100. const iconSize = 'large';
  101. const iconCls = cls(`${prefixCls}-icon`, `${prefixCls}-icon-${type}`);
  102. if (icon) {
  103. return isSemiIcon(icon) ? React.cloneElement((icon as React.ReactElement), { size: iconSize, className: `${prefixCls}-icon` }) : icon;
  104. }
  105. if (type && iconType) {
  106. return React.cloneElement(iconType, { size: iconSize, className: iconCls });
  107. }
  108. return null;
  109. }
  110. render() {
  111. const { content, type, theme, showClose, textMaxWidth, className, style } = this.props;
  112. const direction = this.props.direction || this.context.direction;
  113. const toastCls = cls(prefixCls, className, {
  114. [`${prefixCls}-${type}`]: true,
  115. [`${prefixCls}-${theme}`]: theme === 'light',
  116. [`${prefixCls}-rtl`]: direction === 'rtl',
  117. });
  118. const textStyle: CSSProperties = {};
  119. textStyle.maxWidth = textMaxWidth;
  120. const btnTheme = 'borderless';
  121. const btnSize = 'small';
  122. const reservedIndex = this.props.positionInList ? ( this.props.positionInList.length - this.props.positionInList.index - 1) : 0;
  123. const toastEle = <div
  124. ref={this.toastEle}
  125. role="alert"
  126. aria-label={`${type ? type : 'default'} type`}
  127. className={toastCls}
  128. style={{
  129. ...style,
  130. transform: `translate3d(0,0,${reservedIndex*-10}px)`,
  131. }}
  132. onMouseEnter={this.clearCloseTimer}
  133. onMouseLeave={this.startCloseTimer}
  134. onAnimationStart={this.props.onAnimationStart}
  135. onAnimationEnd={this.props.onAnimationEnd}
  136. >
  137. <div className={`${prefixCls}-content`}>
  138. {this.renderIcon()}
  139. <span className={`${prefixCls}-content-text`} style={textStyle} x-semi-prop="content">
  140. {content}
  141. </span>
  142. {showClose && (
  143. <div className={`${prefixCls}-close-button`}>
  144. <Button
  145. onClick={e => this.close(e)}
  146. type="tertiary"
  147. icon={<IconClose x-semi-prop="icon" />}
  148. theme={btnTheme}
  149. size={btnSize}
  150. />
  151. </div>
  152. )}
  153. </div>
  154. </div>;
  155. if (this.props.stack) {
  156. const height = this.props.stackExpanded && this.toastEle.current && getComputedStyle(this.toastEle.current).height || 0;
  157. return <div className={`${prefixCls}-zero-height-wrapper`} style={{ height }}>
  158. {toastEle}
  159. </div>;
  160. } else {
  161. return toastEle;
  162. }
  163. }
  164. }
  165. export default Toast;