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