modalFoundation.ts 3.7 KB

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