6-nestedBasicUsage.jsx 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import React, { useState, useRef } from 'react';
  2. import { Form, Col, Row, Button, ArrayField, Space } from '@douyinfe/semi-ui';
  3. import { IconMinusCircle, IconPlusCircle, IconRefresh } from '@douyinfe/semi-icons';
  4. const NestDemo = () => {
  5. const formRef = useRef();
  6. const getFormApi = () => {
  7. const formApi = formRef.current.formApi;
  8. return formApi;
  9. };
  10. const formInitValues = {
  11. data: [
  12. {
  13. name: '0',
  14. rules: [
  15. { desc: '0-0-desc', type: '0-0-type' },
  16. { desc: '0-1-desc', type: '0-1-type' },
  17. ],
  18. },
  19. {
  20. name: '1',
  21. rules: [
  22. { desc: '1-0-desc', type: '1-0-type' }
  23. ],
  24. }
  25. ]
  26. };
  27. // const change = () => {
  28. // };
  29. return (
  30. <Form
  31. ref={formRef}
  32. initValues={formInitValues}
  33. labelPosition='inset'
  34. >
  35. <ArrayField field='data'>
  36. {({ add, arrayFields, addWithInitValue }) => (
  37. <React.Fragment>
  38. <Space>
  39. <Button htmlType='reset' theme='solid' type='secondary' icon={<IconRefresh />}>Reset</Button>
  40. </Space>
  41. {
  42. arrayFields.map(({ field, key, remove }, i) => (
  43. <div key={key} style={{ width: 800, display: 'flex', flexWrap: 'wrap' }} id={`data-${i}`} className='line'>
  44. {/* {key} */}
  45. <Space>
  46. <Form.Input
  47. field={`${field}.name`}
  48. label={`${field}.name`}
  49. style={{ width: 700 }}
  50. id={`data-${i}-name`}
  51. >
  52. </Form.Input>
  53. <Button
  54. onClick={() => addWithInitValue({
  55. name: arrayFields.length,
  56. rules: [
  57. { desc: `${arrayFields.length}-0-desc`, type: `${arrayFields.length}-0-type` }
  58. ]
  59. })}
  60. icon={<IconPlusCircle/>}
  61. id={`data-${i}-add`}
  62. disabled={i !== arrayFields.length - 1}
  63. />
  64. <Button
  65. type='danger'
  66. onClick={remove}
  67. id={`data-${i}-remove`}
  68. icon={<IconMinusCircle />}
  69. />
  70. </Space>
  71. <ArrayField field={`${field}.rules`}>
  72. {({ add: addNested, arrayFields: nestedArrayFields, addWithInitValue: addNestedWithInitValue }, ) => (
  73. <React.Fragment>
  74. {
  75. nestedArrayFields.map(({ field: f, key: k, remove: r }, n) => (
  76. <section className='rules' key={k} style={{ display: 'flex', flexWrap: 'wrap', marginLeft: 36 }}>
  77. <Space>
  78. {/* {k} */}
  79. <Form.Input
  80. style={{ width: 280, marginRight: 12 }}
  81. field={`${f}.type`}
  82. label={`${f}.type`}
  83. id={`data-${i}-rule-${n}-type`}
  84. />
  85. <Form.Input
  86. style={{ width: 280 }}
  87. field={`${f}.desc`}
  88. label={`${f}.desc`}
  89. id={`data-${i}-rule-${n}-desc`}
  90. />
  91. <Button
  92. onClick={() => addNestedWithInitValue({ type: `${i}-${n+1}-type`, desc: `${i}-${n+1}-desc` })}
  93. icon={<IconPlusCircle/>}
  94. id={`data-${i}-rule-${n}-add`}
  95. disabled={n !== nestedArrayFields.length - 1}
  96. >
  97. add new line
  98. </Button>
  99. <Button
  100. type='danger'
  101. onClick={r}
  102. id={`data-${i}-rule-${n}-remove`}
  103. icon={<IconMinusCircle />}
  104. />
  105. </Space>
  106. </section>
  107. ))
  108. }
  109. </React.Fragment>
  110. )}
  111. </ArrayField>
  112. </div>
  113. ))
  114. }
  115. </React.Fragment>
  116. )}
  117. </ArrayField>
  118. </Form>
  119. );
  120. };
  121. NestDemo.storyName = 'ArrayField-Nested Level 2';
  122. export default NestDemo;
  123. // class NestArrayField extends React.Component {
  124. // change = () => {
  125. // let number = this.formApi.getValue('number');
  126. // let newData = {
  127. // group: [
  128. // { name: Math.random().toString().slice(0, 3), items: [ { itemName: Math.random(), type: '0-1' } ] },
  129. // // { name: Math.random(), items: [ { itemName: Math.random(), type: '0-1' } ] },
  130. // ]
  131. // };
  132. // this.formApi.setValues(newData, { isOverride: true });
  133. // }
  134. // }