1
0

modalFoundation.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. }
  55. export interface ModalState {
  56. hidden: 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. }
  80. afterClose() {
  81. this._adapter.notifyClose();
  82. }
  83. toggleHidden = (hidden: boolean, callback?: (hidden: boolean) => void) => {
  84. this._adapter.toggleHidden(hidden, callback);
  85. };
  86. // // eslint-disable-next-line max-len
  87. // mergeMotionProp = (motion: Motion, prop: string, cb: () => void) => {
  88. // const mergedMotion = typeof (motion) === 'undefined' || motion ? {
  89. // ...(motion as { [key: string]: (() => void) | boolean }),
  90. // [prop]: (...args: any) => {
  91. // const curr = get(motion, prop);
  92. // if (typeof curr === 'function') {
  93. // curr(...args);
  94. // }
  95. // cb();
  96. // }
  97. // } : false;
  98. // return mergedMotion;
  99. // };
  100. //
  101. // getMergedMotion() {
  102. // let { motion } = this._adapter.getProps();
  103. // const { keepDOM } = this._adapter.getProps();
  104. // motion = this.mergeMotionProp(motion, 'didLeave', this.afterClose.bind(this));
  105. // if (!keepDOM) {
  106. // return motion;
  107. // }
  108. // const mergedMotion = this.mergeMotionProp(motion, 'didLeave', this.toggleHidden.bind(this, true));
  109. // return mergedMotion;
  110. // }
  111. }