2-removeUsage.jsx 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import React, { useState, useRef } from 'react';
  2. import { Form, Col, Row, Button, ArrayField, Space } from '@douyinfe/semi-ui';
  3. import { IconMinusCircle, IconPlusCircle } from '@douyinfe/semi-icons';
  4. function RemoveDemo() {
  5. const formRef = useRef();
  6. const formInitValues = {
  7. data: [
  8. { name: 'Semi D2C', role: 'Engineer' },
  9. { name: 'Semi C2D', role: 'Designer' },
  10. { name: 'Semi DSM', role: 'Designer' },
  11. ]
  12. };
  13. return (
  14. <Form ref={formRef} initValues={formInitValues}>
  15. <ArrayField field="data">
  16. {({ add, addWithInitValue, arrayFields }) => (
  17. <React.Fragment>
  18. <Space>
  19. <Button id='add' onClick={add} icon={<IconPlusCircle />} type="primary">Add</Button>
  20. <Button
  21. id='addWithInit'
  22. onClick={() => addWithInitValue({ name: `Semi New-${arrayFields.length + 1}`, role: 'Designer' })}
  23. icon={<IconPlusCircle />}
  24. type="primary">
  25. Add with row initValue
  26. </Button>
  27. </Space>
  28. {
  29. arrayFields.map(({ field, key, remove }, i) => (
  30. <div key={key} style={{ display: 'flex', width: 600 }} id={`data-${i}`} className='line'>
  31. <Space>
  32. <Form.Input
  33. id={`data-${i}-name`}
  34. field={`${field}[name]`}
  35. label={`${field}.name`}
  36. style={{ width: 200 }}
  37. />
  38. <Form.Input
  39. id={`data-${i}-role`}
  40. field={`${field}[role]`}
  41. label={`${field}.role`}
  42. style={{ width: 200 }}
  43. />
  44. </Space>
  45. <Button style={{ margin: "36px 0 0 12px" }} type="danger" icon={<IconMinusCircle/>} onClick={remove}>remove this line</Button>
  46. </div>
  47. ))
  48. }
  49. </React.Fragment>
  50. )}
  51. </ArrayField>
  52. </Form>
  53. );
  54. }
  55. RemoveDemo.storyName = 'ArrayField-Remove Row';
  56. export default RemoveDemo;