图像与设计
使用 @json-render/solid 构建…
当你需要在 SolidJS 项目中把 json-render spec 渲染为组件树,并配置 catalog、registry、providers、actions、bindings 或流式 UI 时使用。
🧪 尚未被验证(还没有小呱复用过的记录,不代表不好)
GDI C 38/100
进化评分(GDI)
综合排序依据 —— 四个维度的加权结果,让你看出「为什么这条排在前面」
内在质量 55/100 ×0.35
使用度 0/100 ×0.30
口碑 50/100 ×0.20
新鲜度 60/100 ×0.15
适用场景
使用 @json-render/soli特定
解决思路
- 在 App 中从 `@json-render/solid` 引入 `Renderer`、`JSONUIProvider`,并引入 `Spec` 类型与 `registry`。
- 用 `JSONUIProvider` 包裹 `Renderer`,传入 `registry` 与 `initialState`(例如 `{}`),并给 `Renderer` 传入 `spec` 与 `registry`。
- 创建 catalog 时,从 `@json-render/core` 引入 `defineCatalog`,从 `@json-render/solid/schema` 引入 `schema`,从 `zod` 引入 `z`。
- 调用 `defineCatalog(schema, { components, actions })`;在 `components` 中为每个组件定义 `props` 的 zod 对象和 `description`(例如 `Button` 的 `label`、`variant` 可为 `primary`/`secondary` 或 `null`,`Card` 的 `title`)。
- 在 `actions` 中定义 action 的 `description`(例如 `submit`)。
- 定义组件时接收 `ComponentRenderProps<P>`,字段包括 `element`、`children`、`emit`、`on`、`bindings`、`loading`。
- 组件可使用 `BaseComponentProps` 类型;在示例 `Button` 中通过 `props.emit('press')` 触发事件,并通过 `props.props.label` 读取 label。
- 创建 registry 时从 `@json-render/solid` 引入 `defineRegistry`,并引入 `catalog`、`Card`、`Button`。
- 调用 `defineRegistry(catalog, { components: { Card, Button }, actions: { submit: async (params, setState, state) => { ... } } })`,得到 `registry`、`handlers`、`executeAction`。
- 编写 spec 时使用 `{ root, elements }` 结构;`root` 指向根元素 id(如 `card1`)。
- 每个 `elements` 项包含 `type`、`props`、`children`;例如 `card1` 的 `type` 为 `Card`、`props.title` 为 `Hello`、`children` 为 `['btn1']`。
- 事件写在 `on` 中,例如 `btn1` 的 `on.press` 为 `{ action: 'submit' }`。
- 使用 `StateProvider`:管理 state model 读写,并通过 `store` 支持受控模式。
- 使用 `VisibilityProvider`:计算 `visible` 条件。
- 使用 `ValidationProvider`:做字段校验并集成 `validateForm`。
- 使用 `ActionProvider`:运行内置和自定义 actions。
- 使用 `JSONUIProvider`:作为组合 provider wrapper。
- 使用 `useStateStore`。
- 使用 `useStateValue`。
- 使用 `useStateBinding`。
- 使用 `useVisibility`。
- 使用 `useIsVisible`。
- 使用 `useActions`。
- 使用 `useAction`。
- 使用 `useValidation`。
- 使用 `useOptionalValidation`。
- 使用 `useFieldValidation`。
- 使用 `useBoundProp`。
- 使用 `useUIStream`。
- 使用 `useChatUI`。
- 内置 actions 由 `ActionProvider` 自动处理:`setState`。
- 内置 actions 由 `ActionProvider` 自动处理:`pushState`。
- 内置 actions 由 `ActionProvider` 自动处理:`removeState`。
- 内置 actions 由 `ActionProvider` 自动处理:`validateForm`。
- 动态 props 与 bindings:嵌套列表可在 enclosing repeat 内把 `repeat.statePath` 设为 `{ '$item': 'field' }`。
- 支持的表达式形式:`$state`,如 `{'$state': '/path'}`。
- 支持的表达式形式:`$bindState`,如 `{'$bindState': '/path'}`。
- 支持的表达式形式:`$bindItem`,如 `{'$bindItem': 'field'}`。
- 支持的表达式形式:`$template`,如 `{'$template': 'Hi ${/user/name}'}`。
- 支持的表达式形式:`$computed`,如 `{'$computed': 'fn', 'args': {...}}`。
- 支持的表达式形式:`$cond` / `$then` / `$else`,如 `{'$cond': <condition>, '$then': <value>, '$else': <value>}`。
- 在组件中用 `useBoundProp` 处理可写绑定值:调用 `const [value, setValue] = useBoundProp(props.props.value, props.bindings?.value)`。
- 在 `Input` 组件示例中,将 `<input>` 的 `value` 设为 `String(value() ?? '')`,并在 `onInput` 中调用 `setValue(e.currentTarget.value)`。
- `useStateValue`、`useStateBinding` 以及 `useFieldValidation` 的 `state` / `errors` / `isValid` 字段在 Solid 中是 reactive accessors;在 JSX、`createMemo` 或 `createEffect` 中作为函数调用。
- Solid 响应式规则:需要在值保持响应式时,不要在函数签名中解构 component props。
- Solid 响应式规则:把会变化的读取放在 JSX 表达式、`createMemo` 或 `createEffect` 内。
- Solid 响应式规则:context values 通过 getter-based objects 暴露,使消费者始终观察 live signals。
- Streaming UI:使用 `useUIStream`,传入 `{ api: '/api/generate-ui' }`。
- 调用 `await stream.send('Create a support dashboard')`。
- 将 `<Renderer>` 渲染为 `spec={stream.spec}`、`registry={registry}`、`loading={stream.isStreaming}`。
- 聊天 + UI 生成流程使用 `useChatUI`。
适用边界 · 注意事项
- 非 SolidJS 项目或不使用 `@json-render/solid` 时。
- 使用 React、Vue、Svelte 等其他渲染器时。
- 不需要 json-render 规范驱动 UI 时。