1. import React, { useState } from 'react';
    2. import { Button, Modal, Switch } from 'antd';
    3. import FormRender, { useForm } from 'form-render';
    4. const Demo = () => {
    5. const [show, set1] = useState(false);
    6. const form = useForm();
    7. const openModal = () => {
    8. form.setValues({ input1: 'haha' });
    9. set1(true);
    10. };
    11. return (
    12. <div>
    13. <Modal
    14. destroyOnClose
    15. visible={show}
    16. onOk={form.submit}
    17. onCancel={() => {
    18. set1(false);
    19. }}
    20. ></Modal>
    21. <FormRender
    22. displayType="row"
    23. // debugCss
    24. // mapping={{ '' }}
    25. form={form}
    26. schema={schema}
    27. widgets={{ mymy: Switch }}
    28. />
    29. <Button type="primary" onClick={openModal}>
    30. 打开form
    31. </Button>
    32. <Button type="primary" onClick={form.resetFields}>
    33. reset
    34. </Button>
    35. </div>
    36. );
    37. };
    38. export default Demo;
    39. var schema = {
    40. type: 'object',
    41. properties: {
    42. input1: {
    43. title: '简单输入框',
    44. type: 'string',
    45. required: true,
    46. },
    47. input2: {
    48. title: '简单输入框2',
    49. type: 'boolean',
    50. valuePropsName: 'checked',
    51. widget: 'mymy',
    52. },
    53. listName: {
    54. // widget: 'simpleList',
    55. props: {
    56. hideTitle: true,
    57. },
    58. title: '对象数组',
    59. description: '对象数组嵌套功能',
    60. type: 'array',
    61. items: {
    62. type: 'object',
    63. properties: {
    64. multiSelect: {
    65. title: '多选',
    66. description: '下拉多选',
    67. type: 'array',
    68. items: {
    69. type: 'string',
    70. },
    71. enum: ['A', 'B', 'C'],
    72. enumNames: ['杭州', '武汉', '湖州'],
    73. widget: 'multiSelect',
    74. },
    75. radioName: {
    76. title: '单选select',
    77. type: 'string',
    78. enum: ['a', 'b', 'c'],
    79. enumNames: ['早', '中', '晚'],
    80. widget: 'select',
    81. required: true,
    82. },
    83. inputName2: {
    84. title: '复杂输入框',
    85. description: '英文或数字组合',
    86. type: 'string',
    87. min: 4,
    88. max: 20,
    89. rules: [
    90. {
    91. pattern: '^[A-Za-z0-9]+$',
    92. message: '请输入正确格式',
    93. },
    94. ],
    95. },
    96. },
    97. },
    98. },
    99. },
    100. };