index.tsx 5.5 KB

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