SideSheetContent.tsx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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';
  7. import { IconClose } from '@douyinfe/semi-icons';
  8. import { SideSheetProps } from "@douyinfe/semi-foundation/sideSheet/sideSheetFoundation";
  9. import getDataAttr from '@douyinfe/semi-foundation/utils/getDataAttr';
  10. let uuid = 0;
  11. const prefixCls = cssClasses.PREFIX;
  12. export interface SideSheetContentProps {
  13. onClose?: (e: React.MouseEvent) => void;
  14. mask?: boolean;
  15. maskStyle?: CSSProperties;
  16. maskClosable?: boolean;
  17. maskClassName?: string;
  18. title?: React.ReactNode;
  19. closable?: boolean;
  20. headerStyle?: CSSProperties;
  21. width?: CSSProperties['width'];
  22. height: CSSProperties['height'];
  23. style: CSSProperties;
  24. size: SideSheetProps['size'];
  25. bodyStyle?: CSSProperties;
  26. className: string;
  27. dialogClassName?: string;
  28. children?: React.ReactNode;
  29. footer?: React.ReactNode;
  30. 'aria-label'?: string;
  31. maskExtraProps?: {[key: string]: any};
  32. wrapperExtraProps?: {[key: string]: any}
  33. }
  34. export default class SideSheetContent extends React.PureComponent<SideSheetContentProps> {
  35. static propTypes = {
  36. onClose: PropTypes.func,
  37. };
  38. static defaultProps = {
  39. onClose: noop,
  40. };
  41. private sideSheetId: string;
  42. private timeoutId: number;
  43. componentDidMount() {
  44. this.sideSheetId = `sidesheet-${uuid++}`;
  45. }
  46. componentWillUnmount() {
  47. clearTimeout(this.timeoutId);
  48. }
  49. onMaskClick = (e: React.MouseEvent) => {
  50. if (e.target === e.currentTarget) {
  51. this.close(e);
  52. }
  53. };
  54. close = (e: React.MouseEvent) => {
  55. const { onClose } = this.props;
  56. onClose && onClose(e);
  57. };
  58. getMaskElement() {
  59. const {
  60. mask,
  61. maskStyle,
  62. maskClosable,
  63. } = this.props;
  64. if (mask) {
  65. return (
  66. <div
  67. aria-hidden={true}
  68. key="mask"
  69. className={cls(`${prefixCls}-mask`, this.props.maskClassName ?? "")}
  70. style={maskStyle}
  71. onClick={maskClosable ? this.onMaskClick : null}
  72. {...this.props.maskExtraProps}
  73. />
  74. );
  75. }
  76. return null;
  77. }
  78. renderHeader() {
  79. const {
  80. title,
  81. closable,
  82. headerStyle,
  83. } = this.props;
  84. let header, closer;
  85. if (title) {
  86. header = (
  87. <div className={`${prefixCls}-title`} x-semi-prop="title">
  88. {this.props.title}
  89. </div>
  90. );
  91. }
  92. if (closable) {
  93. closer = (
  94. <Button
  95. className={`${prefixCls}-close`}
  96. key="close-btn"
  97. onClick={this.close}
  98. type="tertiary"
  99. icon={<IconClose/>}
  100. theme="borderless"
  101. size="small"
  102. />
  103. );
  104. }
  105. return (
  106. <div className={`${prefixCls}-header`} role={'heading'} aria-level={1} style={{ ...headerStyle }}>
  107. {header}
  108. {closer}
  109. </div>
  110. );
  111. }
  112. getDialogElement() {
  113. const { ...props } = this.props;
  114. const style: CSSProperties = {};
  115. if (props.width) {
  116. style.width = props.width;
  117. // 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
  118. if (!props.mask) {
  119. style.width = '100%';
  120. }
  121. }
  122. if (props.height) {
  123. style.height = props.height;
  124. }
  125. const header = this.renderHeader();
  126. const dialogElement = (
  127. <div
  128. key="dialog-element"
  129. role="dialog"
  130. tabIndex={-1}
  131. className={cls(`${prefixCls}-inner`, `${prefixCls}-inner-wrap`, this.props.dialogClassName??"", `${prefixCls}-size-${props.size}`)}
  132. // onMouseDown={this.onDialogMouseDown}
  133. style={{ ...props.style, ...style }}
  134. {...this.props.wrapperExtraProps}
  135. // id={this.dialogId}
  136. >
  137. <div className={`${prefixCls}-content`}>
  138. {header}
  139. <div className={`${prefixCls}-body`} style={props.bodyStyle} x-semi-prop="children">
  140. {props.children}
  141. </div>
  142. {props.footer ? (
  143. <div className={`${prefixCls}-footer`} x-semi-prop="footer">
  144. {props.footer}
  145. </div>
  146. ) : null}
  147. </div>
  148. </div>
  149. );
  150. return dialogElement;
  151. }
  152. render() {
  153. const {
  154. mask,
  155. className,
  156. width,
  157. onClose,
  158. maskStyle,
  159. maskClosable,
  160. maskClassName,
  161. title,
  162. closable,
  163. headerStyle,
  164. height,
  165. style,
  166. size,
  167. bodyStyle,
  168. dialogClassName,
  169. children,
  170. footer,
  171. maskExtraProps,
  172. wrapperExtraProps,
  173. ...rest
  174. } = this.props;
  175. const wrapperCls = cls(className, {
  176. [`${prefixCls}-fixed`]: !mask,
  177. [`${prefixCls}-size-${this.props.size}`]: !mask
  178. });
  179. const wrapperStyle: CSSProperties = {};
  180. if (!mask && width) {
  181. wrapperStyle.width = width;
  182. }
  183. const dataAttr = getDataAttr(rest);
  184. return (
  185. <div className={wrapperCls} style={wrapperStyle} {...dataAttr}>
  186. {this.getMaskElement()}
  187. {this.getDialogElement()}
  188. </div>
  189. );
  190. }
  191. }