page.mdx 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. import { pageMetadata } from "@/lib/page-metadata"
  2. export const metadata = pageMetadata("docs/data-binding")
  3. # Data Binding
  4. Connect UI elements to dynamic data using expressions in your JSON specs.
  5. ## State Model
  6. Every spec can include a `state` object that holds the data your UI reads from:
  7. ```json
  8. {
  9. "root": "greeting",
  10. "elements": {
  11. "greeting": {
  12. "type": "Text",
  13. "props": { "content": { "$state": "/user/name" } },
  14. "children": []
  15. }
  16. },
  17. "state": {
  18. "user": { "name": "Alice" }
  19. }
  20. }
  21. ```
  22. State can also be provided programmatically at runtime. In `@json-render/react`, this is done via `StateProvider` and hooks like `useStateStore`. See the [React API reference](/docs/api/react) for details.
  23. ## JSON Pointer Paths
  24. All paths in json-render follow JSON Pointer (RFC 6901). A path is a string of `/`-separated tokens starting from the root:
  25. ```
  26. Given this state:
  27. {
  28. "user": { "name": "Alice", "email": "alice@example.com" },
  29. "todos": [
  30. { "title": "Buy milk", "done": false },
  31. { "title": "Walk dog", "done": true }
  32. ]
  33. }
  34. "/user/name" -> "Alice"
  35. "/user/email" -> "alice@example.com"
  36. "/todos/0/title" -> "Buy milk"
  37. "/todos/1/done" -> true
  38. ```
  39. ## Expressions
  40. Expressions are special objects you place in props to read dynamic values instead of hardcoding them. There are six expression types.
  41. ### `$state` — Read from state
  42. Use `{ "$state": "/path" }` in any prop to read a value from the state model:
  43. ```json
  44. {
  45. "type": "Card",
  46. "props": {
  47. "title": { "$state": "/user/name" },
  48. "subtitle": { "$state": "/user/email" }
  49. },
  50. "children": []
  51. }
  52. ```
  53. If state contains `{ "user": { "name": "Alice", "email": "alice@example.com" } }`, the Card renders with title "Alice" and subtitle "alice@example.com".
  54. ### `$item` — Read from the current repeat item
  55. Use `{ "$item": "field" }` inside a [repeat](#repeat) to read a field from the current array item:
  56. ```json
  57. {
  58. "type": "Text",
  59. "props": { "content": { "$item": "title" } },
  60. "children": []
  61. }
  62. ```
  63. Use `{ "$item": "" }` to get the entire item object.
  64. ### `$index` — Current repeat index
  65. Use `{ "$index": true }` inside a [repeat](#repeat) to get the current array index (zero-based number):
  66. ```json
  67. {
  68. "type": "Text",
  69. "props": { "content": { "$index": true } },
  70. "children": []
  71. }
  72. ```
  73. ## Repeat
  74. The `repeat` field on an element renders its children once per item in a state array. It is a top-level field on the element, sibling of `type`, `props`, and `children` — not inside `props`.
  75. ```json
  76. {
  77. "root": "todo-list",
  78. "elements": {
  79. "todo-list": {
  80. "type": "Column",
  81. "props": { "gap": 8 },
  82. "repeat": { "statePath": "/todos", "key": "id" },
  83. "children": ["todo-item"]
  84. },
  85. "todo-item": {
  86. "type": "Card",
  87. "props": {
  88. "title": { "$item": "title" },
  89. "subtitle": { "$item": "description" }
  90. },
  91. "children": []
  92. }
  93. },
  94. "state": {
  95. "todos": [
  96. { "id": "1", "title": "Buy milk", "description": "2% or whole" },
  97. { "id": "2", "title": "Walk dog", "description": "Around the park" }
  98. ]
  99. }
  100. }
  101. ```
  102. - `repeat.statePath` — JSON Pointer to the state array
  103. - `repeat.key` — field name on each item to use as a stable key for rendering
  104. Inside `todo-item`, `{ "$item": "title" }` reads the `title` field from whichever array item is currently being rendered. `{ "$index": true }` would return `0` for the first item, `1` for the second, and so on.
  105. ## Two-Way Binding with `$bindState`
  106. Form components use `{ "$bindState": "/path" }` on their natural value prop for two-way binding. The component reads from and writes to the state path.
  107. ### Value prop (text inputs)
  108. ```json
  109. {
  110. "type": "TextInput",
  111. "props": {
  112. "value": { "$bindState": "/form/email" },
  113. "placeholder": "Enter your email"
  114. },
  115. "children": []
  116. }
  117. ```
  118. ### Checked prop (switches, checkboxes)
  119. ```json
  120. {
  121. "type": "Switch",
  122. "props": {
  123. "label": "Enable notifications",
  124. "checked": { "$bindState": "/settings/notifications" }
  125. },
  126. "children": []
  127. }
  128. ```
  129. ### Pressed prop (toggle buttons)
  130. ```json
  131. {
  132. "type": "ToggleButton",
  133. "props": {
  134. "label": "Bold",
  135. "pressed": { "$bindState": "/editor/bold" }
  136. },
  137. "children": []
  138. }
  139. ```
  140. ## Two-Way Binding with `$bindItem`
  141. Inside a repeat scope, use `{ "$bindItem": "field" }` to bind to a field on the current item:
  142. ```json
  143. {
  144. "type": "Switch",
  145. "props": {
  146. "label": "Done",
  147. "checked": { "$bindItem": "completed" }
  148. },
  149. "children": []
  150. }
  151. ```
  152. Use `{ "$bindItem": "" }` to bind to the entire item.
  153. `statePath` is not used for component binding. It remains for `repeat.statePath` (array iteration path) and action params like `setState.statePath` (target path for mutations).
  154. ## Conditional Props
  155. Use `$cond` / `$then` / `$else` to pick a prop value based on a condition:
  156. ```json
  157. {
  158. "type": "Badge",
  159. "props": {
  160. "label": {
  161. "$cond": { "$state": "/user/isAdmin" },
  162. "$then": "Admin",
  163. "$else": "Member"
  164. }
  165. },
  166. "children": []
  167. }
  168. ```
  169. The condition uses the same [visibility](/docs/visibility) expression format.
  170. ## Quick Reference
  171. <div className="my-6 overflow-x-auto">
  172. <table className="mdx-table w-full text-sm border-collapse">
  173. <thead>
  174. <tr>
  175. <th>Expression</th>
  176. <th>Syntax</th>
  177. <th>Context</th>
  178. </tr>
  179. </thead>
  180. <tbody>
  181. <tr>
  182. <td><code>{"$state"}</code></td>
  183. <td><code>{'{ "$state": "/path" }'}</code></td>
  184. <td>Anywhere</td>
  185. </tr>
  186. <tr>
  187. <td><code>{"$item"}</code></td>
  188. <td><code>{'{ "$item": "field" }'}</code></td>
  189. <td>Inside repeat only</td>
  190. </tr>
  191. <tr>
  192. <td><code>{"$index"}</code></td>
  193. <td><code>{'{ "$index": true }'}</code></td>
  194. <td>Inside repeat only</td>
  195. </tr>
  196. <tr>
  197. <td><code>{"$cond"}</code></td>
  198. <td><code>{'{ "$cond": ..., "$then": ..., "$else": ... }'}</code></td>
  199. <td>Anywhere</td>
  200. </tr>
  201. <tr>
  202. <td><code>{"$bindState"}</code></td>
  203. <td><code>{'{ "$bindState": "/path" }'}</code></td>
  204. <td>Form components (value, checked, pressed)</td>
  205. </tr>
  206. <tr>
  207. <td><code>{"$bindItem"}</code></td>
  208. <td><code>{'{ "$bindItem": "field" }'}</code></td>
  209. <td>Form components inside repeat</td>
  210. </tr>
  211. </tbody>
  212. </table>
  213. </div>
  214. ## External Store (Controlled Mode)
  215. For advanced use cases, you can pass a `StateStore` to `StateProvider` to use your own state management (Redux, Zustand, XState, etc.) instead of the built-in internal store:
  216. ```tsx
  217. import { createStateStore, type StateStore } from "@json-render/react";
  218. const store = createStateStore({ user: { name: "Alice" } });
  219. <StateProvider store={store}>
  220. {children}
  221. </StateProvider>
  222. // Mutate from anywhere — React re-renders automatically:
  223. store.set("/user/name", "Bob");
  224. ```
  225. When `store` is provided, `initialState` and `onStateChange` are ignored. The store is the single source of truth. See the [React API reference](/docs/api/react#external-store-controlled-mode) for the full `StateStore` interface.
  226. ## Next
  227. - [Visibility](/docs/visibility) — conditionally show or hide elements
  228. - [Action handlers](/docs/registry#action-handlers) — respond to user interactions
  229. - [React API reference](/docs/api/react) — React-specific hooks for programmatic state access