modalFoundation.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import BaseFoundation, { DefaultAdapter } from '../base/foundation';
  2. export type OKType = 'primary' | 'secondary' | 'tertiary' | 'warning' | 'danger';
  3. export type Size = 'small' | 'medium' | 'large' | 'full-width';
  4. export interface ModalAdapter extends DefaultAdapter<ModalProps, ModalState> {
  5. disabledBodyScroll: () => void;
  6. enabledBodyScroll: () => void;
  7. notifyCancel: (e: any) => void;
  8. notifyOk: (e: any) => void;
  9. notifyClose: () => void;
  10. toggleDisplayNone: (displayNone: boolean, callback?: (displayNone: boolean) => void) => void;
  11. notifyFullScreen: (isFullScreen: boolean) => void;
  12. getProps: () => ModalProps
  13. }
  14. export interface ModalProps {
  15. afterClose?: () => void;
  16. bodyStyle?: Record<string, any>;
  17. cancelButtonProps?: any;
  18. cancelText?: string;
  19. centered?: boolean;
  20. className?: string;
  21. closable?: boolean;
  22. confirmLoading?: boolean;
  23. cancelLoading?: boolean;
  24. content?: any;
  25. footer?: any;
  26. hasCancel?: boolean;
  27. header?: any;
  28. height?: string | number;
  29. mask?: boolean;
  30. maskClosable?: boolean;
  31. maskStyle?: Record<string, any>;
  32. maskFixed?: boolean;
  33. motion?: boolean;
  34. okButtonProps?: any;
  35. okText?: string;
  36. okType?: OKType;
  37. onCancel?: (e: any) => void | Promise<any>;
  38. onOk?: (e: any) => void | Promise<any>;
  39. style?: Record<string, any>;
  40. title?: any;
  41. visible?: boolean;
  42. width?: string | number;
  43. zIndex?: number;
  44. icon?: any;
  45. getPopupContainer?: () => HTMLElement;
  46. closeIcon?: any;
  47. closeOnEsc?: boolean;
  48. size?: Size;
  49. lazyRender?: boolean;
  50. keepDOM?: boolean;
  51. direction?: any;
  52. fullScreen?: boolean;
  53. preventScroll?: boolean
  54. }
  55. export interface ModalState {
  56. displayNone: boolean;
  57. isFullScreen: boolean
  58. }
  59. export default class ModalFoundation extends BaseFoundation<ModalAdapter> {
  60. constructor(adapter: ModalAdapter) {
  61. super({
  62. ...adapter,
  63. });
  64. }
  65. destroy() {
  66. this.afterHide();
  67. }
  68. handleCancel(e: any) {
  69. this._adapter.notifyCancel(e);
  70. }
  71. handleOk(e: any) {
  72. this._adapter.notifyOk(e);
  73. }
  74. beforeShow() {
  75. this._adapter.disabledBodyScroll();
  76. }
  77. afterHide() {
  78. this._adapter.enabledBodyScroll();
  79. this._adapter.notifyClose();
  80. }
  81. // afterClose() {
  82. // this._adapter.notifyClose();
  83. // }
  84. toggleDisplayNone = (displayNone: boolean, callback?: (displayNone: boolean) => void) => {
  85. this._adapter.toggleDisplayNone(displayNone, callback);
  86. };
  87. // // eslint-disable-next-line max-len
  88. // mergeMotionProp = (motion: Motion, prop: string, cb: () => void) => {
  89. // const mergedMotion = typeof (motion) === 'undefined' || motion ? {
  90. // ...(motion as { [key: string]: (() => void) | boolean }),
  91. // [prop]: (...args: any) => {
  92. // const curr = get(motion, prop);
  93. // if (typeof curr === 'function') {
  94. // curr(...args);
  95. // }
  96. // cb();
  97. // }
  98. // } : false;
  99. // return mergedMotion;
  100. // };
  101. //
  102. // getMergedMotion() {
  103. // let { motion } = this._adapter.getProps();
  104. // const { keepDOM } = this._adapter.getProps();
  105. // motion = this.mergeMotionProp(motion, 'didLeave', this.afterClose.bind(this));
  106. // if (!keepDOM) {
  107. // return motion;
  108. // }
  109. // const mergedMotion = this.mergeMotionProp(motion, 'didLeave', this.toggleHidden.bind(this, true));
  110. // return mergedMotion;
  111. // }
  112. }