@pechynho/stimulus-typescript 0.0.11 → 0.0.12
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/utils.d.ts +1 -1
- package/dist/utils.js +12 -6
- package/package.json +1 -1
- package/src/utils.ts +12 -11
package/dist/utils.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { ActionEvent, Application, Controller } from "@hotwired/stimulus";
|
|
|
2
2
|
export declare const camelCase: (value: string) => string;
|
|
3
3
|
export declare const capitalize: (value: string) => string;
|
|
4
4
|
export declare function addStimulusAction(element: HTMLElement, identifier: string, method: string, event?: string, params?: Record<string, object | string | number | boolean>): void;
|
|
5
|
-
export declare function removeStimulusAction(element: HTMLElement, identifier: string, method: string, event?: string, removeParams?: boolean): void;
|
|
5
|
+
export declare function removeStimulusAction(element: HTMLElement, identifier: string, method: string, event?: string, removeParams?: boolean | string[]): void;
|
|
6
6
|
export declare const isActionEvent: (value: any) => value is ActionEvent;
|
|
7
7
|
export declare const getController: <T extends Controller>(app: Application, element: HTMLElement, identifier: string) => T | null;
|
|
8
8
|
export declare const getControllerAsync: <T extends Controller>(app: Application, element: HTMLElement, identifier: string, timeout?: number, poll?: number) => Promise<T | null>;
|
package/dist/utils.js
CHANGED
|
@@ -19,10 +19,9 @@ export function addStimulusAction(element, identifier, method, event, params) {
|
|
|
19
19
|
element.dataset.action = actions.join(' ');
|
|
20
20
|
if (params !== undefined) {
|
|
21
21
|
for (const [key, value] of Object.entries(params)) {
|
|
22
|
-
|
|
22
|
+
element.dataset[`${camelCase(identifier)}${capitalize(camelCase(key))}Param`] = typeof value === 'object'
|
|
23
23
|
? JSON.stringify(value)
|
|
24
24
|
: String(value);
|
|
25
|
-
element.dataset[`${camelCase(identifier)}${capitalize(camelCase(key))}Param`] = String(serialized);
|
|
26
25
|
}
|
|
27
26
|
}
|
|
28
27
|
}
|
|
@@ -40,11 +39,18 @@ export function removeStimulusAction(element, identifier, method, event, removeP
|
|
|
40
39
|
else {
|
|
41
40
|
delete element.dataset.action;
|
|
42
41
|
}
|
|
43
|
-
if (removeParams) {
|
|
44
|
-
|
|
45
|
-
|
|
42
|
+
if (removeParams === false) {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
const prefix = camelCase(identifier);
|
|
46
|
+
if (Array.isArray(removeParams)) {
|
|
47
|
+
for (const name of removeParams) {
|
|
48
|
+
delete element.dataset[`${prefix}${capitalize(camelCase(name))}Param`];
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
46
52
|
for (const key of Object.keys(element.dataset)) {
|
|
47
|
-
if (key.startsWith(prefix) && key.endsWith(
|
|
53
|
+
if (key.startsWith(prefix) && key.endsWith('Param')) {
|
|
48
54
|
delete element.dataset[key];
|
|
49
55
|
}
|
|
50
56
|
}
|
package/package.json
CHANGED
package/src/utils.ts
CHANGED
|
@@ -29,10 +29,9 @@ export function addStimulusAction(
|
|
|
29
29
|
element.dataset.action = actions.join(' ');
|
|
30
30
|
if (params !== undefined) {
|
|
31
31
|
for (const [key, value] of Object.entries(params)) {
|
|
32
|
-
|
|
32
|
+
element.dataset[`${camelCase(identifier)}${capitalize(camelCase(key))}Param`] = typeof value === 'object'
|
|
33
33
|
? JSON.stringify(value)
|
|
34
34
|
: String(value);
|
|
35
|
-
element.dataset[`${camelCase(identifier)}${capitalize(camelCase(key))}Param`] = String(serialized);
|
|
36
35
|
}
|
|
37
36
|
}
|
|
38
37
|
}
|
|
@@ -42,29 +41,31 @@ export function removeStimulusAction(
|
|
|
42
41
|
identifier: string,
|
|
43
42
|
method: string,
|
|
44
43
|
event?: string,
|
|
45
|
-
removeParams: boolean = false,
|
|
44
|
+
removeParams: boolean | string[] = false,
|
|
46
45
|
): void {
|
|
47
46
|
const existing = element.dataset.action;
|
|
48
47
|
if (existing === undefined || existing.trim() === '') {
|
|
49
48
|
return;
|
|
50
49
|
}
|
|
51
|
-
|
|
52
50
|
const target = `${identifier}#${method}`;
|
|
53
51
|
const descriptor = event !== undefined ? `${event}->${target}` : target;
|
|
54
52
|
const actions = existing.trim().split(/\s+/).filter((a) => a !== descriptor);
|
|
55
|
-
|
|
56
53
|
if (actions.length > 0) {
|
|
57
54
|
element.dataset.action = actions.join(' ');
|
|
58
55
|
} else {
|
|
59
56
|
delete element.dataset.action;
|
|
60
57
|
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
58
|
+
if (removeParams === false) {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
const prefix = camelCase(identifier);
|
|
62
|
+
if (Array.isArray(removeParams)) {
|
|
63
|
+
for (const name of removeParams) {
|
|
64
|
+
delete element.dataset[`${prefix}${capitalize(camelCase(name))}Param`];
|
|
65
|
+
}
|
|
66
|
+
} else {
|
|
66
67
|
for (const key of Object.keys(element.dataset)) {
|
|
67
|
-
if (key.startsWith(prefix) && key.endsWith(
|
|
68
|
+
if (key.startsWith(prefix) && key.endsWith('Param')) {
|
|
68
69
|
delete element.dataset[key];
|
|
69
70
|
}
|
|
70
71
|
}
|