Schema 开发,核心的就是 Schema协议规范
formily Schema 字段文档 https://v1.formilyjs.org/#/0yTeT0/jAU8UVSYI8
Schema 是一份递归协议,主要用于描述数据结构,Formily做了扩展,可以支持描述 UI
schema 最终还是要转义为 jsx渲染到页面
{
"form": { // form属性
"labelCol": 6,
"wrapperCol": 12
},
"schema": {
"type": "object", // 提交的数据类型是对象
"properties": {}
},
"x-designable-id": "sg4mgvcj7a8"
}
schema
formily schema协议参考formily/packages/json-schema/src/schema.ts
type 字段类型
‘string’ | ‘object’ | ‘array’ | ‘number’ | string | ‘void’
- void 字段不提交数据,比如是 Button按钮,Row, Col列
type.object
type 指定为 object 或者 array 的时候,可以指定它的 properties 或者 items,继续向下递归描述
递归结构,其实也就是 object 和 array
{
"schema": {
"type": "object", // 提交的数据类型是对象
"properties": {} // 对象属性
}
type.array
{
"schema": {
"type": "array", // 提交的数据类型是 Array
"items": {} // 数组描述
}
{
"schema": {
"type": "object", // 提交的数据类型是对象
"properties": {
"users": {
"type": "array",
"x-decorator": "FormItem",
"x-component": "ArrayCards",
"x-component-props": {
"title": "Title"
},
"items": {
"type": "object",
"properties": {
"name": {
"type": "void",
"x-component": "ArrayCards.Index",
"x-designable-id": "sg4mgvcj7a8",
"x-index": 0
},
"phone": {
"type": "string",
"title": "Input",
"x-decorator": "FormItem",
"x-component": "Input",
"x-validator": [],
"x-component-props": {},
"x-decorator-props": {},
"name": "phone",
"x-designable-id": "sg4mgvcj7a8",
"x-index": 1
},
}
}
}
}
},
"x-designable-id": "sg4mgvcj7a8"
}
x-* 属性
对 JSON数据描述,增加了x-*属性,兼容数据描述与UI描述
- x-decorator-props 代表 FormItem 属性
- x-component 注册的 UI表单组件
- x-component-props UI表单组件的 props属性,参考 antd组件 API
- x-decorator,字段 UI 包装器组件,值一般都是FormItem
- x-reactions,代表字段联动
- x-editable,字段是否可编辑
- x-visible,字段是否显示
- x-display,代表字段展示模式,值为”none” | “visible” | “hidden”
- x-validator: [{triggerType:”onBlur”,validator:()=>…}] 字段校验时机
- x-validator 字段校验规则描述,代替 v1的 x-rules
const item = {
"type": "void",
"x-component": "Card",
"x-component-props": {
"title": "员工信息"
},
"x-designable-id": "m0dueq9kf8a",
"x-index": 0,
"name": "job",
"properties": {
"username": {
"type": "string",
"title": "姓名", // FormItem的 label
"required": true, // 必填
"x-decorator": "FormItem",
"x-component": "Input",
"x-component-props": {
"maxLength": 20,
"showCount": true,
"placeholder": "请输入姓名",
"prefix": "{{icon('UserOutlined')}}", // icon
},
"x-validator": [],
"x-decorator-props": {},
"x-designable-id": "cwr3pezmfio",
"x-index": 0,
"name": "username",
"x-reactions": {
"dependencies": [
{
"property": "value",
"type": "any"
}
],
"fulfill": {
"state": {
"decoratorProps": "{{{\n labelCol:6,\n offset: 4\n}}}"
}
}
}
}
- x-validator 字段校验规则描述,代替 v1的 x-rules