index.tsx 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 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. 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. };
  125. }
  126. static getDerivedStateFromProps(props: SideSheetReactProps, prevState: SideSheetState) {
  127. const newState: Partial<SideSheetState> = {};
  128. if (props.visible && prevState.hidden) {
  129. newState.hidden = false;
  130. }
  131. if (!props.visible && !props.motion && !prevState.hidden) {
  132. newState.hidden = true;
  133. }
  134. return newState;
  135. }
  136. componentDidMount() {
  137. if (this.props.visible) {
  138. this.foundation.beforeShow();
  139. this._active = this._active || this.props.visible;
  140. }
  141. }
  142. componentDidUpdate(prevProps: SideSheetReactProps, prevState: SideSheetState, snapshot: any) {
  143. // hide => show
  144. if (!prevProps.visible && this.props.visible) {
  145. this.foundation.beforeShow();
  146. }
  147. // show => hide
  148. if (prevProps.visible && !this.props.visible) {
  149. this.foundation.afterHide();
  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. renderContent() {
  164. const {
  165. placement,
  166. className,
  167. children,
  168. width,
  169. height,
  170. motion,
  171. visible,
  172. style,
  173. maskStyle,
  174. size,
  175. zIndex,
  176. getPopupContainer,
  177. keepDOM,
  178. ...props
  179. } = this.props;
  180. const { direction } = this.context;
  181. const isVertical = placement === 'left' || placement === 'right';
  182. const isHorizontal = placement === 'top' || placement === 'bottom';
  183. const sheetWidth = isVertical ? (width ? width : defaultWidthList[size]) : '100%';
  184. const sheetHeight = isHorizontal ? (height ? height : defaultHeight) : '100%';
  185. const classList = cls(prefixCls, className, {
  186. [`${prefixCls}-${placement}`]: placement,
  187. [`${prefixCls}-popup`]: getPopupContainer,
  188. [`${prefixCls}-horizontal`]: isHorizontal,
  189. [`${prefixCls}-rtl`]: direction === 'rtl',
  190. [`${prefixCls}-hidden`]: keepDOM && this.state.hidden,
  191. });
  192. const contentProps = {
  193. ...props,
  194. visible,
  195. motion: false,
  196. className: classList,
  197. width: sheetWidth,
  198. height: sheetHeight,
  199. onClose: this.handleCancel,
  200. };
  201. const mergedMotion = this.foundation.getMergedMotion();
  202. this._active = this._active || visible;
  203. const shouldRender = (visible || keepDOM) && this._active;
  204. if (mergedMotion) {
  205. return (
  206. <SideSheetTransition placement={placement} motion={mergedMotion} controlled={keepDOM} visible={visible}>
  207. {shouldRender ?
  208. transitionStyles => (
  209. <SideSheetContent
  210. {...contentProps}
  211. style={{ ...transitionStyles, ...style }}
  212. maskStyle={{ opacity: transitionStyles.opacity, ...maskStyle }}
  213. >
  214. {children}
  215. </SideSheetContent>
  216. ) : null}
  217. </SideSheetTransition>
  218. );
  219. }
  220. if (shouldRender) {
  221. return (
  222. <SideSheetContent {...contentProps} style={style} maskStyle={maskStyle}>
  223. {children}
  224. </SideSheetContent>
  225. );
  226. }
  227. return null;
  228. }
  229. render() {
  230. const {
  231. zIndex,
  232. getPopupContainer,
  233. } = this.props;
  234. let wrapperStyle: CSSProperties = {
  235. zIndex,
  236. };
  237. if (getPopupContainer) {
  238. wrapperStyle = {
  239. zIndex,
  240. position: 'static',
  241. };
  242. }
  243. return (
  244. <Portal getPopupContainer={getPopupContainer} style={wrapperStyle}>
  245. {this.renderContent()}
  246. </Portal>
  247. );
  248. }
  249. }