SideSheetContent.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import React, { CSSProperties } from 'react';
  2. import PropTypes from 'prop-types';
  3. import cls from 'classnames';
  4. import { cssClasses } from '@douyinfe/semi-foundation/sideSheet/constants';
  5. import Button from '../iconButton';
  6. import { noop } from 'lodash-es';
  7. import { IconClose } from '@douyinfe/semi-icons';
  8. let uuid = 0;
  9. const prefixCls = cssClasses.PREFIX;
  10. export interface SideSheetContentProps{
  11. onClose?: (e: React.MouseEvent | MouseEvent) => void;
  12. mask?: boolean;
  13. maskStyle?: CSSProperties;
  14. maskClosable?: boolean;
  15. title?: React.ReactNode;
  16. closable?: boolean;
  17. headerStyle?: CSSProperties;
  18. width: CSSProperties['width'];
  19. height: CSSProperties['height'];
  20. style: CSSProperties;
  21. bodyStyle?: CSSProperties;
  22. className: string;
  23. footer?: React.ReactNode;
  24. }
  25. export default class SideSheetContent extends React.PureComponent<SideSheetContentProps> {
  26. static propTypes = {
  27. onClose: PropTypes.func,
  28. };
  29. static defaultProps = {
  30. onClose: noop,
  31. };
  32. private sideSheetId: string;
  33. private timeoutId: number;
  34. componentDidMount() {
  35. this.sideSheetId = `sidesheet-${uuid++}`;
  36. }
  37. componentWillUnmount() {
  38. clearTimeout(this.timeoutId);
  39. }
  40. onMaskClick = (e: React.MouseEvent) => {
  41. if (e.target === e.currentTarget) {
  42. this.close(e);
  43. }
  44. };
  45. close = (e: React.MouseEvent) => {
  46. const { onClose } = this.props;
  47. onClose && onClose(e);
  48. };
  49. getMaskElement() {
  50. const {
  51. mask,
  52. maskStyle,
  53. maskClosable,
  54. } = this.props;
  55. if (mask) {
  56. return (
  57. <div
  58. key="mask"
  59. className={`${prefixCls}-mask`}
  60. style={maskStyle}
  61. onClick={maskClosable ? this.onMaskClick : null}
  62. />
  63. );
  64. }
  65. return null;
  66. }
  67. renderHeader() {
  68. const {
  69. title,
  70. closable,
  71. headerStyle,
  72. } = this.props;
  73. let header, closer;
  74. if (title) {
  75. header = (
  76. <div className={`${prefixCls}-title`}>
  77. {this.props.title}
  78. </div>
  79. );
  80. }
  81. if (closable) {
  82. closer = (
  83. <Button
  84. className={`${prefixCls}-close`}
  85. key="close-btn"
  86. onClick={this.close}
  87. type="tertiary"
  88. icon={<IconClose />}
  89. theme="borderless"
  90. size="small"
  91. />
  92. );
  93. }
  94. return (
  95. <div className={`${prefixCls}-header`} style={{ ...headerStyle }}>
  96. {header}
  97. {closer}
  98. </div>
  99. );
  100. }
  101. getDialogElement() {
  102. const { ...props } = this.props;
  103. const style: CSSProperties = {};
  104. if (props.width) {
  105. style.width = props.width;
  106. // When the mask is false, the width is set on the wrapper. At this time, sidesheet-inner does not need to set the width again, otherwise, the percentage will be accumulated repeatedly when the width is a percentage
  107. if (!props.mask) {
  108. style.width = '100%';
  109. }
  110. }
  111. if (props.height) {
  112. style.height = props.height;
  113. }
  114. const header = this.renderHeader();
  115. const dialogElement = (
  116. <div
  117. key="dialog-element"
  118. role="sidesheet"
  119. tabIndex={-1}
  120. className={`${prefixCls}-inner ${prefixCls}-inner-wrap`}
  121. // onMouseDown={this.onDialogMouseDown}
  122. style={{ ...props.style, ...style }}
  123. // id={this.dialogId}
  124. >
  125. <div className={`${prefixCls}-content`}>
  126. {header}
  127. <div className={`${prefixCls}-body`} style={props.bodyStyle}>
  128. {props.children}
  129. </div>
  130. {props.footer ? (
  131. <div className={`${prefixCls}-footer`}>
  132. {props.footer}
  133. </div>
  134. ) : null}
  135. </div>
  136. </div>
  137. );
  138. return dialogElement;
  139. }
  140. render() {
  141. const {
  142. mask,
  143. className,
  144. width,
  145. } = this.props;
  146. const wrapperCls = cls(className, {
  147. [`${prefixCls}-fixed`]: !mask,
  148. });
  149. const wrapperStyle: CSSProperties = {};
  150. if (!mask && width) {
  151. wrapperStyle.width = width;
  152. }
  153. return (
  154. <div className={wrapperCls} style={wrapperStyle}>
  155. {this.getMaskElement()}
  156. {this.getDialogElement()}
  157. </div>
  158. );
  159. }
  160. }