toast.tsx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. renderIcon() {
  77. const { type, icon } = this.props;
  78. const iconMap = {
  79. warning: <IconAlertTriangle />,
  80. success: <IconTickCircle />,
  81. info: <IconInfoCircle />,
  82. error: <IconAlertCircle />
  83. };
  84. const iconType = iconMap[type];
  85. const iconSize = 'large';
  86. const iconCls = cls(`${prefixCls}-icon`, `${prefixCls}-icon-${type}`);
  87. if (icon) {
  88. return isSemiIcon(icon) ? React.cloneElement((icon as React.ReactElement), { size: iconSize, className: `${prefixCls}-icon` }) : icon;
  89. }
  90. if (type && iconType) {
  91. return React.cloneElement(iconType, { size: iconSize, className: iconCls });
  92. }
  93. return null;
  94. }
  95. render() {
  96. const { content, type, theme, showClose, textMaxWidth, className, style } = this.props;
  97. const direction = this.props.direction || this.context.direction;
  98. const toastCls = cls(prefixCls, className, {
  99. [`${prefixCls}-${type}`]: true,
  100. [`${prefixCls}-${theme}`]: theme === 'light',
  101. [`${prefixCls}-rtl`]: direction === 'rtl',
  102. });
  103. const textStyle: CSSProperties = {};
  104. textStyle.maxWidth = textMaxWidth;
  105. const btnTheme = 'borderless';
  106. const btnSize = 'small';
  107. return (
  108. <div
  109. role="alert"
  110. aria-label={`${type ? type : 'default'} type`}
  111. className={toastCls}
  112. style={style}
  113. onMouseEnter={this.clearCloseTimer}
  114. onMouseLeave={this.startCloseTimer}
  115. >
  116. <div className={`${prefixCls}-content`}>
  117. {this.renderIcon()}
  118. <span className={`${prefixCls}-content-text`} style={textStyle} x-semi-prop="content">
  119. {content}
  120. </span>
  121. {showClose && (
  122. <div className={`${prefixCls}-close-button`}>
  123. <Button
  124. onClick={e => this.close(e)}
  125. type="tertiary"
  126. icon={<IconClose x-semi-prop="icon" />}
  127. theme={btnTheme}
  128. size={btnSize}
  129. />
  130. </div>
  131. )}
  132. </div>
  133. </div>
  134. );
  135. }
  136. }
  137. export default Toast;