form-schema-renderer 0.0.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 form-schema-renderer contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,253 @@
1
+ # form-schema-renderer
2
+
3
+ 一个 Vue 3 组件库,支持 JSON Schema 表单渲染和可视化表单设计器。
4
+
5
+ A Vue 3 component library that supports JSON Schema form rendering and a visual form designer.
6
+
7
+ ---
8
+
9
+ ## ✨ 特性 / Features
10
+
11
+ - 📋 **JSON Schema 自动渲染表单** — 根据 JSON Schema 配置自动生成表单
12
+ - 🎨 **可视化表单设计器** — 拖拽排序、属性配置,所见即所得
13
+ - 🔀 **条件可见逻辑(visibleIf)** — 支持 `equals`/`notEquals`/`exists`/`greaterThan`/`lessThan`/`greaterThanOrEqual`/`lessThanOrEqual` 操作符
14
+ - 🔗 **逻辑组合** — 支持 `allOf`(全部满足)/ `anyOf`(任一满足)组合条件
15
+ - 🌍 **多语言支持** — 内置中/英文,标签和描述支持 `{ zh, en }` 对象格式
16
+ - 🧩 **自定义 Widget** — `input` / `textarea` / `number` / `select` / `checkbox` / `date` / `file` / `rich-text`
17
+ - ✅ **AJV Schema 验证** — 基于 ajv 的实时表单验证
18
+ - 📁 **文件上传支持** — 内置文件选择与上传处理
19
+ - 🔄 **设计配置自动转换** — `configToSchema()` 将设计器配置自动转换为标准 JSON Schema
20
+
21
+ ---
22
+
23
+ ## 📦 安装 / Install
24
+
25
+ ```bash
26
+ npm install form-schema-renderer
27
+ ```
28
+
29
+ ### Peer Dependencies
30
+
31
+ | 依赖 | 版本 |
32
+ |------|------|
33
+ | vue | ^3.4.0 |
34
+ | naive-ui | ^2.44.0 |
35
+
36
+ `ajv` 已内置为库依赖,无需业务侧单独安装。
37
+
38
+ ---
39
+
40
+ ## 🚀 快速使用 / Quick Start
41
+
42
+ ### 渲染模式 / Render Mode
43
+
44
+ 通过 JSON Schema 配置自动渲染表单:
45
+
46
+ ```vue
47
+ <script setup lang="ts">
48
+ import { ref } from 'vue'
49
+ import { FormRenderer } from 'form-schema-renderer'
50
+ import 'form-schema-renderer/style.css'
51
+
52
+ const schema = ref({
53
+ type: 'object',
54
+ properties: {
55
+ name: { type: 'string', title: '姓名' },
56
+ age: { type: 'number', title: '年龄' }
57
+ }
58
+ })
59
+ const formData = ref({})
60
+ </script>
61
+
62
+ <template>
63
+ <FormRenderer :schema="schema" v-model="formData" />
64
+ </template>
65
+ ```
66
+
67
+ ### 设计器模式 / Designer Mode
68
+
69
+ 可视化拖拽设计表单,自动生成 JSON Schema:
70
+
71
+ ```vue
72
+ <script setup lang="ts">
73
+ import { ref } from 'vue'
74
+ import { FormRenderer, configToSchema } from 'form-schema-renderer'
75
+ import type { FormDesignConfig } from 'form-schema-renderer'
76
+
77
+ const designConfig = ref<FormDesignConfig>({ fields: [] })
78
+
79
+ // Convert design config to JSON Schema
80
+ const { schema, uiSchema } = configToSchema(designConfig.value)
81
+ </script>
82
+
83
+ <template>
84
+ <FormRenderer mode="design" v-model:designConfig="designConfig" />
85
+ </template>
86
+ ```
87
+
88
+ ### 独立使用设计器 / Standalone Designer
89
+
90
+ ```vue
91
+ <script setup lang="ts">
92
+ import { ref } from 'vue'
93
+ import { FormDesigner } from 'form-schema-renderer'
94
+ import type { FormDesignConfig } from 'form-schema-renderer'
95
+
96
+ const config = ref<FormDesignConfig>({ fields: [] })
97
+ </script>
98
+
99
+ <template>
100
+ <FormDesigner v-model="config" />
101
+ </template>
102
+ ```
103
+
104
+ ---
105
+
106
+ ## 📖 API 参考 / API Reference
107
+
108
+ ### FormRenderer Props
109
+
110
+ | Prop | 类型 | 默认值 | 说明 |
111
+ |------|------|--------|------|
112
+ | mode | `'render' \| 'design'` | `'render'` | 组件模式:渲染或设计 |
113
+ | schema | `object` | — | JSON Schema 对象(渲染模式) |
114
+ | uiSchema | `object` | `{}` | UI 配置,控制 Widget 类型和属性(渲染模式) |
115
+ | modelValue | `object` | — | 表单数据,`v-model` 绑定(渲染模式) |
116
+ | designConfig | `FormDesignConfig` | `{ fields: [] }` | 设计器配置,`v-model:designConfig` 绑定(设计模式) |
117
+ | locale | `string` | `'zh'` | 语言设置,支持 `'zh'` / `'en'` |
118
+
119
+ ### FormRenderer Events
120
+
121
+ | 事件 | 参数 | 说明 |
122
+ |------|------|------|
123
+ | update:modelValue | `(data: object)` | 表单数据变更时触发(渲染模式) |
124
+ | update:designConfig | `(config: FormDesignConfig)` | 设计配置变更时触发(设计模式) |
125
+ | validate | `(valid: boolean, errors: Record<string, string>)` | 表单验证结果,包含验证状态和错误信息 |
126
+
127
+ ---
128
+
129
+ ## 🎨 设计器配置格式 / Designer Config
130
+
131
+ ### FormDesignConfig
132
+
133
+ ```ts
134
+ interface FormDesignConfig {
135
+ title?: string | { zh: string; en: string } // Form title
136
+ fields: FormFieldConfig[] // Field list
137
+ }
138
+ ```
139
+
140
+ ### FormFieldConfig
141
+
142
+ | 字段 | 类型 | 说明 |
143
+ |------|------|------|
144
+ | key | `string` | 字段标识,唯一 |
145
+ | type | `'input' \| 'textarea' \| 'number' \| 'select' \| 'checkbox' \| 'date' \| 'file' \| 'rich-text'` | 组件类型 |
146
+ | label | `string \| { zh: string; en: string }` | 字段标签 |
147
+ | description | `string \| { zh: string; en: string }` | 字段描述(可选) |
148
+ | required | `boolean` | 是否必填 |
149
+ | placeholder | `string` | 占位文本(可选) |
150
+ | options | `FieldOption[]` | 选项列表,select 类型使用(可选) |
151
+ | props | `Record<string, any>` | 传递给底层 Widget 的额外属性(可选) |
152
+ | visibleIf | `VisibleIfRule` | 条件显示规则(可选) |
153
+ | validation | `{ min?, max?, minLength?, maxLength?, pattern? }` | 验证规则(可选) |
154
+
155
+ ### FieldOption
156
+
157
+ ```ts
158
+ interface FieldOption {
159
+ label: string | { zh: string; en: string }
160
+ value: string | number
161
+ }
162
+ ```
163
+
164
+ ---
165
+
166
+ ## 🔀 条件可见 / Conditional Visibility
167
+
168
+ 通过 `visibleIf` 配置字段的条件显示逻辑。
169
+
170
+ ### 支持的操作符 / Supported Operators
171
+
172
+ | 操作符 | 说明 | 示例 |
173
+ |--------|------|------|
174
+ | `equals` | 等于 | `{ field: 'gender', operator: 'equals', value: 'Male' }` |
175
+ | `notEquals` | 不等于 | `{ field: 'status', operator: 'notEquals', value: 'draft' }` |
176
+ | `exists` | 字段存在(有值) | `{ field: 'company', operator: 'exists' }` |
177
+ | `greaterThan` | 大于 | `{ field: 'age', operator: 'greaterThan', value: 18 }` |
178
+ | `lessThan` | 小于 | `{ field: 'age', operator: 'lessThan', value: 65 }` |
179
+ | `greaterThanOrEqual` | 大于等于 | `{ field: 'age', operator: 'greaterThanOrEqual', value: 18 }` |
180
+ | `lessThanOrEqual` | 小于等于 | `{ field: 'score', operator: 'lessThanOrEqual', value: 100 }` |
181
+
182
+ ### 逻辑组合 / Logic Combination
183
+
184
+ 使用 `allOf`(所有条件满足)或 `anyOf`(任一条件满足)组合多个条件:
185
+
186
+ ```json
187
+ {
188
+ "visibleIf": {
189
+ "logic": "allOf",
190
+ "conditions": [
191
+ { "field": "gender", "operator": "equals", "value": "Male" },
192
+ { "field": "age", "operator": "greaterThanOrEqual", "value": 18 }
193
+ ]
194
+ }
195
+ }
196
+ ```
197
+
198
+ ---
199
+
200
+ ## 📤 导出 / Exports
201
+
202
+ ```ts
203
+ // Components
204
+ export { FormRenderer } from 'form-schema-renderer'
205
+ export { FormDesigner } from 'form-schema-renderer'
206
+
207
+ // Utilities
208
+ export { configToSchema } from 'form-schema-renderer'
209
+
210
+ // Types
211
+ export type { FormDesignConfig, FormFieldConfig, FieldOption, VisibleIfCondition, VisibleIfRule, SchemaConvertResult } from 'form-schema-renderer'
212
+ ```
213
+
214
+ ---
215
+
216
+ ## 🛠 开发 / Development
217
+
218
+ ```bash
219
+ # Install dependencies
220
+ npm install
221
+
222
+ # Development mode
223
+ npm run dev
224
+
225
+ # Demo site (port 5173)
226
+ npm run dev:demo
227
+
228
+ # Build library
229
+ npm run build
230
+
231
+ # Build demo site
232
+ npm run build:demo
233
+
234
+ # Run tests
235
+ npm run test
236
+ ```
237
+
238
+ ---
239
+
240
+ ## 🔧 技术栈 / Tech Stack
241
+
242
+ - **Vue 3** — Composition API + `<script setup>`
243
+ - **TypeScript** — 类型安全
244
+ - **Vite** — 构建工具
245
+ - **Naive UI** — 表单组件库
246
+ - **AJV** — JSON Schema 验证
247
+ - **vuedraggable** — 拖拽排序
248
+
249
+ ---
250
+
251
+ ## 📄 License
252
+
253
+ MIT