1
0

modalFoundation.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. }
  15. export interface ModalProps {
  16. afterClose?: () => void;
  17. bodyStyle?: Record<string, any>;
  18. cancelButtonProps?: any;
  19. cancelText?: string;
  20. centered?: boolean;
  21. className?: string;
  22. closable?: boolean;
  23. confirmLoading?: boolean;
  24. cancelLoading?: boolean;
  25. content?: any;
  26. footer?: any;
  27. hasCancel?: boolean;
  28. header?: any;
  29. height?: string | number;
  30. mask?: boolean;
  31. maskClosable?: boolean;
  32. maskStyle?: Record<string, any>;
  33. maskFixed?: boolean;
  34. motion?: Motion;
  35. okButtonProps?: any;
  36. okText?: string;
  37. okType?: OKType;
  38. onCancel?: (e: any) => void | Promise<any>;
  39. onOk?: (e: any) => void | Promise<any>;
  40. style?: Record<string, any>;
  41. title?: any;
  42. visible?: boolean;
  43. width?: string | number;
  44. zIndex?: number;
  45. icon?: any;
  46. getPopupContainer?: () => HTMLElement;
  47. closeIcon?: any;
  48. closeOnEsc?: boolean;
  49. size?: Size;
  50. lazyRender?: boolean;
  51. keepDOM?: boolean;
  52. direction?: any;
  53. fullScreen?: boolean;
  54. preventScroll?: boolean;
  55. }
  56. export interface ModalState {
  57. hidden: boolean;
  58. isFullScreen: boolean;
  59. }
  60. export default class ModalFoundation extends BaseFoundation<ModalAdapter> {
  61. constructor(adapter: ModalAdapter) {
  62. super({
  63. ...adapter,
  64. });
  65. }
  66. destroy() {
  67. this.afterHide();
  68. }
  69. handleCancel(e: any) {
  70. this._adapter.notifyCancel(e);
  71. }
  72. handleOk(e: any) {
  73. this._adapter.notifyOk(e);
  74. }
  75. beforeShow() {
  76. this._adapter.disabledBodyScroll();
  77. }
  78. afterHide() {
  79. this._adapter.enabledBodyScroll();
  80. }
  81. afterClose() {
  82. this._adapter.notifyClose();
  83. }
  84. toggleHidden = (hidden: boolean, callback?: (hidden: boolean) => void) => {
  85. this._adapter.toggleHidden(hidden, 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. }