1
0

index.tsx 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 from '../configProvider/context';
  8. import { cssClasses, strings } from '@douyinfe/semi-foundation/sideSheet/constants';
  9. import SideSheetTransition from './SideSheetTransition';
  10. import SideSheetContent from './SideSheetContent';
  11. import { noop } from 'lodash-es';
  12. import SideSheetFoundation, {
  13. SideSheetAdapter,
  14. SideSheetProps,
  15. SideSheetState
  16. } from '@douyinfe/semi-foundation/sideSheet/sideSheetFoundation';
  17. import '@douyinfe/semi-foundation/sideSheet/sideSheet.scss';
  18. const prefixCls = cssClasses.PREFIX;
  19. const defaultWidthList = strings.WIDTH;
  20. const defaultHeight = strings.HEIGHT;
  21. export { SideSheetContentProps } from './SideSheetContent';
  22. export { SideSheetTransitionProps } from './SideSheetTransition';
  23. export interface SideSheetReactProps extends SideSheetProps{
  24. bodyStyle?: CSSProperties;
  25. headerStyle?: CSSProperties;
  26. maskStyle?: CSSProperties;
  27. style?: CSSProperties;
  28. title?: React.ReactNode;
  29. footer?: React.ReactNode;
  30. children?: React.ReactNode;
  31. onCancel?: (e: React.MouseEvent | React.KeyboardEvent) => void;
  32. }
  33. export {
  34. SideSheetState
  35. };
  36. export default class SideSheet extends BaseComponent<SideSheetReactProps, SideSheetState> {
  37. static contextType = ConfigContext;
  38. static propTypes = {
  39. bodyStyle: PropTypes.object,
  40. headerStyle: PropTypes.object,
  41. children: PropTypes.node,
  42. className: PropTypes.string,
  43. closable: PropTypes.bool,
  44. disableScroll: PropTypes.bool,
  45. getPopupContainer: PropTypes.func,
  46. height: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
  47. mask: PropTypes.bool,
  48. maskClosable: PropTypes.bool,
  49. maskStyle: PropTypes.object,
  50. motion: PropTypes.oneOfType([PropTypes.bool, PropTypes.object, PropTypes.func]),
  51. onCancel: PropTypes.func,
  52. placement: PropTypes.oneOf(strings.PLACEMENT),
  53. size: PropTypes.oneOf(strings.SIZE),
  54. style: PropTypes.object,
  55. title: PropTypes.node,
  56. visible: PropTypes.bool,
  57. width: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
  58. zIndex: PropTypes.number,
  59. afterVisibleChange: PropTypes.func,
  60. closeOnEsc: PropTypes.bool,
  61. footer: PropTypes.node,
  62. keepDOM: PropTypes.bool
  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 = { hidden: !this.props.visible };
  83. this.foundation = new SideSheetFoundation(this.adapter);
  84. this._active = false;
  85. }
  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. toggleHidden: (hidden: boolean) => {
  118. if (hidden !== this.state.hidden) {
  119. this.setState({ hidden });
  120. }
  121. },
  122. };
  123. }
  124. static getDerivedStateFromProps(props: SideSheetReactProps, prevState: SideSheetState) {
  125. const newState: Partial<SideSheetState> = {};
  126. if (props.visible && prevState.hidden) {
  127. newState.hidden = false;
  128. }
  129. if (!props.visible && !props.motion && !prevState.hidden) {
  130. newState.hidden = true;
  131. }
  132. return newState;
  133. }
  134. componentDidMount() {
  135. if (this.props.visible) {
  136. this.foundation.beforeShow();
  137. this._active = this._active || this.props.visible;
  138. }
  139. }
  140. componentDidUpdate(prevProps: SideSheetReactProps, prevState: SideSheetState, snapshot: any) {
  141. // hide => show
  142. if (!prevProps.visible && this.props.visible) {
  143. this.foundation.beforeShow();
  144. }
  145. // show => hide
  146. if (prevProps.visible && !this.props.visible) {
  147. this.foundation.afterHide();
  148. }
  149. }
  150. componentWillUnmount() {
  151. if (this.props.visible) {
  152. this.foundation.destroy();
  153. }
  154. }
  155. handleCancel = (e: React.MouseEvent) => {
  156. this.foundation.handleCancel(e);
  157. };
  158. handleKeyDown = (e: KeyboardEvent) => {
  159. this.foundation.handleKeyDown(e);
  160. };
  161. renderContent() {
  162. const {
  163. placement,
  164. className,
  165. children,
  166. width,
  167. height,
  168. motion,
  169. visible,
  170. style,
  171. maskStyle,
  172. size,
  173. zIndex,
  174. getPopupContainer,
  175. keepDOM,
  176. ...props
  177. } = this.props;
  178. const { direction } = this.context;
  179. const isVertical = placement === 'left' || placement === 'right';
  180. const isHorizontal = placement === 'top' || placement === 'bottom';
  181. const sheetWidth = isVertical ? (width ? width : defaultWidthList[size]) : '100%';
  182. const sheetHeight = isHorizontal ? (height ? height : defaultHeight) : '100%';
  183. const classList = cls(prefixCls, className, {
  184. [`${prefixCls}-${placement}`]: placement,
  185. [`${prefixCls}-popup`]: getPopupContainer,
  186. [`${prefixCls}-horizontal`]: isHorizontal,
  187. [`${prefixCls}-rtl`]: direction === 'rtl',
  188. [`${prefixCls}-hidden`]: keepDOM && this.state.hidden,
  189. });
  190. const contentProps = {
  191. ...props,
  192. visible,
  193. motion: false,
  194. className: classList,
  195. width: sheetWidth,
  196. height: sheetHeight,
  197. onClose: this.handleCancel,
  198. };
  199. const mergedMotion = this.foundation.getMergedMotion();
  200. this._active = this._active || visible;
  201. const shouldRender = (visible || keepDOM) && this._active;
  202. if (mergedMotion) {
  203. return (
  204. <SideSheetTransition placement={placement} motion={mergedMotion} controlled={keepDOM} visible={visible}>
  205. {shouldRender ?
  206. transitionStyles => (
  207. <SideSheetContent
  208. {...contentProps}
  209. style={{ ...transitionStyles, ...style }}
  210. maskStyle={{ opacity: transitionStyles.opacity, ...maskStyle }}
  211. >
  212. {children}
  213. </SideSheetContent>
  214. ) : null}
  215. </SideSheetTransition>
  216. );
  217. }
  218. if (shouldRender) {
  219. return (
  220. <SideSheetContent {...contentProps} style={style} maskStyle={maskStyle}>
  221. {children}
  222. </SideSheetContent>
  223. );
  224. }
  225. return null;
  226. }
  227. render() {
  228. const {
  229. zIndex,
  230. getPopupContainer,
  231. } = this.props;
  232. let wrapperStyle: CSSProperties = {
  233. zIndex,
  234. };
  235. if (getPopupContainer) {
  236. wrapperStyle = {
  237. zIndex,
  238. position: 'static',
  239. };
  240. }
  241. return (
  242. <Portal getPopupContainer={getPopupContainer} style={wrapperStyle}>
  243. {this.renderContent()}
  244. </Portal>
  245. );
  246. }
  247. }