schema的缺点
- 不支持函数,因为函数无法存储,例如函数里面有很多引用
- 不支持jsx,因为 jsx序列化后,无法存储,例如引入了其他的库
- 解决:高级功能:创建了自己的 DSL(领域特定语言)或抽象层
根据传入的 JSONSchema渲染表单
import React, { useMemo, useEffect } from 'react'
import { createForm } from '@formily/core'
import { createSchemaField } from '@formily/react'
import {
Form,
FormItem,
DatePicker,
Checkbox,
Cascader,
Editable,
Input,
NumberPicker,
Switch,
Password,
PreviewText,
Radio,
Reset,
Select,
Space,
Submit,
TimePicker,
Transfer,
TreeSelect,
Upload,
FormGrid,
FormLayout,
FormTab,
FormCollapse,
ArrayTable,
ArrayCards,
} from '@formily/antd'
import { Card, Slider, Rate, Tag } from 'antd'
const Text: React.FC<{
value?: string
content?: string
mode?: 'normal' | 'h1' | 'h2' | 'h3' | 'p'
}> = ({ value, mode, content, ...props }) => {
const tagName = mode === 'normal' || !mode ? 'div' : mode
return React.createElement(tagName, props, value || content)
}
const Tag1: React.FC<{
value?: string
mode?: 'normal' | 'h1' | 'h2' | 'h3' | 'p'
}> = ({ value, mode, ...props }) => {
return React.createElement(Tag, props, value)
}
const SchemaField = createSchemaField({
components: {
Space,
FormGrid,
FormLayout,
FormTab,
FormCollapse,
ArrayTable,
ArrayCards,
FormItem,
DatePicker,
Checkbox,
Cascader,
Editable,
Input,
Text,
NumberPicker,
Switch,
Password,
PreviewText,
Radio,
Reset,
Select,
Submit,
TimePicker,
Transfer,
TreeSelect,
Upload,
Card,
Slider,
Rate,
Tag1,
},
})
export interface IRenderJsonSchemaProps {
form: {}
schema: {}
}
const dataSource = [
{
id: 1,
user: ['章鱼', '22章鱼'],
age: 23,
phone: '18915980989',
like: 'online',
address: '北京市晋升区朝阳大路',
},
{
id: 2,
user: '讲台',
age: 18,
phone: '18885980989',
like: 'offline',
address: '南京市谷口区朝阳大路',
}
]
export const RenderJsonSchema: React.FC<IRenderJsonSchemaProps> = (props) => {
const form = useMemo(() => createForm(), [])
const { form: formProps, schema } = props;
useEffect(() => {
form.setInitialValues({
"ovnavgu2hv1": dataSource,
});
}, [dataSource]);
return (
<Form
{...formProps}
form={form}
onAutoSubmit={console.log}
>
<SchemaField schema={schema} />
</Form>
)
}