json-renderers 1.13.3 → 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 CHANGED
@@ -1,128 +1,272 @@
1
- # JSON Renderers
2
-
3
- A lightweight JavaScript library for rendering browser UI from JSON-driven specs. It turns structured data into DOM elements using JSON-based schema definitions and is designed for quick UI generation without hand-writing repetitive HTML.
4
-
5
- This repo is centered around a small dispatcher that chooses a renderer by `type` and then uses the matching spec-to-DOM pipeline.
6
-
7
- ## Features
8
-
9
- - Config-driven rendering using JSON specs
10
- - Built on top of `json-to-spec` and `@keshavsoft/json-to-tag`
11
- - Supports renderers such as:
12
- - `datalist`
13
- - `select`
14
- - Legacy renderer layers for table/navTabs/form patterns are present in older source folders
15
- - Simple browser-based demos and sample apps included in the repo
16
-
17
- ## Project structure
18
-
19
- ```text
20
- json-renderers/
21
- ├── src/ # main public entry and renderer implementations
22
- │ ├── index.js # package entry point
23
- │ └── v4/ # current renderer implementation set
24
- ├── samples/ # runnable browser examples
25
- ├── examples/ # additional demo usage
26
- ├── docs/ # docs/static assets
27
- ├── json-to-spec/ # bundled spec engine / related tooling
28
- ├── package.json # package metadata and scripts
29
- ├── vite.config.js # Vite config for local dev/build
30
- ├── LICENSE # package license (if present in your checkout)
31
- └── README.md # project documentation
32
- ```
33
-
34
- ## Installation
35
-
36
- Install from npm:
37
-
38
- ```bash
39
- npm install json-renderers
40
- ```
41
-
42
- For local development in this repo:
43
-
44
- ```bash
45
- npm install
46
- ```
47
-
48
- ## Usage
49
-
50
- ### Basic select renderer
51
-
52
- ```js
53
- import render from "json-renderers";
54
-
55
- const data = {
56
- LedgerName: [
57
- "Apex Industries",
58
- "Blue Valley Foods",
59
- "Crown Logistics"
60
- ]
61
- };
62
-
63
- render({
64
- type: "select",
65
- data,
66
- targetHtmlId: "dom-render-container"
67
- });
68
- ```
69
-
70
- ### Basic datalist renderer
71
-
72
- ```js
73
- import render from "json-renderers";
74
-
75
- const data = {
76
- LedgerName: [
77
- "Apex Industries",
78
- "Blue Valley Foods",
79
- "Crown Logistics"
80
- ]
81
- };
82
-
83
- render({
84
- type: "datalist",
85
- data,
86
- targetHtmlId: "dom-render-container"
87
- });
88
- ```
89
-
90
- The renderer resolves the type, looks up the matching implementation, and appends the resulting DOM into the target element.
91
-
92
- ## Running examples locally
93
-
94
- Start the Vite dev server:
95
-
96
- ```bash
97
- npm run dev
98
- ```
99
-
100
- Build the package for production:
101
-
102
- ```bash
103
- npm run build
104
- ```
105
-
106
- ## Example apps included
107
-
108
- The repo includes example and sample pages under:
109
-
110
- - `samples/`
111
- - `examples/`
112
- - `docs/`
113
-
114
- These demonstrate how to use the renderers in the browser with JSON input.
115
-
116
- ## Notes
117
-
118
- - The public package entry is `src/index.js`.
119
- - The main dispatcher currently registers `datalist` and `select` renderers in the `v4` renderer layer.
120
- - Older versions under `src/v1` through `src/v3` and related folders hint at a broader evolution of table, nav-tab, and form renderers.
121
-
122
- ## License
123
-
124
- This project is licensed under the MIT License.
125
-
126
- ## Maintainer
127
-
128
- KeshavSoft
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
+ [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
7
+ [![Version](https://img.shields.io/badge/version-1.13.6-emerald.svg)](package.json)
8
+ [![Layer: DOM Runtime](https://img.shields.io/badge/Layer-DOM%20Mounting%20Runtime-indigo.svg)](#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)