schema的缺点

    • 不支持函数,因为函数无法存储,例如函数里面有很多引用
    • 不支持jsx,因为 jsx序列化后,无法存储,例如引入了其他的库
    • 解决:高级功能:创建了自己的 DSL(领域特定语言)或抽象层

    根据传入的 JSONSchema渲染表单

    1. import React, { useMemo, useEffect } from 'react'
    2. import { createForm } from '@formily/core'
    3. import { createSchemaField } from '@formily/react'
    4. import {
    5. Form,
    6. FormItem,
    7. DatePicker,
    8. Checkbox,
    9. Cascader,
    10. Editable,
    11. Input,
    12. NumberPicker,
    13. Switch,
    14. Password,
    15. PreviewText,
    16. Radio,
    17. Reset,
    18. Select,
    19. Space,
    20. Submit,
    21. TimePicker,
    22. Transfer,
    23. TreeSelect,
    24. Upload,
    25. FormGrid,
    26. FormLayout,
    27. FormTab,
    28. FormCollapse,
    29. ArrayTable,
    30. ArrayCards,
    31. } from '@formily/antd'
    32. import { Card, Slider, Rate, Tag } from 'antd'
    33. const Text: React.FC<{
    34. value?: string
    35. content?: string
    36. mode?: 'normal' | 'h1' | 'h2' | 'h3' | 'p'
    37. }> = ({ value, mode, content, ...props }) => {
    38. const tagName = mode === 'normal' || !mode ? 'div' : mode
    39. return React.createElement(tagName, props, value || content)
    40. }
    41. const Tag1: React.FC<{
    42. value?: string
    43. mode?: 'normal' | 'h1' | 'h2' | 'h3' | 'p'
    44. }> = ({ value, mode, ...props }) => {
    45. return React.createElement(Tag, props, value)
    46. }
    47. const SchemaField = createSchemaField({
    48. components: {
    49. Space,
    50. FormGrid,
    51. FormLayout,
    52. FormTab,
    53. FormCollapse,
    54. ArrayTable,
    55. ArrayCards,
    56. FormItem,
    57. DatePicker,
    58. Checkbox,
    59. Cascader,
    60. Editable,
    61. Input,
    62. Text,
    63. NumberPicker,
    64. Switch,
    65. Password,
    66. PreviewText,
    67. Radio,
    68. Reset,
    69. Select,
    70. Submit,
    71. TimePicker,
    72. Transfer,
    73. TreeSelect,
    74. Upload,
    75. Card,
    76. Slider,
    77. Rate,
    78. Tag1,
    79. },
    80. })
    81. export interface IRenderJsonSchemaProps {
    82. form: {}
    83. schema: {}
    84. }
    85. const dataSource = [
    86. {
    87. id: 1,
    88. user: ['章鱼', '22章鱼'],
    89. age: 23,
    90. phone: '18915980989',
    91. like: 'online',
    92. address: '北京市晋升区朝阳大路',
    93. },
    94. {
    95. id: 2,
    96. user: '讲台',
    97. age: 18,
    98. phone: '18885980989',
    99. like: 'offline',
    100. address: '南京市谷口区朝阳大路',
    101. }
    102. ]
    103. export const RenderJsonSchema: React.FC<IRenderJsonSchemaProps> = (props) => {
    104. const form = useMemo(() => createForm(), [])
    105. const { form: formProps, schema } = props;
    106. useEffect(() => {
    107. form.setInitialValues({
    108. "ovnavgu2hv1": dataSource,
    109. });
    110. }, [dataSource]);
    111. return (
    112. <Form
    113. {...formProps}
    114. form={form}
    115. onAutoSubmit={console.log}
    116. >
    117. <SchemaField schema={schema} />
    118. </Form>
    119. )
    120. }