modalContentFoundation.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import BaseFoundation, { DefaultAdapter } from '../base/foundation';
  2. import KeyCode from '../utils/keyCode';
  3. import { ModalProps } from '../modal/modalFoundation';
  4. export interface ModalContentProps extends ModalProps{
  5. onClose: (e: any) => void;
  6. getContainerContext: () => any;
  7. isFullScreen?: boolean;
  8. }
  9. export interface ModalContentState{
  10. dialogMouseDown: boolean;
  11. }
  12. export interface ModalContentAdapter extends DefaultAdapter<ModalContentProps, ModalContentState>{
  13. notifyClose: (e: any) => void;
  14. notifyDialogMouseDown: () => void;
  15. notifyDialogMouseUp: () => void;
  16. addKeyDownEventListener: () => void;
  17. removeKeyDownEventListener: () => void;
  18. getMouseState: () => boolean;
  19. }
  20. export default class ModalContentFoundation extends BaseFoundation<ModalContentAdapter> {
  21. constructor(adapter: ModalContentAdapter) {
  22. super({ ...ModalContentFoundation.defaultAdapter, ...adapter });
  23. }
  24. destroy() {
  25. this.handleKeyDownEventListenerUnmount();
  26. }
  27. handleDialogMouseDown() {
  28. this._adapter.notifyDialogMouseDown();
  29. }
  30. handleMaskMouseUp() {
  31. this._adapter.notifyDialogMouseUp();
  32. }
  33. handleKeyDown(e: any) {
  34. const { closeOnEsc } = this.getProps();
  35. if (closeOnEsc && e.keyCode === KeyCode.ESC) {
  36. e.stopPropagation();
  37. this.close(e);
  38. return;
  39. }
  40. }
  41. handleKeyDownEventListenerMount() {
  42. this._adapter.addKeyDownEventListener();
  43. }
  44. handleKeyDownEventListenerUnmount() {
  45. this._adapter.removeKeyDownEventListener();
  46. }
  47. getMouseState() {
  48. this._adapter.getMouseState();
  49. }
  50. handleMaskClick(e: any) {
  51. const { dialogMouseDown } = this.getStates();
  52. if (e.target === e.currentTarget && !dialogMouseDown) {
  53. this.close(e);
  54. }
  55. }
  56. close(e: any) {
  57. this._adapter.notifyClose(e);
  58. }
  59. }