gg-wf-scripts 2.4.0 → 2.5.0
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/README.md +19 -0
- package/package.json +1 -1
- package/src/data-engine.js +20 -0
package/README.md
CHANGED
|
@@ -86,6 +86,25 @@ Display data from your queries in the DOM.
|
|
|
86
86
|
|
|
87
87
|
`gg-field` supports dot-paths for nested data (e.g. `author.name`).
|
|
88
88
|
|
|
89
|
+
#### Passing data to web components / React
|
|
90
|
+
|
|
91
|
+
For elements that manage their own DOM (custom elements wrapping React, Lit, etc.), use `gg-data-key` to receive a JSON-serialized value as a `gg-data-value` attribute. The component listens for attribute changes and updates its own state.
|
|
92
|
+
|
|
93
|
+
```html
|
|
94
|
+
<div gg-data="post_form">
|
|
95
|
+
<!-- record.schools is e.g. [{ id, name }, ...] -->
|
|
96
|
+
<my-select gg-data-key="schools"></my-select>
|
|
97
|
+
</div>
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
After the query runs, the engine resolves the dot-path against the record and writes the result:
|
|
101
|
+
|
|
102
|
+
```html
|
|
103
|
+
<my-select gg-data-key="schools" gg-data-value='[{"id":1,"name":"Acme"}]'></my-select>
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
The lookup pierces shadow roots, so the marker can live inside a component's shadow DOM. Leaving `gg-data-key=""` passes the entire record. Note: if the component renders after the query runs, it won't be found — re-run the query (e.g. via `gg-data-on`) once the component has mounted, or have the component pull from `host.__ggRecord` on connect.
|
|
107
|
+
|
|
89
108
|
#### Re-running on URL changes
|
|
90
109
|
|
|
91
110
|
Add `gg-data-on` to re-run a query when specific URL params change:
|
package/package.json
CHANGED
package/src/data-engine.js
CHANGED
|
@@ -17,6 +17,23 @@ function applySwitchFields(root, record) {
|
|
|
17
17
|
});
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
+
function queryDeep(root, selector) {
|
|
21
|
+
const results = [...root.querySelectorAll(selector)];
|
|
22
|
+
root.querySelectorAll("*").forEach((el) => {
|
|
23
|
+
if (el.shadowRoot) results.push(...queryDeep(el.shadowRoot, selector));
|
|
24
|
+
});
|
|
25
|
+
return results;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function applyDataValues(root, record) {
|
|
29
|
+
queryDeep(root, "[gg-data-key]").forEach((el) => {
|
|
30
|
+
const path = el.getAttribute("gg-data-key");
|
|
31
|
+
const value = path ? getPath(record, path) : record;
|
|
32
|
+
if (value === undefined) return;
|
|
33
|
+
el.setAttribute("gg-data-value", JSON.stringify(value));
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
|
|
20
37
|
function populateFormFields(root, record) {
|
|
21
38
|
root.querySelectorAll(
|
|
22
39
|
"input[name], select[name], textarea[name]",
|
|
@@ -97,6 +114,7 @@ export function initDataEngine(context, { debug = false } = {}) {
|
|
|
97
114
|
clone.__ggRecord = record;
|
|
98
115
|
populateFields(clone, record);
|
|
99
116
|
applySwitchFields(clone, record);
|
|
117
|
+
applyDataValues(clone, record);
|
|
100
118
|
container.appendChild(clone);
|
|
101
119
|
});
|
|
102
120
|
} else if (isForm) {
|
|
@@ -109,6 +127,7 @@ export function initDataEngine(context, { debug = false } = {}) {
|
|
|
109
127
|
if (!result) return;
|
|
110
128
|
container.__ggRecord = result;
|
|
111
129
|
populateFormFields(container, result);
|
|
130
|
+
applyDataValues(container, result);
|
|
112
131
|
} else {
|
|
113
132
|
if (Array.isArray(result)) {
|
|
114
133
|
console.warn(
|
|
@@ -120,6 +139,7 @@ export function initDataEngine(context, { debug = false } = {}) {
|
|
|
120
139
|
container.__ggRecord = result;
|
|
121
140
|
populateFields(container, result);
|
|
122
141
|
applySwitchFields(container, result);
|
|
142
|
+
applyDataValues(container, result);
|
|
123
143
|
}
|
|
124
144
|
}
|
|
125
145
|
|