toast.tsx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. }
  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. context: ContextValue;
  50. get adapter(): ToastAdapter {
  51. return {
  52. ...super.adapter,
  53. notifyWrapperToRemove: (id: string) => {
  54. this.props.close(id);
  55. },
  56. notifyClose: () => {
  57. this.props.onClose();
  58. },
  59. };
  60. }
  61. componentDidMount() {
  62. this.foundation.init();
  63. }
  64. componentWillUnmount() {
  65. this.foundation.destroy();
  66. }
  67. close(e: React.MouseEvent) {
  68. this.foundation.close(e);
  69. }
  70. clearCloseTimer = () => {
  71. this.foundation.clearCloseTimer_();
  72. };
  73. startCloseTimer = () => {
  74. this.foundation.startCloseTimer_();
  75. };
  76. restartCloseTimer = () => {
  77. this.foundation.restartCloseTimer();
  78. }
  79. renderIcon() {
  80. const { type, icon } = this.props;
  81. const iconMap = {
  82. warning: <IconAlertTriangle />,
  83. success: <IconTickCircle />,
  84. info: <IconInfoCircle />,
  85. error: <IconAlertCircle />
  86. };
  87. const iconType = iconMap[type];
  88. const iconSize = 'large';
  89. const iconCls = cls(`${prefixCls}-icon`, `${prefixCls}-icon-${type}`);
  90. if (icon) {
  91. return isSemiIcon(icon) ? React.cloneElement((icon as React.ReactElement), { size: iconSize, className: `${prefixCls}-icon` }) : icon;
  92. }
  93. if (type && iconType) {
  94. return React.cloneElement(iconType, { size: iconSize, className: iconCls });
  95. }
  96. return null;
  97. }
  98. render() {
  99. const { content, type, theme, showClose, textMaxWidth, className, style } = this.props;
  100. const direction = this.props.direction || this.context.direction;
  101. const toastCls = cls(prefixCls, className, {
  102. [`${prefixCls}-${type}`]: true,
  103. [`${prefixCls}-${theme}`]: theme === 'light',
  104. [`${prefixCls}-rtl`]: direction === 'rtl',
  105. });
  106. const textStyle: CSSProperties = {};
  107. textStyle.maxWidth = textMaxWidth;
  108. const btnTheme = 'borderless';
  109. const btnSize = 'small';
  110. return (
  111. <div
  112. role="alert"
  113. aria-label={`${type ? type : 'default'} type`}
  114. className={toastCls}
  115. style={style}
  116. onMouseEnter={this.clearCloseTimer}
  117. onMouseLeave={this.startCloseTimer}
  118. >
  119. <div className={`${prefixCls}-content`}>
  120. {this.renderIcon()}
  121. <span className={`${prefixCls}-content-text`} style={textStyle} x-semi-prop="content">
  122. {content}
  123. </span>
  124. {showClose && (
  125. <div className={`${prefixCls}-close-button`}>
  126. <Button
  127. onClick={e => this.close(e)}
  128. type="tertiary"
  129. icon={<IconClose x-semi-prop="icon" />}
  130. theme={btnTheme}
  131. size={btnSize}
  132. />
  133. </div>
  134. )}
  135. </div>
  136. </div>
  137. );
  138. }
  139. }
  140. export default Toast;