json-renderers 1.13.6 → 1.13.7
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 +272 -128
- package/docs/dist/min.js +702 -0
- package/docs/index.html +679 -13
- package/package.json +17 -6
package/README.md
CHANGED
|
@@ -1,128 +1,272 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
1
|
+
# json-renderers
|
|
2
|
+
|
|
3
|
+
> **Browser DOM Component Renderer for JSON-Driven UIs**
|
|
4
|
+
> Consumes declarative component specifications from `json-renderers-build` and mounts real DOM elements into target containers using `@keshavsoft/json-to-tag`.
|
|
5
|
+
|
|
6
|
+
[](https://opensource.org/licenses/MIT)
|
|
7
|
+
[](package.json)
|
|
8
|
+
[](#the-story-of-json-renderers)
|
|
9
|
+
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
## The Story of `json-renderers`
|
|
13
|
+
|
|
14
|
+
`json-renderers` is the **DOM Mounting & Runtime Renderer** of the KeshavSoft JSON-to-DOM ecosystem.
|
|
15
|
+
|
|
16
|
+
While its sister library [`json-renderers-build`](https://github.com/keshavsoft/json-renderers-build) handles **headless specification compilation** (pure data ➔ JSON AST), `json-renderers` is responsible for the **browser lifecycle**: resolving the target container, compiling the spec through `json-renderers-build`, instantiating concrete HTML elements via `@keshavsoft/json-to-tag`, and mounting them into the document.
|
|
17
|
+
|
|
18
|
+
---
|
|
19
|
+
|
|
20
|
+
## The 3-Tier Declarative Pipeline & The "In-Between" Story
|
|
21
|
+
|
|
22
|
+
```
|
|
23
|
+
┌────────────────────────────────────────────────────────────────────────┐
|
|
24
|
+
│ 1. Application Layer: Raw Data & Configurations │
|
|
25
|
+
│ data: [{ id: 101, name: "Alpha" }], columns: ["Name"] │
|
|
26
|
+
└───────────────────────────────────┬────────────────────────────────────┘
|
|
27
|
+
│ Calls: render({ type, data, columns, targetHtmlId })
|
|
28
|
+
▼
|
|
29
|
+
┌────────────────────────────────────────────────────────────────────────┐
|
|
30
|
+
│ 2. Spec Compiler: json-renderers-build (Headless AST Builder) │
|
|
31
|
+
│ • Skeletons for table, select, selectOptionsOnly │
|
|
32
|
+
│ • Compiled via json-to-spec engine │
|
|
33
|
+
│ • Output: Deterministic JSON-to-DOM AST (specAsJsonToDom) │
|
|
34
|
+
└───────────────────────────────────┬────────────────────────────────────┘
|
|
35
|
+
│ In-Between Handoff: specAsJsonToDom
|
|
36
|
+
▼
|
|
37
|
+
┌────────────────────────────────────────────────────────────────────────┐
|
|
38
|
+
│ 3. DOM Mounting Runtime: json-renderers (THIS REPO) │
|
|
39
|
+
│ • Passes spec to @keshavsoft/json-to-tag │
|
|
40
|
+
│ • Creates real HTML elements (HTMLTableElement, HTMLSelectElement) │
|
|
41
|
+
│ • Resolves document.getElementById(targetHtmlId) │
|
|
42
|
+
│ • Handles append / prepend / replacement mount strategies │
|
|
43
|
+
└───────────────────────────────────┬────────────────────────────────────┘
|
|
44
|
+
│ Mounts into DOM
|
|
45
|
+
▼
|
|
46
|
+
┌────────────────────────────────────────────────────────────────────────┐
|
|
47
|
+
│ 4. Live Browser DOM Container │
|
|
48
|
+
│ <div id="targetHtmlId"> <table>...</table> </div> │
|
|
49
|
+
└────────────────────────────────────────────────────────────────────────┘
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### The "In-Between": How `json-renderers` and `json-renderers-build` Connect
|
|
53
|
+
|
|
54
|
+
When you call `render({ type, targetHtmlId, data, columns, appendPosition })`:
|
|
55
|
+
|
|
56
|
+
1. **Spec Request:** `json-renderers` calls `json-renderers-build({ type, data, columns })`.
|
|
57
|
+
2. **Headless AST:** `json-renderers-build` merges the inputs with its internal component blueprints (`skeleton.json`) via `json-to-spec` and returns a pure specification tree (`specAsJsonToDom`).
|
|
58
|
+
3. **Fragment Unwrapping:** If the component is a fragment (such as `selectOptionsOnly` which has `{ children: [...] }` without a root `tagName`), `json-renderers` unwraps the `children` array.
|
|
59
|
+
4. **DOM Instantiation:** The specification is passed to `@keshavsoft/json-to-tag(jsonToSend)`, creating real browser `Node` or `NodeList` instances.
|
|
60
|
+
5. **DOM Mounting:** `json-renderers` locates `document.getElementById(targetHtmlId)`:
|
|
61
|
+
- If `appendPosition === "prepend"`, it executes `container.prepend(content)`.
|
|
62
|
+
- Otherwise, it clears `container.innerHTML = ""` and mounts `container.append(content)`.
|
|
63
|
+
- Returns the updated container element.
|
|
64
|
+
|
|
65
|
+
---
|
|
66
|
+
|
|
67
|
+
## Features
|
|
68
|
+
|
|
69
|
+
- **End-to-End JSON to DOM:** One function call turns your business data into fully rendered, styled UI controls.
|
|
70
|
+
- **Built on `json-renderers-build` (v13):**
|
|
71
|
+
- **`table`:** Complete responsive table with Bootstrap classes (`table table-hover table-striped mb-0`), headers, and rows.
|
|
72
|
+
- **`select`:** Standalone `<select id="LedgerName">` populated with dynamic options.
|
|
73
|
+
- **`selectOptionsOnly`:** Option fragments injected directly into pre-existing `<select>` elements.
|
|
74
|
+
- **Flexible Mounting Modes:** Replace container content (default) or `prepend` to existing content.
|
|
75
|
+
- **Global & ESM Distribution:** Usable via npm or directly in the browser via CDN script (`window.ks.jsonRenderers`).
|
|
76
|
+
|
|
77
|
+
---
|
|
78
|
+
|
|
79
|
+
## Installation
|
|
80
|
+
|
|
81
|
+
### NPM
|
|
82
|
+
|
|
83
|
+
```bash
|
|
84
|
+
npm install json-renderers
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### Browser (CDN / ES Module)
|
|
88
|
+
|
|
89
|
+
```html
|
|
90
|
+
<!-- Load ES module bundle directly from CDN -->
|
|
91
|
+
<script type="module" src="https://cdn.jsdelivr.net/gh/keshavsoft/json-renderers@main/docs/dist/v13/min.js"></script>
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
When loaded via `<script type="module">`, it automatically registers globally on:
|
|
95
|
+
```javascript
|
|
96
|
+
window.ks.jsonRenderers = {
|
|
97
|
+
meta: {
|
|
98
|
+
version: "v13.1.0",
|
|
99
|
+
description: "build table from store data and render to DOM uses json-to-spec, json-to-dom under the hood"
|
|
100
|
+
},
|
|
101
|
+
renderToDom: [Function: render]
|
|
102
|
+
};
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
---
|
|
106
|
+
|
|
107
|
+
## Quick Start & Usage
|
|
108
|
+
|
|
109
|
+
### 1. Rendering a Table into the DOM (`table`)
|
|
110
|
+
|
|
111
|
+
HTML:
|
|
112
|
+
```html
|
|
113
|
+
<div id="table-container"></div>
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
JavaScript:
|
|
117
|
+
```javascript
|
|
118
|
+
import render from "json-renderers";
|
|
119
|
+
|
|
120
|
+
const records = [
|
|
121
|
+
{ id: 101, name: "Alpha Enterprise", city: "Hyderabad" },
|
|
122
|
+
{ id: 102, name: "Beta Logistics", city: "Bengaluru" }
|
|
123
|
+
];
|
|
124
|
+
|
|
125
|
+
render({
|
|
126
|
+
type: "table",
|
|
127
|
+
targetHtmlId: "table-container",
|
|
128
|
+
data: records,
|
|
129
|
+
columns: ["Name", "City"]
|
|
130
|
+
});
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
*Result:* The table is rendered directly into `#table-container` with clean Bootstrap table classes.
|
|
134
|
+
|
|
135
|
+
---
|
|
136
|
+
|
|
137
|
+
### 2. Rendering a Select Dropdown (`select`)
|
|
138
|
+
|
|
139
|
+
HTML:
|
|
140
|
+
```html
|
|
141
|
+
<div id="dropdown-container"></div>
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
JavaScript:
|
|
145
|
+
```javascript
|
|
146
|
+
import render from "json-renderers";
|
|
147
|
+
|
|
148
|
+
render({
|
|
149
|
+
type: "select",
|
|
150
|
+
targetHtmlId: "dropdown-container",
|
|
151
|
+
data: ["Account Receivable", "Account Payable", "Sales Revenue"]
|
|
152
|
+
});
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
*Result:* Generates `<select id="LedgerName">` with three `<option>` children and mounts it into `#dropdown-container`.
|
|
156
|
+
|
|
157
|
+
---
|
|
158
|
+
|
|
159
|
+
### 3. Populating an Existing Select Element (`selectOptionsOnly`)
|
|
160
|
+
|
|
161
|
+
When your page already contains a `<select>` element and you only want to dynamically populate its options:
|
|
162
|
+
|
|
163
|
+
HTML:
|
|
164
|
+
```html
|
|
165
|
+
<select id="user-role-select" class="form-select">
|
|
166
|
+
<option value="" disabled selected>Select Role...</option>
|
|
167
|
+
</select>
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
JavaScript:
|
|
171
|
+
```javascript
|
|
172
|
+
import render from "json-renderers";
|
|
173
|
+
|
|
174
|
+
render({
|
|
175
|
+
type: "selectOptionsOnly",
|
|
176
|
+
targetHtmlId: "user-role-select",
|
|
177
|
+
data: ["Administrator", "Editor", "Viewer"]
|
|
178
|
+
});
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
*Result:* The `<option>` elements are appended directly into `#user-role-select`.
|
|
182
|
+
|
|
183
|
+
---
|
|
184
|
+
|
|
185
|
+
## API Reference
|
|
186
|
+
|
|
187
|
+
### `render(options)` / `default export`
|
|
188
|
+
|
|
189
|
+
The entry point exported by `json-renderers` accepts a single configuration object:
|
|
190
|
+
|
|
191
|
+
```javascript
|
|
192
|
+
render({
|
|
193
|
+
type = "table",
|
|
194
|
+
targetHtmlId,
|
|
195
|
+
data,
|
|
196
|
+
columns,
|
|
197
|
+
appendPosition,
|
|
198
|
+
showLog = false
|
|
199
|
+
})
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
| Parameter | Type | Default | Description |
|
|
203
|
+
|---|---|---|---|
|
|
204
|
+
| `type` | `string` | `"table"` | The component renderer to invoke: `"table"`, `"select"`, or `"selectOptionsOnly"`. |
|
|
205
|
+
| `targetHtmlId` | `string` | `undefined` | The ID of the DOM element (`document.getElementById(targetHtmlId)`) to mount into. |
|
|
206
|
+
| `data` | `Array` | `[]` | Data array. For `table`: array of row objects. For `select` / `selectOptionsOnly`: array of strings. |
|
|
207
|
+
| `columns` | `Array` | `undefined` | Column headers for `table`. Accepts an array of strings (e.g. `["Name"]`) or objects (e.g. `[{ title: "Name" }]`). |
|
|
208
|
+
| `appendPosition` | `string` | `undefined` | If set to `"prepend"`, prepends to the container. Otherwise replaces (`container.innerHTML = ""`). |
|
|
209
|
+
| `showLog` | `boolean` | `false` | Enables debug console logging during render execution. |
|
|
210
|
+
|
|
211
|
+
**Returns:** `HTMLElement` — The mounted DOM container.
|
|
212
|
+
|
|
213
|
+
---
|
|
214
|
+
|
|
215
|
+
## Comparison: `json-renderers` vs `json-renderers-build`
|
|
216
|
+
|
|
217
|
+
| Aspect | `json-renderers-build` | `json-renderers` (THIS REPO) |
|
|
218
|
+
|---|---|---|
|
|
219
|
+
| **Role** | Specification Builder / Compiler | DOM Mounting Runtime |
|
|
220
|
+
| **Output** | Declarative JSON AST (`specAsJsonToDom`) | Real HTML DOM Elements inserted into page |
|
|
221
|
+
| **DOM Dependency** | Zero (100% Headless & Isomorphic) | Browser DOM (`document.getElementById`) |
|
|
222
|
+
| **Environments** | Browser, Node.js, SSR, Web Workers | Browser runtime |
|
|
223
|
+
| **Core Dependency** | `json-to-spec` | `json-renderers-build`, `@keshavsoft/json-to-tag` |
|
|
224
|
+
| **Typical Caller** | `json-renderers`, Custom build pipelines | Web Applications, Dashboards, UI Views |
|
|
225
|
+
|
|
226
|
+
---
|
|
227
|
+
|
|
228
|
+
## Project Structure
|
|
229
|
+
|
|
230
|
+
```text
|
|
231
|
+
json-renderers/
|
|
232
|
+
├── docs/ # Documentation portal & distribution
|
|
233
|
+
│ ├── dist/ # Production bundles (Vite build)
|
|
234
|
+
│ │ ├── min.js # Latest bundle
|
|
235
|
+
│ │ └── v13/min.js # v13 production bundle
|
|
236
|
+
│ └── index.html # Interactive Documentation & Live DOM Workbench
|
|
237
|
+
├── samples/ # Runnable browser verification samples
|
|
238
|
+
│ ├── table/ # Table DOM mounting sample
|
|
239
|
+
│ ├── select/ # Select dropdown DOM mounting sample
|
|
240
|
+
│ └── selectOptionsOnly/ # Existing select options population sample
|
|
241
|
+
├── src/ # Source code
|
|
242
|
+
│ ├── index.js # Entry router (exports ./v13/index.js)
|
|
243
|
+
│ └── v13/ # CURRENT: Runtime DOM renderer
|
|
244
|
+
│ ├── buildSpec/ # In-between handoff: calls json-renderers-build & json-to-tag
|
|
245
|
+
│ ├── meta.js # Version & metadata descriptor
|
|
246
|
+
│ ├── registerGlobal.js # Global window.ks namespace attachment
|
|
247
|
+
│ └── index.js # DOM resolver and mounter
|
|
248
|
+
├── package.json # Package definition & scripts
|
|
249
|
+
├── vite.config.js # Vite build configuration
|
|
250
|
+
└── README.md # Complete repository guide
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
---
|
|
254
|
+
|
|
255
|
+
## Development & Build
|
|
256
|
+
|
|
257
|
+
```bash
|
|
258
|
+
# Install dependencies
|
|
259
|
+
npm install
|
|
260
|
+
|
|
261
|
+
# Start Vite development server
|
|
262
|
+
npm run dev
|
|
263
|
+
|
|
264
|
+
# Build production bundle (into docs/dist/v13/min.js and docs/dist/min.js)
|
|
265
|
+
npm run build
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
---
|
|
269
|
+
|
|
270
|
+
## License
|
|
271
|
+
|
|
272
|
+
MIT © [KeshavSoft](https://github.com/keshavsoft)
|