Skip to content

Commit 106dc7c

Browse files
committed
Wiring update
1 parent 0ae767c commit 106dc7c

File tree

7 files changed

+54
-4
lines changed

7 files changed

+54
-4
lines changed

src/pg/json/__examples__/basic/basic.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ export default class XPgJsonBasic extends HTMLElement {
2020
}]
2121
}; // Array or Object
2222
this.$json.addEventListener('change', (e: any) => {
23-
const { parent, key, path, value } = e.detail;
23+
const { parent, key, value } = e.detail;
2424
if (value !== parent[key]) {
2525
parent[key] = value;
2626
}
27+
console.log(this.$json.value);
2728
});
2829
}
2930
}

src/pg/json/json.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,36 @@ export default class PgJson extends HTMLElement {
8686
if (typeof this.value === 'object') {
8787
const $object = document.createElement('pg-json-object') as PgJsonObject;
8888
$object.value = unwrapObject(this.value);
89+
$object.addEventListener('update', this.handleUpdate.bind(this));
8990
this.$container.appendChild($object);
9091
} else if (Array.isArray(this.value)) {
9192
const $array = document.createElement('pg-json-array') as PgJsonArray;
9293
$array.value = unwrapArray(this.value);
94+
$array.addEventListener('update', this.handleUpdate.bind(this));
9395
this.$container.appendChild($array);
9496
}
9597
}
9698
}
99+
100+
getParent(path: string[], parent) {
101+
const key = path.pop();
102+
if (key) {
103+
return this.getParent(path, parent[key]);
104+
}
105+
return parent;
106+
}
107+
108+
handleUpdate(e: any) {
109+
const { path, key, value } = e.detail;
110+
const parent = this.getParent(path, this.value);
111+
this.dispatchEvent(
112+
new CustomEvent('change', {
113+
detail: {
114+
parent,
115+
key,
116+
value,
117+
}
118+
})
119+
)
120+
}
97121
}

src/pg/jsonArray/jsonArray.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,21 @@ export default class PgJsonArray extends HTMLElement {
2727
container: this.$items,
2828
items: this.value,
2929
type: (item) => item.type,
30+
create: ($item, item) => {
31+
$item.addEventListener('update', (e: any) => {
32+
const { path, key, value } = e.detail;
33+
path.push(this.key);
34+
this.dispatchEvent(
35+
new CustomEvent('update', {
36+
detail: {
37+
path,
38+
key,
39+
value,
40+
}
41+
})
42+
);
43+
});
44+
},
3045
});
3146
}
3247

src/pg/jsonBoolean/jsonBoolean.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,10 @@ export default class PgJsonBoolean extends HTMLElement {
5454
items: this.options,
5555
});
5656
if (result !== undefined) {
57-
this.dispatchEvent(new CustomEvent('change', {
57+
this.dispatchEvent(new CustomEvent('update', {
5858
detail: {
59+
path: [this.key],
60+
key: this.key,
5961
value: result.value
6062
}
6163
}));

src/pg/jsonNumber/jsonNumber.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export default class PgJsonNumber extends HTMLElement {
2323
new CustomEvent('update', {
2424
detail: {
2525
path: [this.key],
26+
key: this.key,
2627
value: e.detail.value,
2728
}
2829
})

src/pg/jsonObject/jsonObject.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,19 @@ export default class PgJsonObject extends HTMLElement {
2929
type: (item) => item.type,
3030
create: ($item, item) => {
3131
$item.addEventListener('update', (e: any) => {
32+
const { path, key, value } = e.detail;
33+
path.push(this.key);
3234
this.dispatchEvent(
3335
new CustomEvent('update', {
34-
36+
detail: {
37+
path,
38+
key,
39+
value,
40+
}
3541
})
3642
);
3743
});
38-
}
44+
},
3945
});
4046
}
4147

src/pg/jsonString/jsonString.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export default class PgJsonString extends HTMLElement {
2323
new CustomEvent('update', {
2424
detail: {
2525
path: [this.key],
26+
key: this.key,
2627
value: e.detail.value,
2728
}
2829
})

0 commit comments

Comments
 (0)