toast.tsx 5.3 KB

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