page.mdx 6.4 KB

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