index.tsx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* eslint-disable max-len */
  2. import React from 'react';
  3. import cls from 'classnames';
  4. import PropTypes from 'prop-types';
  5. import Button from '../iconButton';
  6. import { strings, cssClasses } from '@douyinfe/semi-foundation/banner/constants';
  7. import BannerFoundation, { BannerAdapter } from '@douyinfe/semi-foundation/banner/foundation';
  8. import '@douyinfe/semi-foundation/banner/banner.scss';
  9. import Typography from '../typography';
  10. import { IconClose, IconAlertTriangle, IconInfoCircle, IconTickCircle, IconAlertCircle } from '@douyinfe/semi-icons';
  11. import warning from '@douyinfe/semi-foundation/utils/warning';
  12. import BaseComponent from '../_base/baseComponent';
  13. const prefixCls = cssClasses.PREFIX;
  14. const types = strings.TYPE;
  15. export type Type = 'info' | 'danger' | 'warning' | 'success';
  16. export interface BannerProps {
  17. type?: Type;
  18. className?: string;
  19. children?: React.ReactNode | undefined;
  20. fullMode?: boolean;
  21. title?: React.ReactNode;
  22. description?: React.ReactNode;
  23. icon?: string | React.ReactNode;
  24. closeIcon?: string | React.ReactNode;
  25. style?: React.CSSProperties;
  26. bordered?: boolean;
  27. onClose?: (e: React.MouseEvent) => void;
  28. }
  29. export interface BannerState {
  30. visible: boolean;
  31. }
  32. export default class Banner extends BaseComponent<BannerProps, BannerState> {
  33. static propTypes = {
  34. // target: PropTypes.func,
  35. fullMode: PropTypes.bool,
  36. // insertAfter: PropTypes.func,
  37. type: PropTypes.oneOf(types),
  38. title: PropTypes.node,
  39. description: PropTypes.node,
  40. icon: PropTypes.oneOfType([PropTypes.string, PropTypes.node]),
  41. closeIcon: PropTypes.oneOfType([PropTypes.string, PropTypes.node]),
  42. children: PropTypes.node,
  43. style: PropTypes.object,
  44. className: PropTypes.string,
  45. onClose: PropTypes.func,
  46. bordered: PropTypes.bool,
  47. };
  48. static defaultProps = {
  49. // eslint-disable-next-line @typescript-eslint/no-empty-function
  50. onClose: () => { },
  51. type: 'info',
  52. fullMode: true,
  53. };
  54. foundation: BannerFoundation;
  55. constructor(props: BannerProps) {
  56. super(props);
  57. this.state = {
  58. visible: true,
  59. };
  60. warning(
  61. 'target' in this.props,
  62. '[Semi Banner] \'target\' has been deprecated, please write JSX directly instead.'
  63. );
  64. }
  65. get adapter(): BannerAdapter<BannerProps, BannerState> {
  66. return {
  67. ...super.adapter,
  68. setVisible: () => {
  69. this.setState({ visible: false });
  70. },
  71. notifyClose: (e: React.MouseEvent) => {
  72. const { onClose } = this.props;
  73. onClose(e);
  74. },
  75. };
  76. }
  77. componentDidMount() {
  78. this.foundation = new BannerFoundation(this.adapter);
  79. this.foundation.init();
  80. }
  81. componentWillUnmount() {
  82. this.foundation.destroy();
  83. }
  84. remove: React.MouseEventHandler = e => {
  85. e && e.stopPropagation();
  86. this.foundation.removeBanner(e);
  87. };
  88. renderCloser() {
  89. const { closeIcon } = this.props;
  90. if (closeIcon === null) {
  91. return closeIcon;
  92. }
  93. const closer = (
  94. <Button
  95. className={`${prefixCls}-close`}
  96. onClick={this.remove}
  97. icon={closeIcon || <IconClose />}
  98. theme="borderless"
  99. size="small"
  100. type="tertiary"
  101. aria-label='Close'
  102. />
  103. );
  104. return closer;
  105. }
  106. renderIcon() {
  107. const { type, icon } = this.props;
  108. const iconMap = {
  109. warning: <IconAlertTriangle size="large" />,
  110. success: <IconTickCircle size="large" />,
  111. info: <IconInfoCircle size="large" />,
  112. danger: <IconAlertCircle size="large" />
  113. };
  114. let iconType: React.ReactNode = iconMap[type];
  115. const iconCls = cls({
  116. [`${prefixCls }-icon`]: true,
  117. // [prefixCls + '-' + type]: true,
  118. });
  119. if (typeof icon !== 'undefined') {
  120. iconType = icon;
  121. }
  122. if (iconType) {
  123. return (
  124. <div className={iconCls}>
  125. {iconType}
  126. </div>
  127. );
  128. }
  129. return null;
  130. }
  131. render() {
  132. const { children, type, className, style, bordered, title, description, fullMode } = this.props;
  133. const { visible } = this.state;
  134. const wrapper = cls(prefixCls, className, {
  135. [`${prefixCls}-${type}`]: type,
  136. [`${prefixCls}-full`]: fullMode,
  137. [`${prefixCls}-in-container`]: !fullMode,
  138. [`${prefixCls}-bordered`]: !fullMode && bordered,
  139. });
  140. const banner = visible ? (
  141. <div className={wrapper} style={style} role="alert">
  142. <div className={`${prefixCls}-content-wrapper`}>
  143. <div className={`${prefixCls}-content`}>
  144. {this.renderIcon()}
  145. <div className={`${prefixCls}-content-body`}>
  146. {title ? <Typography.Title heading={5} className={`${prefixCls}-title`} component="div">{title}</Typography.Title> : null}
  147. {description ? <Typography.Paragraph className={`${prefixCls}-description`} component="div">{description}</Typography.Paragraph> : null}
  148. </div>
  149. </div>
  150. {this.renderCloser()}
  151. </div>
  152. {children ? <div className={`${prefixCls}-extra`}>{children}</div> : null}
  153. </div>
  154. ) : null;
  155. return banner;
  156. }
  157. }