AuditDetailed.js 3.6 KB

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