AuditDetailed.js 3.6 KB

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