index.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. import React, { useState, useEffect, useMemo } 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. import RelModal from './RelModal';
  8. import FormulaModal from './FormulaModal';
  9. function AuditForm(props) {
  10. const { value, onChange } = props;
  11. const [formList, setFormList] = useState([]);
  12. const [select, setSelect] = useState('');
  13. const [selectList, setSelectList] = useState([]);
  14. const [visible, setVisible] = useState(false);
  15. const [relVisible, setRelVisible] = useState(false); //关联选项弹窗
  16. const [forVisible, setForVisible] = useState(false); //计算公式弹窗
  17. const [addToTable, setAddToTable] = useState(false);
  18. const [currentTableID, setCurrentTableID] = useState('');
  19. console.log('-----------------', formList);
  20. const handleAddItem = (item) => {
  21. if (addToTable) {
  22. // 新增列处理
  23. const column = generateItem(item);
  24. // 找到对应的表格
  25. const tableItem = formList.find(
  26. (item) => item.props.id === currentTableID,
  27. );
  28. column.isColumn = true;
  29. // 把新增的列加入到表格中
  30. tableItem.columns.push(column);
  31. // 把新增列的表格放回
  32. const newFormList = [];
  33. for (const item of formList) {
  34. if (item.props.id !== currentTableID) {
  35. newFormList.push(item);
  36. } else {
  37. newFormList.push(tableItem);
  38. }
  39. }
  40. handleChangeList(newFormList);
  41. setCurrentTableID('');
  42. } else {
  43. const formItem = generateItem(item);
  44. handleChangeList([...formList, formItem]);
  45. }
  46. setAddToTable(false);
  47. setVisible(false);
  48. };
  49. const generateItem = (item) => {
  50. let newItem = {
  51. ...item,
  52. props: { ...item.props, id: `${item.componentName}_${uuidv4()}` },
  53. };
  54. // 如果是表格的话
  55. if (item.props.label === '表格') {
  56. newItem.columns = [];
  57. newItem.isTable = true;
  58. }
  59. delete newItem.icon;
  60. return newItem;
  61. };
  62. const onChangeAttribute = (newItemProps) => {
  63. let oldFormItem = findFormItem();
  64. let newFormList = [];
  65. // 是列
  66. if (oldFormItem.isColumn) {
  67. // 找到表格和col然后改掉属性
  68. for (const item of formList) {
  69. if (item.isTable) {
  70. for (const column of item.columns) {
  71. if (column.props.id === selectList[selectList.length - 1]) {
  72. column.props = { ...column.props, ...newItemProps };
  73. }
  74. }
  75. }
  76. newFormList.push(item);
  77. }
  78. } else {
  79. for (const item of formList) {
  80. if (item.props.id === select) {
  81. item.props = { ...item.props, ...newItemProps };
  82. }
  83. newFormList.push(item);
  84. }
  85. }
  86. handleChangeList(newFormList);
  87. };
  88. const handleChangeList = (list) => {
  89. setFormList(list);
  90. onChange?.(list);
  91. };
  92. // 表格列变化时(新增,调整顺序)
  93. const handleTableColumnChange = (id, newCole = []) => {
  94. if (newCole.length) {
  95. // 调整col顺序
  96. const tableItem = formList.find((item) => item.props.id === id);
  97. tableItem.columns = newCole;
  98. const newFormList = [];
  99. for (const item of formList) {
  100. if (item.props.id !== currentTableID) {
  101. newFormList.push(item);
  102. } else {
  103. newFormList.push(tableItem);
  104. }
  105. }
  106. handleChangeList(newFormList);
  107. } else {
  108. setCurrentTableID(id);
  109. setAddToTable(true);
  110. }
  111. };
  112. const handleFormContentSelect = (ids) => {
  113. setSelectList(ids);
  114. setSelect(ids[0]);
  115. };
  116. const findFormItem = () => {
  117. let formItem = formList.find((item) => item.props.id === selectList[0]);
  118. if (formItem?.isTable) {
  119. // 如果是表格的话,还要寻找内部的被点击的col
  120. if (selectList.length === 1) {
  121. return formItem || null;
  122. }
  123. return (
  124. formItem.columns.find(
  125. (item) => item.props.id === selectList[selectList.length - 1],
  126. ) || null
  127. );
  128. } else {
  129. return formList.find((item) => item.props.id === select) || null;
  130. }
  131. };
  132. useEffect(() => {
  133. if (value instanceof Array) {
  134. setFormList([...value]);
  135. onChange?.([...value]);
  136. }
  137. }, [value]);
  138. return (
  139. <div>
  140. <Button onClick={() => setVisible(true)}>增加控件</Button>
  141. <div
  142. style={{
  143. display: 'flex',
  144. justifyContent: 'space-between',
  145. alignItems: 'flex-start',
  146. width: 1200,
  147. marginTop: 20,
  148. }}
  149. >
  150. <FormContent
  151. onSelect={handleFormContentSelect}
  152. onChange={handleChangeList}
  153. onTableColumnChange={handleTableColumnChange}
  154. list={formList}
  155. ></FormContent>
  156. <ItemAttribute
  157. formList={formList}
  158. key={selectList[selectList.length - 1]}
  159. item={findFormItem()}
  160. onRelClick={() => setRelVisible(true)}
  161. onFormulaClick={() => setForVisible(true)}
  162. onChange={onChangeAttribute}
  163. ></ItemAttribute>
  164. </div>
  165. <ComponentLibrary
  166. addToTable={addToTable}
  167. onOk={handleAddItem}
  168. visible={visible || addToTable}
  169. onCancel={() => {
  170. setVisible(false);
  171. setAddToTable(false);
  172. }}
  173. />
  174. <RelModal
  175. item={formList.find((item) => item.props.id === select)}
  176. formList={formList}
  177. visible={relVisible}
  178. onCancel={() => setRelVisible(false)}
  179. onChange={(value) => {
  180. setRelVisible(false);
  181. onChangeAttribute(value);
  182. }}
  183. />
  184. <FormulaModal
  185. item={formList.find((item) => item.props.id === select)}
  186. numFiledList={formList.filter(
  187. (item) => item.componentName == 'NumberField',
  188. )}
  189. visible={forVisible}
  190. onCancel={() => setForVisible(false)}
  191. onChange={(value) => {
  192. setForVisible(false);
  193. onChangeAttribute(value);
  194. }}
  195. />
  196. </div>
  197. );
  198. }
  199. export default AuditForm;