@vnejs/module.controller.tabs 0.2.1

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.
@@ -0,0 +1,23 @@
1
+ import { ModuleCore } from "@vnejs/module.core";
2
+ import type { ModuleControllerTabsConstants, ModuleControllerTabsEvents, ModuleControllerTabsParams, ModuleControllerTabsSettings, ModuleControllerTabsState, ModuleControllerTabsTabSetPayload } from "./types.js";
3
+ export type { ModuleControllerTabsConstants, ModuleControllerTabsEvents, ModuleControllerTabsParams, ModuleControllerTabsSettings, ModuleControllerTabsState, ModuleControllerTabsTabSetPayload, } from "./types.js";
4
+ export declare abstract class ModuleControllerTabs<TEvents extends ModuleControllerTabsEvents = ModuleControllerTabsEvents, TConstants extends ModuleControllerTabsConstants = ModuleControllerTabsConstants, TSettings extends ModuleControllerTabsSettings = ModuleControllerTabsSettings, TParams extends ModuleControllerTabsParams = ModuleControllerTabsParams, TState extends ModuleControllerTabsState = ModuleControllerTabsState> extends ModuleCore<TEvents, TConstants, TSettings, TParams, TState> {
5
+ abstract name: string;
6
+ abstract EVENT_SHOW: string;
7
+ abstract EVENT_HIDE: string;
8
+ abstract EVENT_TAB_SET: string;
9
+ abstract CONTROLS_ZINDEX: number;
10
+ abstract TABS: Record<string, unknown>;
11
+ controls: {
12
+ abstract_tab_prev: () => undefined;
13
+ abstract_tab_next: () => undefined;
14
+ };
15
+ subscribe: () => void;
16
+ getDefaultState: () => TState;
17
+ onShow: () => Promise<unknown[]> | undefined;
18
+ onHide: () => Promise<unknown[]> | undefined;
19
+ onTabSet: ({ tab }?: ModuleControllerTabsTabSetPayload) => void | "" | undefined;
20
+ onPrevTab: () => undefined;
21
+ onNextTab: () => undefined;
22
+ getNewTab: (isNext: boolean) => string;
23
+ }
package/dist/index.js ADDED
@@ -0,0 +1,33 @@
1
+ import { ModuleCore } from "@vnejs/module.core";
2
+ export class ModuleControllerTabs extends ModuleCore {
3
+ controls = {
4
+ [this.CONST.CONTROLS.BUTTONS.TAB_PREV]: () => void this.onPrevTab(),
5
+ [this.CONST.CONTROLS.BUTTONS.TAB_NEXT]: () => void this.onNextTab(),
6
+ };
7
+ subscribe = () => {
8
+ this.on(this.EVENT_SHOW, this.onShow);
9
+ this.on(this.EVENT_HIDE, this.onHide);
10
+ this.on(this.EVENT_TAB_SET, this.onTabSet);
11
+ };
12
+ getDefaultState = () => ({});
13
+ onShow = () => {
14
+ this.updateState({ currentTab: Object.keys(this.TABS)[0] });
15
+ return this.emit(this.EVENTS.CONTROLS.PUSH, {
16
+ key: this.name,
17
+ controls: this.controls,
18
+ index: this.CONTROLS_ZINDEX + 1,
19
+ checkNext: true,
20
+ });
21
+ };
22
+ onHide = () => this.emit(this.EVENTS.CONTROLS.POP, { key: this.name });
23
+ onTabSet = ({ tab } = {}) => tab && this.updateState({ currentTab: tab });
24
+ onPrevTab = () => void this.emit(this.EVENT_TAB_SET, { tab: this.getNewTab(false) });
25
+ onNextTab = () => void this.emit(this.EVENT_TAB_SET, { tab: this.getNewTab(true) });
26
+ getNewTab = (isNext) => {
27
+ const keys = Object.keys(this.TABS);
28
+ const currentTabIndex = keys.findIndex((key) => key === this.state.currentTab);
29
+ if (isNext)
30
+ return currentTabIndex === keys.length - 1 ? keys[0] : keys[(currentTabIndex + 1) % keys.length];
31
+ return currentTabIndex === 0 ? keys[keys.length - 1] : keys[(currentTabIndex - 1) % keys.length];
32
+ };
33
+ }
@@ -0,0 +1,12 @@
1
+ import type { ModuleCoreConstants, ModuleCoreEvents, ModuleCoreParams, ModuleCoreSettings } from "@vnejs/module.core";
2
+ import type { Constants as ControlsConstants, PluginName as ControlsPluginName, SubscribeEvents as ControlsSubscribeEvents } from "@vnejs/contracts.controls";
3
+ export type ModuleControllerTabsEvents = ModuleCoreEvents & Record<ControlsPluginName, ControlsSubscribeEvents>;
4
+ export type ModuleControllerTabsConstants = ModuleCoreConstants & Record<ControlsPluginName, ControlsConstants>;
5
+ export type ModuleControllerTabsSettings = ModuleCoreSettings;
6
+ export type ModuleControllerTabsParams = ModuleCoreParams;
7
+ export type ModuleControllerTabsState = {
8
+ currentTab?: string;
9
+ };
10
+ export type ModuleControllerTabsTabSetPayload = {
11
+ tab?: string;
12
+ };
package/dist/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "@vnejs/module.controller.tabs",
3
+ "version": "0.2.1",
4
+ "description": "",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./dist/index.d.ts",
10
+ "import": "./dist/index.js",
11
+ "require": "./dist/index.js",
12
+ "default": "./dist/index.js"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist",
17
+ "src",
18
+ "tsconfig.json"
19
+ ],
20
+ "scripts": {
21
+ "test": "echo \"Error: no test specified\" && exit 1",
22
+ "build": "npx @vnejs/monorepo package",
23
+ "publish:major": "npx @vnejs/monorepo publish major --access public",
24
+ "publish:minor": "npx @vnejs/monorepo publish minor --access public",
25
+ "publish:patch": "npx @vnejs/monorepo publish patch --access public"
26
+ },
27
+ "author": "",
28
+ "license": "ISC",
29
+ "peerDependencies": {
30
+ "@vnejs/module.core": "~0.2.0",
31
+ "@vnejs/contracts.controls": "~0.2.0",
32
+ "@vnejs/shared": "~0.2.0"
33
+ },
34
+ "devDependencies": {
35
+ "@vnejs/configs.ts-common": "~0.2.0",
36
+ "@vnejs/module.core": "~0.2.0",
37
+ "@vnejs/contracts.controls": "~0.2.0",
38
+ "@vnejs/shared": "~0.2.0"
39
+ }
40
+ }
package/src/index.ts ADDED
@@ -0,0 +1,77 @@
1
+ import { ModuleCore } from "@vnejs/module.core";
2
+ import type { ControlsPopPayload, ControlsPushPayload } from "@vnejs/contracts.controls";
3
+
4
+ import type {
5
+ ModuleControllerTabsConstants,
6
+ ModuleControllerTabsEvents,
7
+ ModuleControllerTabsParams,
8
+ ModuleControllerTabsSettings,
9
+ ModuleControllerTabsState,
10
+ ModuleControllerTabsTabSetPayload,
11
+ } from "./types.js";
12
+
13
+ export type {
14
+ ModuleControllerTabsConstants,
15
+ ModuleControllerTabsEvents,
16
+ ModuleControllerTabsParams,
17
+ ModuleControllerTabsSettings,
18
+ ModuleControllerTabsState,
19
+ ModuleControllerTabsTabSetPayload,
20
+ } from "./types.js";
21
+
22
+ export abstract class ModuleControllerTabs<
23
+ TEvents extends ModuleControllerTabsEvents = ModuleControllerTabsEvents,
24
+ TConstants extends ModuleControllerTabsConstants = ModuleControllerTabsConstants,
25
+ TSettings extends ModuleControllerTabsSettings = ModuleControllerTabsSettings,
26
+ TParams extends ModuleControllerTabsParams = ModuleControllerTabsParams,
27
+ TState extends ModuleControllerTabsState = ModuleControllerTabsState,
28
+ > extends ModuleCore<TEvents, TConstants, TSettings, TParams, TState> {
29
+ abstract override name: string;
30
+
31
+ abstract EVENT_SHOW: string;
32
+ abstract EVENT_HIDE: string;
33
+ abstract EVENT_TAB_SET: string;
34
+
35
+ abstract CONTROLS_ZINDEX: number;
36
+ abstract TABS: Record<string, unknown>;
37
+
38
+ controls = {
39
+ [this.CONST.CONTROLS.BUTTONS.TAB_PREV]: () => void this.onPrevTab(),
40
+ [this.CONST.CONTROLS.BUTTONS.TAB_NEXT]: () => void this.onNextTab(),
41
+ };
42
+
43
+ subscribe = () => {
44
+ this.on(this.EVENT_SHOW, this.onShow);
45
+ this.on(this.EVENT_HIDE, this.onHide);
46
+ this.on(this.EVENT_TAB_SET, this.onTabSet);
47
+ };
48
+
49
+ getDefaultState = (): TState => ({}) as TState;
50
+
51
+ onShow = () => {
52
+ this.updateState({ currentTab: Object.keys(this.TABS)[0] } as Partial<TState>);
53
+
54
+ return this.emit(this.EVENTS.CONTROLS.PUSH, {
55
+ key: this.name,
56
+ controls: this.controls,
57
+ index: this.CONTROLS_ZINDEX + 1,
58
+ checkNext: true,
59
+ } satisfies ControlsPushPayload);
60
+ };
61
+
62
+ onHide = () => this.emit(this.EVENTS.CONTROLS.POP, { key: this.name } satisfies ControlsPopPayload);
63
+
64
+ onTabSet = ({ tab }: ModuleControllerTabsTabSetPayload = {}) => tab && this.updateState({ currentTab: tab } as Partial<TState>);
65
+
66
+ onPrevTab = () => void this.emit(this.EVENT_TAB_SET, { tab: this.getNewTab(false) } satisfies ModuleControllerTabsTabSetPayload);
67
+ onNextTab = () => void this.emit(this.EVENT_TAB_SET, { tab: this.getNewTab(true) } satisfies ModuleControllerTabsTabSetPayload);
68
+
69
+ getNewTab = (isNext: boolean) => {
70
+ const keys = Object.keys(this.TABS);
71
+ const currentTabIndex = keys.findIndex((key) => key === this.state.currentTab);
72
+
73
+ if (isNext) return currentTabIndex === keys.length - 1 ? keys[0] : keys[(currentTabIndex + 1) % keys.length];
74
+
75
+ return currentTabIndex === 0 ? keys[keys.length - 1] : keys[(currentTabIndex - 1) % keys.length];
76
+ };
77
+ }
package/src/types.ts ADDED
@@ -0,0 +1,22 @@
1
+ import type { ModuleCoreConstants, ModuleCoreEvents, ModuleCoreParams, ModuleCoreSettings } from "@vnejs/module.core";
2
+ import type {
3
+ Constants as ControlsConstants,
4
+ PluginName as ControlsPluginName,
5
+ SubscribeEvents as ControlsSubscribeEvents,
6
+ } from "@vnejs/contracts.controls";
7
+
8
+ export type ModuleControllerTabsEvents = ModuleCoreEvents & Record<ControlsPluginName, ControlsSubscribeEvents>;
9
+
10
+ export type ModuleControllerTabsConstants = ModuleCoreConstants & Record<ControlsPluginName, ControlsConstants>;
11
+
12
+ export type ModuleControllerTabsSettings = ModuleCoreSettings;
13
+
14
+ export type ModuleControllerTabsParams = ModuleCoreParams;
15
+
16
+ export type ModuleControllerTabsState = {
17
+ currentTab?: string;
18
+ };
19
+
20
+ export type ModuleControllerTabsTabSetPayload = {
21
+ tab?: string;
22
+ };
package/tsconfig.json ADDED
@@ -0,0 +1,9 @@
1
+ {
2
+ "extends": "@vnejs/configs.ts-common/tsconfig.json",
3
+ "compilerOptions": {
4
+ "rootDir": "src",
5
+ "outDir": "dist"
6
+ },
7
+ "include": ["src/**/*.ts"],
8
+ "exclude": ["dist", "node_modules"]
9
+ }