index.tsx 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 };
  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. };
  123. }
  124. static getDerivedStateFromProps(props: SideSheetReactProps, prevState: SideSheetState) {
  125. const newState: Partial<SideSheetState> = {};
  126. if (props.visible && prevState.displayNone) {
  127. newState.displayNone = false;
  128. }
  129. if (!props.visible && !props.motion && !prevState.displayNone) {
  130. newState.displayNone = true;
  131. }
  132. return newState;
  133. }
  134. componentDidMount() {
  135. if (this.props.visible) {
  136. this.foundation.beforeShow();
  137. }
  138. }
  139. componentDidUpdate(prevProps: SideSheetReactProps, prevState: SideSheetState, snapshot: any) {
  140. // hide => show
  141. if (!prevProps.visible && this.props.visible) {
  142. this.foundation.beforeShow();
  143. }
  144. // show => hide
  145. if (prevProps.visible && !this.props.visible) {
  146. this.foundation.afterHide();
  147. }
  148. if (prevState.displayNone !== this.state.displayNone) {
  149. this.foundation.onVisibleChange(!this.state.displayNone);
  150. }
  151. }
  152. componentWillUnmount() {
  153. if (this.props.visible) {
  154. this.foundation.destroy();
  155. }
  156. }
  157. handleCancel = (e: React.MouseEvent) => {
  158. this.foundation.handleCancel(e);
  159. };
  160. handleKeyDown = (e: KeyboardEvent) => {
  161. this.foundation.handleKeyDown(e);
  162. };
  163. updateState = () => {
  164. this.foundation.toggleDisplayNone(!this.props.visible);
  165. }
  166. renderContent() {
  167. const {
  168. placement,
  169. className,
  170. children,
  171. width,
  172. height,
  173. motion,
  174. visible,
  175. style,
  176. maskStyle,
  177. size,
  178. zIndex,
  179. getPopupContainer,
  180. keepDOM,
  181. ...props
  182. } = this.props;
  183. const { direction } = this.context;
  184. const isVertical = placement === 'left' || placement === 'right';
  185. const isHorizontal = placement === 'top' || placement === 'bottom';
  186. const sheetHeight = isHorizontal ? (height ? height : defaultHeight) : '100%';
  187. const classList = cls(prefixCls, className, {
  188. [`${prefixCls}-${placement}`]: placement,
  189. [`${prefixCls}-popup`]: getPopupContainer,
  190. [`${prefixCls}-horizontal`]: isHorizontal,
  191. [`${prefixCls}-rtl`]: direction === 'rtl',
  192. [`${prefixCls}-hidden`]: keepDOM && this.state.displayNone,
  193. });
  194. const contentProps = {
  195. ...( isVertical ? (width ? { width } : {}) : { width: "100%" }),
  196. ...props,
  197. visible,
  198. motion: false,
  199. size,
  200. className: classList,
  201. height: sheetHeight,
  202. onClose: this.handleCancel,
  203. };
  204. const shouldRender = (this.props.visible || this.props.keepDOM) || (this.props.motion && !this.state.displayNone /* When there is animation, we use displayNone to judge whether animation is ended and judge whether to unmount content */);
  205. // Since user could change animate duration , we don't know which animation end first. So we call updateState func twice.
  206. return <CSSAnimation motion={this.props.motion} animationState={visible ? 'enter' : 'leave'} startClassName={
  207. visible ? `${prefixCls}-animation-mask_show` : `${prefixCls}-animation-mask_hide`
  208. } onAnimationEnd={this.updateState}>
  209. {
  210. ({
  211. animationClassName: maskAnimationClassName,
  212. animationEventsNeedBind: maskAnimationEventsNeedBind
  213. }) => {
  214. return <CSSAnimation
  215. motion={this.props.motion}
  216. animationState={visible ? 'enter' : 'leave'}
  217. startClassName={visible ? `${prefixCls}-animation-content_show_${this.props.placement}` : `${prefixCls}-animation-content_hide_${this.props.placement}`}
  218. onAnimationEnd={this.updateState /* for no mask case*/}
  219. >
  220. {({ animationClassName, animationStyle, animationEventsNeedBind }) => {
  221. return shouldRender ? <SideSheetContent
  222. {...contentProps}
  223. maskExtraProps={maskAnimationEventsNeedBind}
  224. wrapperExtraProps={animationEventsNeedBind}
  225. dialogClassName={animationClassName}
  226. maskClassName={maskAnimationClassName}
  227. maskStyle={{ ...maskStyle }}
  228. style={{ ...animationStyle, ...style }}>
  229. {children}
  230. </SideSheetContent> : <></>;
  231. }}
  232. </CSSAnimation>;
  233. }
  234. }
  235. </CSSAnimation>;
  236. }
  237. render() {
  238. const {
  239. zIndex,
  240. getPopupContainer,
  241. } = this.props;
  242. let wrapperStyle: CSSProperties = {
  243. zIndex,
  244. };
  245. if (getPopupContainer) {
  246. wrapperStyle = {
  247. zIndex,
  248. position: 'static',
  249. };
  250. }
  251. return (
  252. <Portal getPopupContainer={getPopupContainer} style={wrapperStyle}>
  253. {this.renderContent()}
  254. </Portal>
  255. );
  256. }
  257. }