popconfirmFoundation.ts 3.0 KB

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