SideSheetContent.tsx 6.2 KB

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