AuditDetailed.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import DDComponents from '@/components/DDComponents';
  2. import React, { useMemo, useState } from 'react';
  3. import { Button, Form } from 'antd';
  4. import { FormulaType } from '@/components/AuditForm/FormulaModal';
  5. const AuditDetailed = (props) => {
  6. // const [form] = Form.useForm();
  7. const {
  8. allValues = [],
  9. items,
  10. form,
  11. onValuesChange,
  12. onTableValChange,
  13. } = props;
  14. const depId = useMemo(() => {
  15. const id = items.find((item) => item.componentName == 'DepartmentField')
  16. ?.props.id;
  17. const value = allValues.find((item) => item.id == id)?.value;
  18. if (value) return value[0];
  19. }, [allValues, items]);
  20. console.log(items, allValues);
  21. const data = useMemo(() => {
  22. let linkedData = {};
  23. items.forEach((d) => {
  24. const item = d.props;
  25. if (item.linked) {
  26. linkedData = { ...linkedData, [item.id]: item.linked };
  27. }
  28. });
  29. const linkedList =
  30. items
  31. ?.map((item) => {
  32. const linked = item.props.linked;
  33. return linked ? Object.values(linked).flat() : [];
  34. })
  35. .flat() || [];
  36. return { linkedData, linkedList };
  37. }, [items]);
  38. const onFinish = (values) => {
  39. console.log(values);
  40. };
  41. const GetComponent = (item) => {
  42. const { id, label, bizAlias, required, notUpper } = item.props;
  43. //判断是否关联项
  44. if (data.linkedList.findIndex((curId) => curId == id) !== -1) {
  45. let control = null; //当前空间是否显示的条件 当id为control.id的组件选择的选项值为control.value 时显示
  46. Object.keys(data.linkedData).forEach((ctlIs) => {
  47. const linked = data.linkedData[ctlIs];
  48. Object.keys(linked).forEach((value) => {
  49. const ids = linked[value];
  50. if (ids.findIndex((curId) => curId == id) !== -1) {
  51. control = { id: ctlIs, value };
  52. }
  53. });
  54. });
  55. let currentValue = form.getFieldValue(control?.id);
  56. if (currentValue != control?.value) return null;
  57. }
  58. let formLabel;
  59. if (bizAlias !== undefined) {
  60. formLabel = bizAlias;
  61. } else {
  62. try {
  63. // 判断label是否为JSON数组
  64. formLabel = JSON.parse(label).join(',');
  65. } catch (error) {
  66. formLabel = label;
  67. }
  68. }
  69. const renderComponents = () => {
  70. let content = '';
  71. if (item.componentName === 'CodeField') {
  72. content = <DDComponents item={item} depId={depId} />;
  73. } else if (item.componentName === 'FormulaField') {
  74. const strList = item.props?.formula?.map((formu) => {
  75. if (formu.type == FormulaType.Filed) {
  76. const numItem = allValues?.find((item) => item.id == formu.id);
  77. return numItem?.value[0] || 0;
  78. } else {
  79. return formu.label;
  80. }
  81. });
  82. console.log('-------44444-------------', item, strList);
  83. const evalStr = strList?.join('');
  84. content = <DDComponents item={item} evalStr={evalStr} />;
  85. } else {
  86. content = <DDComponents item={item} />;
  87. }
  88. return content;
  89. };
  90. // const component = DDComponents({ item });
  91. // if (!component) return null;
  92. return (
  93. <>
  94. {item?.isTable === undefined ? (
  95. <Form.Item
  96. key={id}
  97. name={id}
  98. label={formLabel}
  99. rules={[{ required: required }]}
  100. >
  101. {renderComponents()}
  102. </Form.Item>
  103. ) : (
  104. <DDComponents item={item} onChange={onTableValChange} />
  105. )}
  106. </>
  107. );
  108. };
  109. return (
  110. <Form
  111. form={form}
  112. style={{ minHeight: '80vh', overflowY: 'auto', paddingRight: 20 }}
  113. layout="vertical"
  114. autoComplete="off"
  115. onValuesChange={onValuesChange}
  116. onFinish={onFinish}
  117. >
  118. {items.map((item) => GetComponent(item))}
  119. </Form>
  120. );
  121. };
  122. export default AuditDetailed;