@mschop/alpine-web-components 1.0.0 → 1.0.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.
- package/AlpineWebComponent.ts +14 -16
- package/README.md +5 -3
- package/example/src/Input.ts +10 -1
- package/package.json +1 -1
package/AlpineWebComponent.ts
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
import Alpine from 'alpinejs';
|
|
2
2
|
|
|
3
|
+
export interface AttributeCast<T> {
|
|
4
|
+
fromAttribute: (value: string) => T
|
|
5
|
+
toAttribute: (value: T) => string
|
|
6
|
+
}
|
|
3
7
|
|
|
4
8
|
export interface AttributeCasts {
|
|
5
|
-
[keyof: string]:
|
|
9
|
+
[keyof: string]: AttributeCast<any>
|
|
6
10
|
}
|
|
7
11
|
|
|
8
12
|
export interface Attributes {
|
|
@@ -66,9 +70,7 @@ abstract class AlpineWebComponent extends HTMLElement {
|
|
|
66
70
|
return;
|
|
67
71
|
}
|
|
68
72
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
this.state.attributes[name] = this.castAttribute(name, newValue);
|
|
73
|
+
this.state.attributes[name] = this.castFromAttribute(name, newValue);
|
|
72
74
|
}
|
|
73
75
|
|
|
74
76
|
private loadTemplate() {
|
|
@@ -76,14 +78,6 @@ abstract class AlpineWebComponent extends HTMLElement {
|
|
|
76
78
|
}
|
|
77
79
|
|
|
78
80
|
private bootState() {
|
|
79
|
-
// const initStateJson = this.attributes['initial-state']
|
|
80
|
-
// if (typeof initStateJson === 'string') {
|
|
81
|
-
// const initState = JSON.parse(initStateJson);
|
|
82
|
-
// Object.keys(initState).forEach((key) => {
|
|
83
|
-
// this.state[key] = initState[key]
|
|
84
|
-
// })
|
|
85
|
-
// }
|
|
86
|
-
|
|
87
81
|
let state = this.state;
|
|
88
82
|
|
|
89
83
|
// @ts-ignore
|
|
@@ -114,12 +108,16 @@ abstract class AlpineWebComponent extends HTMLElement {
|
|
|
114
108
|
return;
|
|
115
109
|
}
|
|
116
110
|
|
|
117
|
-
this.state.attributes[key] = this.
|
|
111
|
+
this.state.attributes[key] = this.castFromAttribute(key, value)
|
|
118
112
|
})
|
|
119
113
|
}
|
|
120
114
|
|
|
121
|
-
private
|
|
122
|
-
return this.attributeCasts[name] === undefined ? value : this.attributeCasts[name](value);
|
|
115
|
+
private castToAttribute(name: string, value: any) {
|
|
116
|
+
return this.attributeCasts[name] === undefined ? value : this.attributeCasts[name].toAttribute(value);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
private castFromAttribute(name: string, value: string): any {
|
|
120
|
+
return this.attributeCasts[name] === undefined ? value : this.attributeCasts[name].fromAttribute(value);
|
|
123
121
|
}
|
|
124
122
|
|
|
125
123
|
protected getRoot(): this|ShadowRoot {
|
|
@@ -134,7 +132,7 @@ abstract class AlpineWebComponent extends HTMLElement {
|
|
|
134
132
|
|
|
135
133
|
protected updateAttribute(key: string): void
|
|
136
134
|
{
|
|
137
|
-
const newValue = this.state.attributes[key];
|
|
135
|
+
const newValue = this.castToAttribute(key, this.state.attributes[key]);
|
|
138
136
|
const currentValue = this.getAttribute(key);
|
|
139
137
|
if (newValue === currentValue) {
|
|
140
138
|
return;
|
package/README.md
CHANGED
|
@@ -14,14 +14,16 @@ The following example shows you a basic "Counter" example.
|
|
|
14
14
|
|
|
15
15
|
```ts
|
|
16
16
|
import AlpineWebComponent from "alpine-shadow-component";
|
|
17
|
+
import type {State} from "../../AlpineWebComponent";
|
|
17
18
|
|
|
18
|
-
interface State {
|
|
19
|
+
interface CounterState extends State {
|
|
19
20
|
counter: number,
|
|
20
21
|
}
|
|
21
22
|
|
|
22
23
|
class Counter extends AlpineWebComponent {
|
|
23
24
|
|
|
24
|
-
state:
|
|
25
|
+
state: CounterState = {
|
|
26
|
+
attributes: {},
|
|
25
27
|
counter: 1,
|
|
26
28
|
}
|
|
27
29
|
|
|
@@ -45,4 +47,4 @@ window.define('my-counter', Counter);
|
|
|
45
47
|
|
|
46
48
|
You should now be able to reference the counter with `<my-counter></my-counter>`
|
|
47
49
|
|
|
48
|
-
Please see directory `example` for
|
|
50
|
+
Please see directory `example` for example on how to bind / update attributes etc..
|
package/example/src/Input.ts
CHANGED
|
@@ -8,9 +8,14 @@ interface MyState extends State {
|
|
|
8
8
|
class Input extends AlpineWebComponent {
|
|
9
9
|
static observedAttributes = ['nth', 'data-value']
|
|
10
10
|
|
|
11
|
+
attributeCasts = {
|
|
12
|
+
nth: {fromAttribute: (x: string) => parseInt(x), toAttribute: (x: number) => x.toString()}
|
|
13
|
+
};
|
|
14
|
+
|
|
11
15
|
protected template: string = `
|
|
12
16
|
<div>
|
|
13
|
-
Input <span x-text="
|
|
17
|
+
Input für <span x-text="component.typeofNth()"></span> <span x-text="attributes.nth.toFixed(2)"></span>
|
|
18
|
+
</span><input x-model="attributes['data-value']" type="text" />
|
|
14
19
|
</div>
|
|
15
20
|
`;
|
|
16
21
|
|
|
@@ -25,6 +30,10 @@ class Input extends AlpineWebComponent {
|
|
|
25
30
|
protected useShadow(): boolean {
|
|
26
31
|
return true;
|
|
27
32
|
}
|
|
33
|
+
|
|
34
|
+
typeofNth(): string {
|
|
35
|
+
return typeof this.state.attributes.nth;
|
|
36
|
+
}
|
|
28
37
|
}
|
|
29
38
|
|
|
30
39
|
window.customElements.define('my-input', Input);
|