index.tsx 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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';
  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. 'aria-label': PropTypes.string,
  64. };
  65. static defaultProps: SideSheetReactProps = {
  66. visible: false,
  67. motion: true,
  68. mask: true,
  69. placement: 'right',
  70. closable: true,
  71. footer: null,
  72. zIndex: 1000,
  73. maskClosable: true,
  74. size: 'small',
  75. disableScroll: true,
  76. closeOnEsc: false,
  77. afterVisibleChange: noop,
  78. keepDOM: false
  79. };
  80. private _active: boolean;
  81. constructor(props: SideSheetReactProps) {
  82. super(props);
  83. this.state = { hidden: !this.props.visible };
  84. this.foundation = new SideSheetFoundation(this.adapter);
  85. this._active = false;
  86. }
  87. get adapter(): SideSheetAdapter {
  88. return {
  89. ...super.adapter,
  90. disabledBodyScroll: () => {
  91. const { getPopupContainer } = this.props;
  92. if (!getPopupContainer && document) {
  93. document.body.style.overflow = 'hidden';
  94. }
  95. },
  96. enabledBodyScroll: () => {
  97. const { getPopupContainer } = this.props;
  98. if (!getPopupContainer && document) {
  99. document.body.style.overflow = '';
  100. }
  101. },
  102. notifyCancel: (e: React.MouseEvent | React.KeyboardEvent) => {
  103. this.props.onCancel && this.props.onCancel(e);
  104. },
  105. notifyVisibleChange: (visible: boolean) => {
  106. this.props.afterVisibleChange(visible);
  107. },
  108. setOnKeyDownListener: () => {
  109. if (window) {
  110. window.addEventListener('keydown', this.handleKeyDown);
  111. }
  112. },
  113. removeKeyDownListener: () => {
  114. if (window) {
  115. window.removeEventListener('keydown', this.handleKeyDown);
  116. }
  117. },
  118. toggleHidden: (hidden: boolean) => {
  119. if (hidden !== this.state.hidden) {
  120. this.setState({ hidden });
  121. }
  122. },
  123. };
  124. }
  125. static getDerivedStateFromProps(props: SideSheetReactProps, prevState: SideSheetState) {
  126. const newState: Partial<SideSheetState> = {};
  127. if (props.visible && prevState.hidden) {
  128. newState.hidden = false;
  129. }
  130. if (!props.visible && !props.motion && !prevState.hidden) {
  131. newState.hidden = true;
  132. }
  133. return newState;
  134. }
  135. componentDidMount() {
  136. if (this.props.visible) {
  137. this.foundation.beforeShow();
  138. this._active = this._active || this.props.visible;
  139. }
  140. }
  141. componentDidUpdate(prevProps: SideSheetReactProps, prevState: SideSheetState, snapshot: any) {
  142. // hide => show
  143. if (!prevProps.visible && this.props.visible) {
  144. this.foundation.beforeShow();
  145. }
  146. // show => hide
  147. if (prevProps.visible && !this.props.visible) {
  148. this.foundation.afterHide();
  149. }
  150. }
  151. componentWillUnmount() {
  152. if (this.props.visible) {
  153. this.foundation.destroy();
  154. }
  155. }
  156. handleCancel = (e: React.MouseEvent) => {
  157. this.foundation.handleCancel(e);
  158. };
  159. handleKeyDown = (e: KeyboardEvent) => {
  160. this.foundation.handleKeyDown(e);
  161. };
  162. renderContent() {
  163. const {
  164. placement,
  165. className,
  166. children,
  167. width,
  168. height,
  169. motion,
  170. visible,
  171. style,
  172. maskStyle,
  173. size,
  174. zIndex,
  175. getPopupContainer,
  176. keepDOM,
  177. ...props
  178. } = this.props;
  179. const { direction } = this.context;
  180. const isVertical = placement === 'left' || placement === 'right';
  181. const isHorizontal = placement === 'top' || placement === 'bottom';
  182. const sheetWidth = isVertical ? (width ? width : defaultWidthList[size]) : '100%';
  183. const sheetHeight = isHorizontal ? (height ? height : defaultHeight) : '100%';
  184. const classList = cls(prefixCls, className, {
  185. [`${prefixCls}-${placement}`]: placement,
  186. [`${prefixCls}-popup`]: getPopupContainer,
  187. [`${prefixCls}-horizontal`]: isHorizontal,
  188. [`${prefixCls}-rtl`]: direction === 'rtl',
  189. [`${prefixCls}-hidden`]: keepDOM && this.state.hidden,
  190. });
  191. const contentProps = {
  192. ...props,
  193. visible,
  194. motion: false,
  195. className: classList,
  196. width: sheetWidth,
  197. height: sheetHeight,
  198. onClose: this.handleCancel,
  199. };
  200. const mergedMotion = this.foundation.getMergedMotion();
  201. this._active = this._active || visible;
  202. const shouldRender = (visible || keepDOM) && this._active;
  203. if (mergedMotion) {
  204. return (
  205. <SideSheetTransition placement={placement} motion={mergedMotion} controlled={keepDOM} visible={visible}>
  206. {shouldRender ?
  207. transitionStyles => (
  208. <SideSheetContent
  209. {...contentProps}
  210. style={{ ...transitionStyles, ...style }}
  211. maskStyle={{ opacity: transitionStyles.opacity, ...maskStyle }}
  212. >
  213. {children}
  214. </SideSheetContent>
  215. ) : null}
  216. </SideSheetTransition>
  217. );
  218. }
  219. if (shouldRender) {
  220. return (
  221. <SideSheetContent {...contentProps} style={style} maskStyle={maskStyle}>
  222. {children}
  223. </SideSheetContent>
  224. );
  225. }
  226. return null;
  227. }
  228. render() {
  229. const {
  230. zIndex,
  231. getPopupContainer,
  232. } = this.props;
  233. let wrapperStyle: CSSProperties = {
  234. zIndex,
  235. };
  236. if (getPopupContainer) {
  237. wrapperStyle = {
  238. zIndex,
  239. position: 'static',
  240. };
  241. }
  242. return (
  243. <Portal getPopupContainer={getPopupContainer} style={wrapperStyle}>
  244. {this.renderContent()}
  245. </Portal>
  246. );
  247. }
  248. }