index.tsx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. aria-label='Close'
  101. />
  102. );
  103. return closer;
  104. }
  105. renderIcon() {
  106. const { type, icon } = this.props;
  107. const iconMap = {
  108. warning: <IconAlertTriangle size="large" />,
  109. success: <IconTickCircle size="large" />,
  110. info: <IconInfoCircle size="large" />,
  111. danger: <IconAlertCircle size="large" />
  112. };
  113. let iconType: React.ReactNode = iconMap[type];
  114. const iconCls = cls({
  115. [`${prefixCls }-icon`]: true,
  116. // [prefixCls + '-' + type]: true,
  117. });
  118. if (typeof icon !== 'undefined') {
  119. iconType = icon;
  120. }
  121. if (iconType) {
  122. return (
  123. <div className={iconCls}>
  124. {iconType}
  125. </div>
  126. );
  127. }
  128. return null;
  129. }
  130. render() {
  131. const { children, type, className, style, bordered, title, description, fullMode } = this.props;
  132. const { visible } = this.state;
  133. const wrapper = cls(prefixCls, className, {
  134. [`${prefixCls}-${type}`]: type,
  135. [`${prefixCls}-full`]: fullMode,
  136. [`${prefixCls}-in-container`]: !fullMode,
  137. [`${prefixCls}-bordered`]: !fullMode && bordered,
  138. });
  139. const banner = visible ? (
  140. <div className={wrapper} style={style} role="alert">
  141. <div className={`${prefixCls}-content-wrapper`}>
  142. <div className={`${prefixCls}-content`}>
  143. {this.renderIcon()}
  144. <div className={`${prefixCls}-content-body`}>
  145. {title ? <Typography.Title heading={5} className={`${prefixCls}-title`} component="div">{title}</Typography.Title> : null}
  146. {description ? <Typography.Paragraph className={`${prefixCls}-description`} component="div">{description}</Typography.Paragraph> : null}
  147. </div>
  148. </div>
  149. {this.renderCloser()}
  150. </div>
  151. {children ? <div className={`${prefixCls}-extra`}>{children}</div> : null}
  152. </div>
  153. ) : null;
  154. return banner;
  155. }
  156. }