json-renderers-build 1.2.1 → 1.3.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-renderers-build",
3
- "version": "1.2.1",
3
+ "version": "1.3.2",
4
4
  "description": "Standalone, config-driven Table renderer built on top of json-to-dom",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
package/src/index.js CHANGED
@@ -1,2 +1,2 @@
1
- export * from "./v2/index.js";
2
- export { default } from "./v2/index.js";
1
+ export * from "./v3/index.js";
2
+ export { default } from "./v3/index.js";
@@ -0,0 +1,20 @@
1
+ const deriveColumnsFromData = ({ inData = [] } = {}) => {
2
+ const localData = inData;
3
+
4
+ if (!Array.isArray(localData) || localData.length === 0) {
5
+ return [];
6
+ }
7
+
8
+ const firstRow = localData[0];
9
+ if (!firstRow || typeof firstRow !== "object") {
10
+ return [];
11
+ }
12
+
13
+ return Object.keys(firstRow).map(key => ({
14
+ key,
15
+ label: key
16
+ }));
17
+ };
18
+
19
+ export { deriveColumnsFromData };
20
+ export default deriveColumnsFromData;
@@ -0,0 +1,39 @@
1
+ import registerGlobal from "./registerGlobal.js";
2
+
3
+ import renderTable from "./table/index.js";
4
+ import renderSelect from "./select/index.js";
5
+
6
+ const RENDERER_MAP = {
7
+ table: renderTable,
8
+ select: renderSelect
9
+ };
10
+
11
+ const render = ({
12
+ type = "table",
13
+ data,
14
+ columns
15
+ } = {}) => {
16
+ const rawType = type;
17
+ const resolvedType = typeof rawType === "string" ? rawType.toLowerCase() : "table";
18
+ const renderer = RENDERER_MAP[resolvedType];
19
+
20
+ if (!renderer) {
21
+ console.error(
22
+ `[Renderer] Unknown renderer type "${rawType}". Available types: ${Object.keys(RENDERER_MAP).join(", ")}`
23
+ );
24
+ return null;
25
+ }
26
+
27
+ const localData = data;
28
+ const localColumns = columns;
29
+
30
+ // Default to table renderer
31
+ return renderer({
32
+ inColumns: localColumns,
33
+ inData: localData
34
+ });
35
+ };
36
+
37
+ registerGlobal(render);
38
+
39
+ export default render;
package/src/v3/meta.js ADDED
@@ -0,0 +1,6 @@
1
+ export const meta = {
2
+ version: "v3.0.0",
3
+ description: "build table from store data and render to DOM uses json-to-spec, json-to-dom under the hood"
4
+ };
5
+
6
+ export default meta;
@@ -0,0 +1,14 @@
1
+ import meta from "./meta.js";
2
+
3
+ export const registerGlobal = (inFuncDefinition) => {
4
+ if (typeof globalThis === "undefined" || !inFuncDefinition) return;
5
+
6
+ globalThis.ks ??= {};
7
+
8
+ globalThis.ks.jsonRenderersBuild ??= {
9
+ meta,
10
+ renderToDom: inFuncDefinition
11
+ };
12
+ };
13
+
14
+ export default registerGlobal;
@@ -0,0 +1,18 @@
1
+ import jsonToSpec from "json-to-spec";
2
+
3
+ import skeletonJson from './skeleton.json' with { type: 'json' };
4
+
5
+ // --- Story of Table Render ---
6
+ const startFunc = ({
7
+ inData,
8
+ } = {}) => {
9
+ const localData = inData ?? [];
10
+
11
+ let specAsJsonToDom = jsonToSpec(skeletonJson, {
12
+ arrayOfStrings: localData
13
+ });
14
+
15
+ return specAsJsonToDom;
16
+ };
17
+
18
+ export default startFunc;
@@ -0,0 +1,18 @@
1
+ {
2
+ "tagName": "select",
3
+ "attributes": {
4
+ "id": "LedgerName"
5
+ },
6
+ "jsonToSpec": {
7
+ "operation": "loopArray",
8
+ "source": "arrayOfStrings",
9
+ "template": {
10
+ "tagName": "option",
11
+ "attributes": {
12
+ "value": "${}"
13
+ },
14
+ "textContent": "${}"
15
+ }
16
+ },
17
+ "children": []
18
+ }
@@ -0,0 +1,20 @@
1
+ import jsonToSpec from "json-to-spec";
2
+
3
+ import skeletonJson from './skeleton.json' with { type: 'json' };
4
+
5
+ // --- Story of Table Render ---
6
+ const startFunc = ({
7
+ inColumns,
8
+ inData,
9
+ } = {}) => {
10
+ const localData = inData ?? [];
11
+ const localColumns = inColumns;
12
+
13
+ let specAsJsonToDom = jsonToSpec(skeletonJson.default, {
14
+ columns: localColumns, data: localData
15
+ });
16
+
17
+ return specAsJsonToDom;
18
+ };
19
+
20
+ export default startFunc;
@@ -0,0 +1,47 @@
1
+ {
2
+ "default": {
3
+ "tagName": "table",
4
+ "attributes": {
5
+ "class": "table table-hover table-striped mb-0"
6
+ },
7
+ "children": [
8
+ {
9
+ "tagName": "thead",
10
+ "children": [
11
+ {
12
+ "tagName": "tr",
13
+ "jsonToSpec": {
14
+ "operation": "loopCollection",
15
+ "source": "columns",
16
+ "template": {
17
+ "tagName": "th",
18
+ "textContent": "${title}"
19
+ }
20
+ },
21
+ "children": []
22
+ }
23
+ ]
24
+ },
25
+ {
26
+ "tagName": "tbody",
27
+ "jsonToSpec": {
28
+ "operation": "loopCollection",
29
+ "source": "data",
30
+ "template": {
31
+ "tagName": "tr",
32
+ "jsonToSpec": {
33
+ "operation": "loopObject",
34
+ "source": "data",
35
+ "template": {
36
+ "tagName": "td",
37
+ "textContent": "${value}"
38
+ }
39
+ },
40
+ "children": []
41
+ }
42
+ },
43
+ "children": []
44
+ }
45
+ ]
46
+ }
47
+ }