toast.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 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-es';
  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. }
  19. class Toast extends BaseComponent<ToastReactProps, ToastState> {
  20. static contextType = ConfigContext;
  21. static propTypes = {
  22. onClose: PropTypes.func,
  23. content: PropTypes.node,
  24. close: PropTypes.func,
  25. duration: PropTypes.number,
  26. theme: PropTypes.oneOf(strings.themes),
  27. type: PropTypes.oneOf(strings.types),
  28. textMaxWidth: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
  29. style: PropTypes.object,
  30. className: PropTypes.string,
  31. showClose: PropTypes.bool,
  32. icon: PropTypes.oneOfType([PropTypes.string, PropTypes.node]),
  33. direction: PropTypes.oneOf(strings.directions),
  34. };
  35. static defaultProps = {
  36. onClose: noop,
  37. content: '',
  38. close: noop,
  39. duration: numbers.duration,
  40. textMaxWidth: 450,
  41. showClose: true,
  42. theme: 'normal'
  43. };
  44. constructor(props: ToastReactProps) {
  45. super(props);
  46. this.state = {};
  47. this.foundation = new ToastFoundation(this.adapter);
  48. }
  49. get adapter(): ToastAdapter {
  50. return {
  51. ...super.adapter,
  52. notifyWrapperToRemove: (id: string) => {
  53. this.props.close(id);
  54. },
  55. notifyClose: () => {
  56. this.props.onClose();
  57. },
  58. };
  59. }
  60. componentDidMount() {
  61. this.foundation.init();
  62. }
  63. componentWillUnmount() {
  64. this.foundation.destroy();
  65. }
  66. close(e: React.MouseEvent) {
  67. this.foundation.close(e);
  68. }
  69. clearCloseTimer = () => {
  70. this.foundation.clearCloseTimer_();
  71. };
  72. startCloseTimer = () => {
  73. this.foundation.startCloseTimer_();
  74. };
  75. renderIcon() {
  76. const { type, icon } = this.props;
  77. const iconMap = {
  78. warning: <IconAlertTriangle />,
  79. success: <IconTickCircle />,
  80. info: <IconInfoCircle />,
  81. error: <IconAlertCircle />
  82. };
  83. const iconType = iconMap[type];
  84. const iconSize = 'large';
  85. const iconCls = cls(`${prefixCls}-icon`, `${prefixCls}-icon-${type}`);
  86. if (icon) {
  87. return isSemiIcon(icon) ? React.cloneElement((icon as React.ReactElement), { size: iconSize, className: `${prefixCls}-icon` }) : icon;
  88. }
  89. if (type && iconType) {
  90. return React.cloneElement(iconType, { size: iconSize, className: iconCls });
  91. }
  92. return null;
  93. }
  94. render() {
  95. const { content, type, theme, showClose, textMaxWidth, className, style } = this.props;
  96. const direction = this.props.direction || this.context.direction;
  97. const toastCls = cls(prefixCls, className, {
  98. [`${prefixCls}-${type}`]: true,
  99. [`${prefixCls}-${theme}`]: theme === 'light',
  100. [`${prefixCls}-rtl`]: direction === 'rtl',
  101. });
  102. const textStyle: CSSProperties = {};
  103. textStyle.maxWidth = textMaxWidth;
  104. const btnTheme = 'borderless';
  105. const btnSize = 'small';
  106. return (
  107. <div
  108. className={toastCls}
  109. style={style}
  110. onMouseEnter={this.clearCloseTimer}
  111. onMouseLeave={this.startCloseTimer}
  112. >
  113. <div className={`${prefixCls}-content`}>
  114. {this.renderIcon()}
  115. <span className={`${prefixCls}-content-text`} style={textStyle}>
  116. {content}
  117. </span>
  118. {showClose && (
  119. <div className={`${prefixCls}-close-button`}>
  120. <Button
  121. onClick={e => this.close(e)}
  122. type="tertiary"
  123. icon={<IconClose />}
  124. theme={btnTheme}
  125. size={btnSize}
  126. />
  127. </div>
  128. )}
  129. </div>
  130. </div>
  131. );
  132. }
  133. }
  134. export default Toast;