mountAndAdd.jsx 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import { ArrayField, TextArea, Form, Button, useFormState } from '@douyinfe/semi-ui';
  2. import React, { useRef, useState } from 'react';
  3. const Demo = () => {
  4. const formRef = useRef();
  5. const [flag, setFlag] = useState(false);
  6. return (
  7. <Form ref={formRef} onValueChange={values => console.log(values)} style={{ width: 250 }}>
  8. <Button type="primary" onClick={() => setFlag(!flag)} className="btn-margin-right">
  9. {!flag ? '开启' : '关闭'}
  10. </Button>
  11. {flag && (
  12. <ArrayField field="rules">
  13. {({ add, arrayFields, addWithInitValue }) => (
  14. <React.Fragment>
  15. <Button onClick={add} theme="light">
  16. Add new line
  17. </Button>
  18. <Button
  19. onClick={() => {
  20. addWithInitValue({ name: 'Semi DSM', type: 'Designer' });
  21. }}
  22. style={{ marginLeft: 8 }}
  23. >
  24. Add new line with init value
  25. </Button>
  26. {arrayFields.map(({ field, key, remove }, i) => (
  27. <div key={key} style={{ width: 1000, display: 'flex' }}>
  28. <Form.Input
  29. field={`${field}[name]`}
  30. label={`${field}.name`}
  31. style={{ width: 200, marginRight: 16 }}
  32. />
  33. <Form.Select
  34. field={`${field}[role]`}
  35. label={`${field}.role`}
  36. style={{ width: 120 }}
  37. optionList={[
  38. { label: 'Engineer', value: 'Engineer' },
  39. { label: 'Designer', value: 'Designer' },
  40. ]}
  41. />
  42. <Button type="danger" theme="borderless" onClick={remove} style={{ margin: 12 }} />
  43. </div>
  44. ))}
  45. </React.Fragment>
  46. )}
  47. </ArrayField>
  48. )}
  49. </Form>
  50. );
  51. };
  52. export default Demo;