index.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /* eslint-disable no-nested-ternary */
  2. import React, { CSSProperties } from 'react';
  3. import BaseComponent from '../_base/baseComponent';
  4. import PropTypes from 'prop-types';
  5. import Portal from '../_portal';
  6. import cls from 'classnames';
  7. import ConfigContext, { ContextValue } from '../configProvider/context';
  8. import { cssClasses, strings } from '@douyinfe/semi-foundation/sideSheet/constants';
  9. import SideSheetContent from './SideSheetContent';
  10. import { noop } from 'lodash';
  11. import SideSheetFoundation, {
  12. SideSheetAdapter,
  13. SideSheetProps,
  14. SideSheetState
  15. } from '@douyinfe/semi-foundation/sideSheet/sideSheetFoundation';
  16. import '@douyinfe/semi-foundation/sideSheet/sideSheet.scss';
  17. import CSSAnimation from "../_cssAnimation";
  18. const prefixCls = cssClasses.PREFIX;
  19. const defaultWidthList = strings.WIDTH;
  20. const defaultHeight = strings.HEIGHT;
  21. export type { SideSheetContentProps } from './SideSheetContent';
  22. export interface SideSheetReactProps extends SideSheetProps {
  23. bodyStyle?: CSSProperties;
  24. headerStyle?: CSSProperties;
  25. maskStyle?: CSSProperties;
  26. style?: CSSProperties;
  27. title?: React.ReactNode;
  28. footer?: React.ReactNode;
  29. children?: React.ReactNode;
  30. onCancel?: (e: React.MouseEvent | React.KeyboardEvent) => void
  31. }
  32. export type {
  33. SideSheetState
  34. };
  35. export default class SideSheet extends BaseComponent<SideSheetReactProps, SideSheetState> {
  36. static contextType = ConfigContext;
  37. static propTypes = {
  38. bodyStyle: PropTypes.object,
  39. headerStyle: PropTypes.object,
  40. children: PropTypes.node,
  41. className: PropTypes.string,
  42. closable: PropTypes.bool,
  43. disableScroll: PropTypes.bool,
  44. getPopupContainer: PropTypes.func,
  45. height: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
  46. mask: PropTypes.bool,
  47. maskClosable: PropTypes.bool,
  48. maskStyle: PropTypes.object,
  49. motion: PropTypes.oneOfType([PropTypes.bool, PropTypes.object, PropTypes.func]),
  50. onCancel: PropTypes.func,
  51. placement: PropTypes.oneOf(strings.PLACEMENT),
  52. size: PropTypes.oneOf(strings.SIZE),
  53. style: PropTypes.object,
  54. title: PropTypes.node,
  55. visible: PropTypes.bool,
  56. width: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
  57. zIndex: PropTypes.number,
  58. afterVisibleChange: PropTypes.func,
  59. closeOnEsc: PropTypes.bool,
  60. footer: PropTypes.node,
  61. keepDOM: PropTypes.bool,
  62. 'aria-label': PropTypes.string,
  63. };
  64. static defaultProps: SideSheetReactProps = {
  65. visible: false,
  66. motion: true,
  67. mask: true,
  68. placement: 'right',
  69. closable: true,
  70. footer: null,
  71. zIndex: 1000,
  72. maskClosable: true,
  73. size: 'small',
  74. disableScroll: true,
  75. closeOnEsc: false,
  76. afterVisibleChange: noop,
  77. keepDOM: false
  78. };
  79. private _active: boolean;
  80. constructor(props: SideSheetReactProps) {
  81. super(props);
  82. this.state = { displayNone: !this.props.visible, shouldRender: this.props.visible };
  83. this.foundation = new SideSheetFoundation(this.adapter);
  84. }
  85. context: ContextValue;
  86. get adapter(): SideSheetAdapter {
  87. return {
  88. ...super.adapter,
  89. disabledBodyScroll: () => {
  90. const { getPopupContainer } = this.props;
  91. if (!getPopupContainer && document) {
  92. document.body.style.overflow = 'hidden';
  93. }
  94. },
  95. enabledBodyScroll: () => {
  96. const { getPopupContainer } = this.props;
  97. if (!getPopupContainer && document) {
  98. document.body.style.overflow = '';
  99. }
  100. },
  101. notifyCancel: (e: React.MouseEvent | React.KeyboardEvent) => {
  102. this.props.onCancel && this.props.onCancel(e);
  103. },
  104. notifyVisibleChange: (visible: boolean) => {
  105. this.props.afterVisibleChange(visible);
  106. },
  107. setOnKeyDownListener: () => {
  108. if (window) {
  109. window.addEventListener('keydown', this.handleKeyDown);
  110. }
  111. },
  112. removeKeyDownListener: () => {
  113. if (window) {
  114. window.removeEventListener('keydown', this.handleKeyDown);
  115. }
  116. },
  117. toggleDisplayNone: (displayNone: boolean) => {
  118. if (displayNone !== this.state.displayNone) {
  119. this.setState({ displayNone: displayNone });
  120. }
  121. },
  122. setShouldRender: (shouldRender: boolean) => {
  123. if (shouldRender !== this.state.shouldRender) {
  124. this.setState({ shouldRender });
  125. }
  126. }
  127. };
  128. }
  129. static getDerivedStateFromProps(props: SideSheetReactProps, prevState: SideSheetState) {
  130. const newState: Partial<SideSheetState> = {};
  131. if (props.visible && prevState.displayNone) {
  132. newState.displayNone = false;
  133. }
  134. if (!props.visible && !props.motion && !prevState.displayNone) {
  135. newState.displayNone = true;
  136. }
  137. return newState;
  138. }
  139. componentDidMount() {
  140. if (this.props.visible) {
  141. this.foundation.beforeShow();
  142. }
  143. }
  144. componentDidUpdate(prevProps: SideSheetReactProps, prevState: SideSheetState, snapshot: any) {
  145. // hide => show
  146. if (!prevProps.visible && this.props.visible) {
  147. this.foundation.beforeShow();
  148. }
  149. // show => hide
  150. if (prevProps.visible && !this.props.visible) {
  151. this.foundation.afterHide();
  152. }
  153. const shouldRender = (this.props.visible || this.props.keepDOM);
  154. if (shouldRender === true && this.state.shouldRender === false) {
  155. this.foundation.setShouldRender(true);
  156. }
  157. if (prevState.displayNone !== this.state.displayNone) {
  158. this.foundation.onVisibleChange(!this.state.displayNone);
  159. }
  160. }
  161. componentWillUnmount() {
  162. if (this.props.visible) {
  163. this.foundation.destroy();
  164. }
  165. }
  166. handleCancel = (e: React.MouseEvent) => {
  167. this.foundation.handleCancel(e);
  168. };
  169. handleKeyDown = (e: KeyboardEvent) => {
  170. this.foundation.handleKeyDown(e);
  171. };
  172. updateState = () => {
  173. const shouldRender = (this.props.visible || this.props.keepDOM);
  174. this.foundation.setShouldRender(shouldRender);
  175. this.foundation.toggleDisplayNone(!this.props.visible);
  176. }
  177. renderContent() {
  178. const {
  179. placement,
  180. className,
  181. children,
  182. width,
  183. height,
  184. motion,
  185. visible,
  186. style,
  187. maskStyle,
  188. size,
  189. zIndex,
  190. getPopupContainer,
  191. keepDOM,
  192. ...props
  193. } = this.props;
  194. const { direction } = this.context;
  195. const isVertical = placement === 'left' || placement === 'right';
  196. const isHorizontal = placement === 'top' || placement === 'bottom';
  197. const sheetWidth = isVertical ? (width ? width : defaultWidthList[size]) : '100%';
  198. const sheetHeight = isHorizontal ? (height ? height : defaultHeight) : '100%';
  199. const classList = cls(prefixCls, className, {
  200. [`${prefixCls}-${placement}`]: placement,
  201. [`${prefixCls}-popup`]: getPopupContainer,
  202. [`${prefixCls}-horizontal`]: isHorizontal,
  203. [`${prefixCls}-rtl`]: direction === 'rtl',
  204. [`${prefixCls}-hidden`]: keepDOM && this.state.displayNone,
  205. });
  206. const contentProps = {
  207. ...props,
  208. visible,
  209. motion: false,
  210. className: classList,
  211. width: sheetWidth,
  212. height: sheetHeight,
  213. onClose: this.handleCancel,
  214. };
  215. // Since user could change animate duration , we don't know which animation end first. So we call updateState func twice.
  216. return <CSSAnimation motion={this.props.motion} animationState={visible ? 'enter' : 'leave'} startClassName={
  217. visible ? `${prefixCls}-animation-mask_show` : `${prefixCls}-animation-mask_hide`
  218. } onAnimationEnd={this.updateState}>
  219. {
  220. ({
  221. animationClassName: maskAnimationClassName,
  222. animationEventsNeedBind: maskAnimationEventsNeedBind
  223. }) => {
  224. return <CSSAnimation
  225. motion={this.props.motion}
  226. animationState={visible ? 'enter' : 'leave'}
  227. startClassName={visible ? `${prefixCls}-animation-content_show_${this.props.placement}` : `${prefixCls}-animation-content_hide_${this.props.placement}`}
  228. onAnimationEnd={this.updateState /* for no mask case*/}
  229. >
  230. {({ animationClassName, animationStyle, animationEventsNeedBind }) => {
  231. return this.state.shouldRender ? <SideSheetContent
  232. {...contentProps}
  233. maskExtraProps={maskAnimationEventsNeedBind}
  234. wrapperExtraProps={animationEventsNeedBind}
  235. dialogClassName={animationClassName}
  236. maskClassName={maskAnimationClassName}
  237. maskStyle={{ ...maskStyle }}
  238. style={{ ...animationStyle, ...style }}>
  239. {children}
  240. </SideSheetContent> : <></>;
  241. }}
  242. </CSSAnimation>;
  243. }
  244. }
  245. </CSSAnimation>;
  246. }
  247. render() {
  248. const {
  249. zIndex,
  250. getPopupContainer,
  251. } = this.props;
  252. let wrapperStyle: CSSProperties = {
  253. zIndex,
  254. };
  255. if (getPopupContainer) {
  256. wrapperStyle = {
  257. zIndex,
  258. position: 'static',
  259. };
  260. }
  261. return (
  262. <Portal getPopupContainer={getPopupContainer} style={wrapperStyle}>
  263. {this.renderContent()}
  264. </Portal>
  265. );
  266. }
  267. }