@vnejs/module.controller.tabs 0.2.1 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -7,16 +7,17 @@ export declare abstract class ModuleControllerTabs<TEvents extends ModuleControl
7
7
  abstract EVENT_HIDE: string;
8
8
  abstract EVENT_TAB_SET: string;
9
9
  abstract CONTROLS_ZINDEX: number;
10
- abstract TABS: Record<string, unknown>;
10
+ TABS: Record<string, unknown>;
11
11
  controls: {
12
12
  abstract_tab_prev: () => undefined;
13
13
  abstract_tab_next: () => undefined;
14
14
  };
15
15
  subscribe: () => void;
16
16
  getDefaultState: () => TState;
17
- onShow: () => Promise<unknown[]> | undefined;
17
+ getTabKeys: () => string[];
18
+ onShow: ({ tab }?: ModuleControllerTabsTabSetPayload) => Promise<unknown[]> | undefined;
18
19
  onHide: () => Promise<unknown[]> | undefined;
19
- onTabSet: ({ tab }?: ModuleControllerTabsTabSetPayload) => void | "" | undefined;
20
+ onTabSet: ({ tab }?: ModuleControllerTabsTabSetPayload) => void;
20
21
  onPrevTab: () => undefined;
21
22
  onNextTab: () => undefined;
22
23
  getNewTab: (isNext: boolean) => string;
package/dist/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { ModuleCore } from "@vnejs/module.core";
2
2
  export class ModuleControllerTabs extends ModuleCore {
3
+ TABS = {};
3
4
  controls = {
4
5
  [this.CONST.CONTROLS.BUTTONS.TAB_PREV]: () => void this.onPrevTab(),
5
6
  [this.CONST.CONTROLS.BUTTONS.TAB_NEXT]: () => void this.onNextTab(),
@@ -10,8 +11,9 @@ export class ModuleControllerTabs extends ModuleCore {
10
11
  this.on(this.EVENT_TAB_SET, this.onTabSet);
11
12
  };
12
13
  getDefaultState = () => ({});
13
- onShow = () => {
14
- this.updateState({ currentTab: Object.keys(this.TABS)[0] });
14
+ getTabKeys = () => Object.keys(this.TABS);
15
+ onShow = ({ tab } = {}) => {
16
+ this.updateState({ currentTab: tab ?? this.getTabKeys()[0] });
15
17
  return this.emit(this.EVENTS.CONTROLS.PUSH, {
16
18
  key: this.name,
17
19
  controls: this.controls,
@@ -20,11 +22,15 @@ export class ModuleControllerTabs extends ModuleCore {
20
22
  });
21
23
  };
22
24
  onHide = () => this.emit(this.EVENTS.CONTROLS.POP, { key: this.name });
23
- onTabSet = ({ tab } = {}) => tab && this.updateState({ currentTab: tab });
25
+ onTabSet = ({ tab } = {}) => {
26
+ if (tab === undefined || tab === "")
27
+ return;
28
+ this.updateState({ currentTab: tab });
29
+ };
24
30
  onPrevTab = () => void this.emit(this.EVENT_TAB_SET, { tab: this.getNewTab(false) });
25
31
  onNextTab = () => void this.emit(this.EVENT_TAB_SET, { tab: this.getNewTab(true) });
26
32
  getNewTab = (isNext) => {
27
- const keys = Object.keys(this.TABS);
33
+ const keys = this.getTabKeys();
28
34
  const currentTabIndex = keys.findIndex((key) => key === this.state.currentTab);
29
35
  if (isNext)
30
36
  return currentTabIndex === keys.length - 1 ? keys[0] : keys[(currentTabIndex + 1) % keys.length];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vnejs/module.controller.tabs",
3
- "version": "0.2.1",
3
+ "version": "0.2.3",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/src/index.ts CHANGED
@@ -33,7 +33,7 @@ export abstract class ModuleControllerTabs<
33
33
  abstract EVENT_TAB_SET: string;
34
34
 
35
35
  abstract CONTROLS_ZINDEX: number;
36
- abstract TABS: Record<string, unknown>;
36
+ TABS: Record<string, unknown> = {};
37
37
 
38
38
  controls = {
39
39
  [this.CONST.CONTROLS.BUTTONS.TAB_PREV]: () => void this.onPrevTab(),
@@ -48,8 +48,10 @@ export abstract class ModuleControllerTabs<
48
48
 
49
49
  getDefaultState = (): TState => ({}) as TState;
50
50
 
51
- onShow = () => {
52
- this.updateState({ currentTab: Object.keys(this.TABS)[0] } as Partial<TState>);
51
+ getTabKeys = () => Object.keys(this.TABS);
52
+
53
+ onShow = ({ tab }: ModuleControllerTabsTabSetPayload = {}) => {
54
+ this.updateState({ currentTab: tab ?? this.getTabKeys()[0] } as Partial<TState>);
53
55
 
54
56
  return this.emit(this.EVENTS.CONTROLS.PUSH, {
55
57
  key: this.name,
@@ -61,13 +63,17 @@ export abstract class ModuleControllerTabs<
61
63
 
62
64
  onHide = () => this.emit(this.EVENTS.CONTROLS.POP, { key: this.name } satisfies ControlsPopPayload);
63
65
 
64
- onTabSet = ({ tab }: ModuleControllerTabsTabSetPayload = {}) => tab && this.updateState({ currentTab: tab } as Partial<TState>);
66
+ onTabSet = ({ tab }: ModuleControllerTabsTabSetPayload = {}) => {
67
+ if (tab === undefined || tab === "") return;
68
+
69
+ this.updateState({ currentTab: tab } as Partial<TState>);
70
+ };
65
71
 
66
72
  onPrevTab = () => void this.emit(this.EVENT_TAB_SET, { tab: this.getNewTab(false) } satisfies ModuleControllerTabsTabSetPayload);
67
73
  onNextTab = () => void this.emit(this.EVENT_TAB_SET, { tab: this.getNewTab(true) } satisfies ModuleControllerTabsTabSetPayload);
68
74
 
69
75
  getNewTab = (isNext: boolean) => {
70
- const keys = Object.keys(this.TABS);
76
+ const keys = this.getTabKeys();
71
77
  const currentTabIndex = keys.findIndex((key) => key === this.state.currentTab);
72
78
 
73
79
  if (isNext) return currentTabIndex === keys.length - 1 ? keys[0] : keys[(currentTabIndex + 1) % keys.length];