popconfirmFoundation.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /* eslint-disable @typescript-eslint/no-empty-function */
  2. import BaseFoundation, { DefaultAdapter } from '../base/foundation';
  3. import isPromise from '../utils/isPromise';
  4. export interface PopconfirmAdapter<P = Record<string, any>, S = Record<string, any>> extends DefaultAdapter<P, S> {
  5. setVisible: (visible: boolean) => void;
  6. updateConfirmLoading: (loading: boolean) => void;
  7. updateCancelLoading: (loading: boolean) => void;
  8. notifyConfirm: (e: any) => Promise<any> | void;
  9. notifyCancel: (e: any) => Promise<any> | void;
  10. notifyVisibleChange: (visible: boolean) => void;
  11. notifyClickOutSide: (e: any) => void
  12. }
  13. export default class PopConfirmFoundation<P = Record<string, any>, S = Record<string, any>> extends BaseFoundation<PopconfirmAdapter<P, S>, P, S> {
  14. init(): void {}
  15. destroy(): void {}
  16. handleCancel(e: any): void {
  17. const maybePromise = this._adapter.notifyCancel(e) as Promise<any>;
  18. if (isPromise(maybePromise)) {
  19. this._adapter.updateCancelLoading(true);
  20. maybePromise.then(
  21. (result: any) => {
  22. this.handleVisibleChange(false);
  23. this._adapter.updateCancelLoading(false);
  24. },
  25. (errors: any) => {
  26. this._adapter.updateCancelLoading(false);
  27. }
  28. );
  29. } else {
  30. this.handleVisibleChange(false);
  31. }
  32. }
  33. handleConfirm(e: any): void {
  34. const maybePromise = this._adapter.notifyConfirm(e) as Promise<any>;
  35. if (isPromise(maybePromise)) {
  36. this._adapter.updateConfirmLoading(true);
  37. maybePromise.then(
  38. (result: any) => {
  39. this._adapter.updateConfirmLoading(false);
  40. this.handleVisibleChange(false);
  41. },
  42. (errors: any) => {
  43. this._adapter.updateConfirmLoading(false);
  44. }
  45. );
  46. } else {
  47. this.handleVisibleChange(false);
  48. }
  49. }
  50. handleClickOutSide(e: any): void {
  51. this._adapter.notifyClickOutSide(e);
  52. }
  53. handleVisibleChange(visible: boolean): void {
  54. if (!this._isControlledComponent('visible')) {
  55. this._adapter.setVisible(visible);
  56. }
  57. this._adapter.notifyVisibleChange(visible);
  58. }
  59. }