1
0

ModalContent.tsx 8.6 KB

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