foundation.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import BaseFoundation, { DefaultAdapter } from '../base/foundation';
  2. import { numbers } from './constants';
  3. export interface UserGuideAdapter<P = Record<string, any>, S = Record<string, any>> extends DefaultAdapter<P, S>{
  4. notifyChange: (current: number) => void;
  5. notifyPrev: (current: number) => void;
  6. notifyNext: (current: number) => void;
  7. notifySkip: () => void;
  8. notifyFinish: () => void;
  9. setCurrent: (current: number) => void;
  10. disabledBodyScroll: () => void;
  11. enabledBodyScroll: () => void
  12. }
  13. export default class UserGuideFoundation <P = Record<string, any>, S = Record<string, any>> extends BaseFoundation<UserGuideAdapter<P, S>, P, S> {
  14. constructor(adapter: UserGuideAdapter<P, S>) {
  15. super({ ...adapter });
  16. }
  17. init() {
  18. }
  19. destroy() {
  20. this._adapter.enabledBodyScroll();
  21. }
  22. _notifyChange(current: number): void {
  23. const { current: stateCurrent } = this.getStates();
  24. if (stateCurrent !== current) {
  25. this._adapter.notifyChange(current);
  26. }
  27. }
  28. getIsControlledComponent(): boolean {
  29. return this._isInProps('current');
  30. }
  31. beforeShow() {
  32. this._adapter.disabledBodyScroll();
  33. }
  34. afterHide() {
  35. this._adapter.enabledBodyScroll();
  36. }
  37. getFinalPaading() {
  38. const { spotlightPadding, steps } = this.getProps();
  39. const { current } = this.getStates();
  40. const stepPadding = steps[current]?.spotlightPadding;
  41. const padding = typeof stepPadding === 'number' ? stepPadding :
  42. typeof spotlightPadding === 'number' ? spotlightPadding :
  43. numbers.DEFAULT_SPOTLIGHT_PADDING;
  44. return padding;
  45. }
  46. handlePrev = () => {
  47. const { current } = this.getStates();
  48. const newCurrent = current - 1;
  49. if (!this.getIsControlledComponent()) {
  50. this._adapter.setCurrent(newCurrent);
  51. }
  52. this._notifyChange(newCurrent);
  53. this._adapter.notifyPrev(newCurrent);
  54. };
  55. handleNext = () => {
  56. const { steps } = this.getProps();
  57. const { current } = this.getStates();
  58. const isLastStep = current === steps.length - 1;
  59. const newCurrent = isLastStep ? current : current + 1;
  60. if (isLastStep) {
  61. this._adapter.notifyFinish();
  62. } else {
  63. this._notifyChange(newCurrent);
  64. this._adapter.notifyNext(newCurrent);
  65. if (!this.getIsControlledComponent()) {
  66. this._adapter.setCurrent(newCurrent);
  67. }
  68. }
  69. };
  70. handleSkip = () => {
  71. this._adapter.notifySkip();
  72. };
  73. }