ModalContent.tsx 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /* eslint-disable eqeqeq */
  2. import React, { CSSProperties } from 'react';
  3. import PropTypes from 'prop-types';
  4. import cls from 'classnames';
  5. import { cssClasses } from '@douyinfe/semi-foundation/modal/constants';
  6. import ConfigContext from '../configProvider/context';
  7. import Button from '../iconButton';
  8. import Typography from '../typography';
  9. import BaseComponent from '../_base/baseComponent';
  10. // eslint-disable-next-line max-len
  11. import ModalContentFoundation, {
  12. ModalContentAdapter,
  13. ModalContentProps,
  14. ModalContentState
  15. } from '@douyinfe/semi-foundation/modal/modalContentFoundation';
  16. import { noop } from 'lodash-es';
  17. import { IconClose } from '@douyinfe/semi-icons';
  18. let uuid = 0;
  19. export default class ModalContent extends BaseComponent<ModalContentProps, ModalContentState> {
  20. static contextType = ConfigContext;
  21. static propTypes = {
  22. close: PropTypes.func,
  23. getContainerContext: PropTypes.func,
  24. contentClassName: PropTypes.string,
  25. maskClassName: PropTypes.string,
  26. onAnimationEnd: PropTypes.func
  27. };
  28. static defaultProps = {
  29. close: noop,
  30. getContainerContext: noop,
  31. contentClassName: '',
  32. maskClassName: ''
  33. };
  34. dialogId: string;
  35. private timeoutId: NodeJS.Timeout;
  36. constructor(props: ModalContentProps) {
  37. super(props);
  38. this.state = {
  39. dialogMouseDown: false,
  40. };
  41. this.foundation = new ModalContentFoundation(this.adapter);
  42. this.dialogId = `dialog-${uuid++}`;
  43. }
  44. get adapter(): ModalContentAdapter {
  45. return {
  46. ...super.adapter,
  47. notifyClose: (e: React.MouseEvent) => {
  48. this.props.onClose(e);
  49. },
  50. notifyDialogMouseDown: () => {
  51. this.setState({ dialogMouseDown: true });
  52. },
  53. notifyDialogMouseUp: () => {
  54. if (this.state.dialogMouseDown) {
  55. // Not setting setTimeout triggers close when modal external mouseUp
  56. this.timeoutId = setTimeout(() => {
  57. this.setState({ dialogMouseDown: false });
  58. }, 0);
  59. }
  60. },
  61. addKeyDownEventListener: () => {
  62. if (this.props.closeOnEsc) {
  63. document.addEventListener('keydown', this.foundation.handleKeyDown.bind(this.foundation));
  64. }
  65. },
  66. removeKeyDownEventListener: () => {
  67. if (this.props.closeOnEsc) {
  68. document.removeEventListener('keydown', this.foundation.handleKeyDown.bind(this.foundation));
  69. }
  70. },
  71. getMouseState: () => this.state.dialogMouseDown,
  72. };
  73. }
  74. componentDidMount() {
  75. this.foundation.handleKeyDownEventListenerMount();
  76. }
  77. componentWillUnmount() {
  78. clearTimeout(this.timeoutId);
  79. this.foundation.destroy();
  80. }
  81. onKeyDown = (e: React.MouseEvent) => {
  82. this.foundation.handleKeyDown(e);
  83. };
  84. // Record when clicking the modal box
  85. onDialogMouseDown = () => {
  86. this.foundation.handleDialogMouseDown();
  87. };
  88. // Cancel recording when clicking the modal box at the end
  89. onMaskMouseUp = () => {
  90. this.foundation.handleMaskMouseUp();
  91. };
  92. // onMaskClick will judge dialogMouseDown before onMaskMouseUp updates dialogMouseDown
  93. onMaskClick = (e: React.MouseEvent) => {
  94. this.foundation.handleMaskClick(e);
  95. };
  96. close = (e: React.MouseEvent) => {
  97. this.foundation.close(e);
  98. };
  99. getMaskElement = () => {
  100. const { ...props } = this.props;
  101. const { mask, maskClassName } = props;
  102. if (mask) {
  103. const className = cls(`${cssClasses.DIALOG}-mask`, {
  104. // [`${cssClasses.DIALOG}-mask-hidden`]: !props.visible,
  105. });
  106. return <div key="mask" className={cls(className, maskClassName)} style={props.maskStyle}/>;
  107. }
  108. return null;
  109. };
  110. renderCloseBtn = () => {
  111. const {
  112. closable,
  113. closeIcon,
  114. } = this.props;
  115. let closer;
  116. if (closable) {
  117. const iconType = closeIcon || <IconClose/>;
  118. closer = (
  119. <Button
  120. className={`${cssClasses.DIALOG}-close`}
  121. key="close-btn"
  122. onClick={this.close}
  123. type="tertiary"
  124. icon={iconType}
  125. theme="borderless"
  126. size="small"
  127. />
  128. );
  129. }
  130. return closer;
  131. };
  132. renderIcon = () => {
  133. const { icon } = this.props;
  134. return icon ? <span className={`${cssClasses.DIALOG}-icon-wrapper`}>{icon}</span> : null;
  135. };
  136. renderHeader = () => {
  137. if ('header' in this.props) {
  138. return this.props.header;
  139. }
  140. const { title } = this.props;
  141. const closer = this.renderCloseBtn();
  142. const icon = this.renderIcon();
  143. return (title === null || title === undefined) ?
  144. null :
  145. (
  146. <div className={`${cssClasses.DIALOG}-header`}>
  147. {icon}
  148. <Typography.Title heading={5} className={`${cssClasses.DIALOG}-title`}>{title}</Typography.Title>
  149. {closer}
  150. </div>
  151. );
  152. };
  153. renderBody = () => {
  154. const {
  155. bodyStyle,
  156. children,
  157. title,
  158. } = this.props;
  159. const bodyCls = cls(`${cssClasses.DIALOG}-body`, {
  160. [`${cssClasses.DIALOG}-withIcon`]: this.props.icon,
  161. });
  162. const closer = this.renderCloseBtn();
  163. const icon = this.renderIcon();
  164. const hasHeader = title !== null && title !== undefined || 'header' in this.props;
  165. return hasHeader ?
  166. <div className={bodyCls} style={bodyStyle}>{children}</div> :
  167. (
  168. <div className={`${cssClasses.DIALOG}-body-wrapper`}>
  169. {icon}
  170. <div className={bodyCls} style={bodyStyle}>{children}</div>
  171. {closer}
  172. </div>
  173. );
  174. };
  175. getDialogElement = () => {
  176. const { ...props } = this.props;
  177. const style: CSSProperties = {};
  178. const digCls = cls(`${cssClasses.DIALOG}`, {
  179. [`${cssClasses.DIALOG}-centered`]: props.centered,
  180. [`${cssClasses.DIALOG}-${props.size}`]: props.size,
  181. });
  182. if (props.width) {
  183. style.width = props.width;
  184. }
  185. if (props.height) {
  186. style.height = props.height;
  187. }
  188. if (props.isFullScreen) {
  189. style.width = '100%';
  190. style.height = '100%';
  191. style.margin = 'unset';
  192. }
  193. const body = this.renderBody();
  194. const header = this.renderHeader();
  195. const footer = props.footer ? <div className={`${cssClasses.DIALOG}-footer`}>{props.footer}</div> : null;
  196. const dialogElement = (
  197. <div
  198. key="dialog-element"
  199. className={digCls}
  200. onMouseDown={this.onDialogMouseDown}
  201. style={{ ...props.style, ...style }}
  202. id={this.dialogId}
  203. >
  204. <div
  205. onAnimationEnd={props.onAnimationEnd}
  206. className={cls([`${cssClasses.DIALOG}-content`,
  207. props.contentClassName,
  208. {[`${cssClasses.DIALOG}-content-fullScreen`]: props.isFullScreen }])}>
  209. {header}
  210. {body}
  211. {footer}
  212. </div>
  213. </div>
  214. );
  215. // return props.visible ? dialogElement : null;
  216. return dialogElement;
  217. };
  218. render() {
  219. const {
  220. maskClosable,
  221. className,
  222. getPopupContainer,
  223. maskFixed,
  224. getContainerContext,
  225. } = this.props;
  226. const { direction } = this.context;
  227. const classList = cls(className, {
  228. [`${cssClasses.DIALOG}-popup`]: getPopupContainer && !maskFixed,
  229. [`${cssClasses.DIALOG}-fixed`]: maskFixed,
  230. [`${cssClasses.DIALOG}-rtl`]: direction === 'rtl',
  231. });
  232. const containerContext = getContainerContext();
  233. const elem = (
  234. <div className={classList}>
  235. {this.getMaskElement()}
  236. <div
  237. role="modal"
  238. tabIndex={-1}
  239. className={`${cssClasses.DIALOG}-wrap`}
  240. onClick={maskClosable ? this.onMaskClick : null}
  241. onMouseUp={maskClosable ? this.onMaskMouseUp : null}
  242. >
  243. {this.getDialogElement()}
  244. </div>
  245. </div>
  246. );
  247. // @ts-ignore Unreachable branch
  248. // eslint-disable-next-line max-len
  249. return containerContext && containerContext.Provider ?
  250. <containerContext.Provider value={containerContext.value}>{elem}</containerContext.Provider> : elem;
  251. }
  252. }