10-initCombineUsage.jsx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import React, { useState, useRef } from 'react';
  2. import { Form, Col, Row, Button, ArrayField, Space, useFormState } from '@douyinfe/semi-ui';
  3. import { IconMinusCircle, IconPlusCircle } from '@douyinfe/semi-icons';
  4. import { JSONTree } from 'react-json-tree';
  5. const FormStateTree = () => {
  6. const formState = useFormState();
  7. return (
  8. <JSONTree
  9. shouldExpandNodeInitially={() => true}
  10. hideRoot
  11. data={formState}
  12. />
  13. );
  14. };
  15. function InitCombineDemo() {
  16. const formRef = useRef();
  17. return (
  18. <Form ref={formRef} initValues={{
  19. data: [
  20. { name: 'NameInFormProp', role: 'RoleInFormProp' },
  21. ]
  22. }}>
  23. <div>
  24. <Button htmlType='reset'>Reset</Button>
  25. </div>
  26. {/* <ArrayField field="data"
  27. initValue={[{ name: 'NameInArrayFieldProp', role: 'RoleInArrayFieldProp' }]}
  28. >
  29. {({ add, addWithInitValue, arrayFields }) => (
  30. <React.Fragment>
  31. <Space>
  32. <Button id='dataAdd' onClick={add} icon={<IconPlusCircle />} type="primary">Add</Button>
  33. <Button
  34. id='dataAddWithInit'
  35. onClick={() => addWithInitValue({ name: `Data-${arrayFields.length + 1}`, role: 'Designer' })}
  36. icon={<IconPlusCircle />}
  37. type="primary">
  38. Add with row initValue
  39. </Button>
  40. </Space>
  41. {
  42. arrayFields.map(({ field, key, remove }, i) => (
  43. <div key={key} style={{ display: 'flex', width: 600 }} id={`data-${i}`} className='line'>
  44. <Space>
  45. <Form.Input
  46. id={`data-${i}-name`}
  47. field={`${field}[name]`}
  48. label={`${field}.name`}
  49. style={{ width: 200 }}
  50. />
  51. <Form.Input
  52. id={`data-${i}-role`}
  53. field={`${field}[role]`}
  54. label={`${field}.role`}
  55. style={{ width: 200 }}
  56. />
  57. </Space>
  58. <Button style={{ margin: "36px 0 0 12px" }} type="danger" icon={<IconMinusCircle/>} onClick={remove}>remove this line</Button>
  59. </div>
  60. ))
  61. }
  62. </React.Fragment>
  63. )}
  64. </ArrayField> */}
  65. <ArrayField field="dataB" initValue={[{ name: 'NameInArrayFieldProp', role: 'RoleInArrayFieldProp' }]}>
  66. {({ add, addWithInitValue, arrayFields }) => (
  67. <React.Fragment>
  68. <Space>
  69. <Button id='dataBAdd' onClick={add} icon={<IconPlusCircle />} type="primary">Add</Button>
  70. <Button
  71. id='dataBAddWithInit'
  72. onClick={() => addWithInitValue({ name: `Data-${arrayFields.length + 1}`, role: 'Designer' })}
  73. icon={<IconPlusCircle />}
  74. type="primary">
  75. Add with row initValue
  76. </Button>
  77. </Space>
  78. {
  79. arrayFields.map(({ field, key, remove }, i) => (
  80. <div key={key} style={{ display: 'flex', width: 600 }} id={`dataB-${i}`} className='line'>
  81. <Space>
  82. <Form.Input
  83. id={`dataB-${i}-name`}
  84. field={`${field}[name]`}
  85. label={`${field}.name`}
  86. style={{ width: 200 }}
  87. initValue={'NameInFieldProp'}
  88. />
  89. <Form.Input
  90. id={`dataB-${i}-role`}
  91. field={`${field}[role]`}
  92. label={`${field}.role`}
  93. style={{ width: 200 }}
  94. initValue={'RoleInFieldProp'}
  95. />
  96. </Space>
  97. <Button style={{ margin: "36px 0 0 12px" }} type="danger" icon={<IconMinusCircle/>} onClick={remove}>remove this line</Button>
  98. </div>
  99. ))
  100. }
  101. </React.Fragment>
  102. )}
  103. </ArrayField>
  104. <FormStateTree></FormStateTree>
  105. </Form>
  106. );
  107. }
  108. InitCombineDemo.storyName = 'ArrayField-init combine';
  109. export default InitCombineDemo;