@mschop/alpine-web-components 1.0.0 → 1.0.2
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 +19 -21
- package/README.md +6 -4
- package/example/src/Input.ts +10 -1
- package/package.json +2 -2
package/AlpineWebComponent.ts
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
import Alpine from 'alpinejs';
|
|
2
2
|
|
|
3
|
+
export interface AttributeCast<T> {
|
|
4
|
+
fromAttribute: (value: null|string) => T
|
|
5
|
+
toAttribute: (value: T) => null|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
|
|
@@ -110,16 +104,16 @@ abstract class AlpineWebComponent extends HTMLElement {
|
|
|
110
104
|
this.constructor.observedAttributes?.forEach((key: string) => {
|
|
111
105
|
const value = this.getAttribute(key);
|
|
112
106
|
|
|
113
|
-
|
|
114
|
-
return;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
this.state.attributes[key] = this.castAttribute(key, value)
|
|
107
|
+
this.state.attributes[key] = this.castFromAttribute(key, value)
|
|
118
108
|
})
|
|
119
109
|
}
|
|
120
110
|
|
|
121
|
-
private
|
|
122
|
-
return this.attributeCasts[name] === undefined ? value : this.attributeCasts[name](value);
|
|
111
|
+
private castToAttribute(name: string, value: any): null|string {
|
|
112
|
+
return this.attributeCasts[name] === undefined ? value : this.attributeCasts[name].toAttribute(value);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
private castFromAttribute(name: string, value: null|string): any {
|
|
116
|
+
return this.attributeCasts[name] === undefined ? value : this.attributeCasts[name].fromAttribute(value);
|
|
123
117
|
}
|
|
124
118
|
|
|
125
119
|
protected getRoot(): this|ShadowRoot {
|
|
@@ -134,12 +128,16 @@ abstract class AlpineWebComponent extends HTMLElement {
|
|
|
134
128
|
|
|
135
129
|
protected updateAttribute(key: string): void
|
|
136
130
|
{
|
|
137
|
-
const newValue = this.state.attributes[key];
|
|
131
|
+
const newValue = this.castToAttribute(key, this.state.attributes[key]);
|
|
138
132
|
const currentValue = this.getAttribute(key);
|
|
139
133
|
if (newValue === currentValue) {
|
|
140
134
|
return;
|
|
141
135
|
}
|
|
142
|
-
|
|
136
|
+
if (newValue === null) {
|
|
137
|
+
this.removeAttribute(key)
|
|
138
|
+
} else {
|
|
139
|
+
this.setAttribute(key, this.state.attributes[key])
|
|
140
|
+
}
|
|
143
141
|
this.dispatchEvent(
|
|
144
142
|
new CustomEvent(
|
|
145
143
|
'updated-' + key,
|
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
|
|
|
@@ -40,9 +42,9 @@ class Counter extends AlpineWebComponent {
|
|
|
40
42
|
}
|
|
41
43
|
}
|
|
42
44
|
|
|
43
|
-
window.define('my-counter', Counter);
|
|
45
|
+
window.customElements.define('my-counter', Counter);
|
|
44
46
|
```
|
|
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);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mschop/alpine-web-components",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "Use alpinejs with web components and shadow root",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
"type": "git",
|
|
11
11
|
"url": "git+ssh://git@gitlab.com/mschop/alpine-web-components.git"
|
|
12
12
|
},
|
|
13
|
-
"author": "",
|
|
13
|
+
"author": "mschop",
|
|
14
14
|
"license": "MIT",
|
|
15
15
|
"bugs": {
|
|
16
16
|
"url": "https://gitlab.com/mschop/alpine-web-components/issues"
|