index.tsx 11 KB

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