index.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import React, { useState } from 'react';
  2. import FormContent from './FormContent';
  3. import ComponentLibrary from './ComponentLibrary';
  4. import ItemAttribute from './ItemAttribute';
  5. import { uuidv4 } from '@antv/xflow';
  6. import { Button } from 'antd';
  7. function AuditForm(props) {
  8. const [formList, setFormList] = useState([]);
  9. const [select, setSelect] = useState(-1);
  10. const [visible, setVisible] = useState(false);
  11. const handleAddItem = item => {
  12. const formItem = generateItem(item);
  13. setFormList([...formList, formItem]);
  14. setVisible(false);
  15. };
  16. const generateItem = item => {
  17. return {
  18. ...item,
  19. id: `${item.componentName}_${uuidv4()}`,
  20. icon: null,
  21. };
  22. };
  23. const onChangeAttribute = newItem => {
  24. let oldValue = formList[select].props;
  25. formList[select].props = { ...oldValue, ...newItem };
  26. setFormList([...formList]);
  27. };
  28. return (
  29. <div>
  30. <Button onClick={() => setVisible(true)}>增加控件</Button>
  31. <div
  32. style={{
  33. display: 'flex',
  34. justifyContent: 'space-between',
  35. alignItems: 'flex-start',
  36. width: 800,
  37. marginTop: 20,
  38. }}
  39. >
  40. <FormContent onSelect={setSelect} onChange={setFormList} list={formList}></FormContent>
  41. <ItemAttribute item={formList[select]} onChange={onChangeAttribute}></ItemAttribute>
  42. </div>
  43. <ComponentLibrary
  44. onOk={handleAddItem}
  45. visible={visible}
  46. onCancel={() => setVisible(false)}
  47. ></ComponentLibrary>
  48. </div>
  49. );
  50. }
  51. export default AuditForm;