Переглянути джерело

fix: 获取不到部分控件默认值问题及默认值不显示问题

ZhaoJun 1 рік тому
батько
коміт
c71c11dde9

+ 8 - 2
src/components/DDComponents/DDDateField/index.js

@@ -1,14 +1,20 @@
 import React from 'react';
 import { DatePicker } from 'antd';
+import moment from 'moment';
 
 function DDDateField(props) {
-  const { format = '', disabled, onChange } = props;
+  const { format = '', disabled, defaultValue, onChange } = props;
 
   const handleChange = date => {
-    onChange?.(date.format('YYYY-MM-DD HH:mm:ss'), props.id, props.label);
+    if (date) {
+      onChange?.(date.format('YYYY-MM-DD HH:mm:ss'), props.id, props.label);
+    }
+    onChange?.(date, props.id, props.label);
   };
+
   return (
     <DatePicker
+      defaultValue={defaultValue !== undefined ? moment(defaultValue[0]) : null}
       disabled={disabled}
       format={format.replace('yyyy', 'YYYY').replace('dd', 'DD')}
       onChange={handleChange}

+ 13 - 5
src/components/DDComponents/DIYTable/index.tsx

@@ -149,7 +149,13 @@ function DIYTable(props: IProps) {
           if (index === 0) {
             // 列配置
             row.forEach((col: any) => {
-              tableColumnDef.push({ title: col.name, dataIndex: col.type, className: '' });
+              if (col) {
+                tableColumnDef.push({
+                  title: col?.name || '',
+                  dataIndex: col?.type || '',
+                  className: '',
+                });
+              }
             });
           }
         }
@@ -181,10 +187,12 @@ function DIYTable(props: IProps) {
           const rowData: any = {};
           if (displayOnly) {
             row.forEach((col: any) => {
-              rowData.index = index + 1;
-              // eslint-disable-next-line prefer-destructuring
-              rowData[col.type] = col.value[0];
-              rowData.key = col.id + index;
+              if (col) {
+                rowData.index = index + 1;
+                // eslint-disable-next-line prefer-destructuring
+                rowData[col.type] = col.value[0];
+                rowData.key = col.id + index;
+              }
             });
             newTableData.push(rowData);
           }

+ 30 - 3
src/components/DDComponents/DepartmentField/index.js

@@ -1,11 +1,11 @@
 import { TreeSelect } from 'antd';
-import React, { useState, useEffect } from 'react';
+import React, { useState, useEffect, useMemo } from 'react';
 import { connect } from 'dva';
 // import { queryDDdepList } from '@/services/boom';
 // import { queryDDdepList } from '@/services/boom';
 
 function DepartmentField(props) {
-  const { value = [], onChange, depUserTree } = props;
+  const { value = [], onChange, defaultValue, depUserTree } = props;
   // const [treeData, setTreeData] = useState([]);
 
   // const genTreeNode = dep => {
@@ -47,6 +47,33 @@ function DepartmentField(props) {
     return result;
   };
 
+  const findDepID = (list, Name) => {
+    let result = '';
+    const dep = list.find(item => item.Name === Name);
+    if (dep) {
+      result = dep.ID;
+    } else {
+      for (let index = 0; index < list.length; index++) {
+        const element = list[index];
+        if (element?.children && element?.children.length) {
+          result = findDepID(element.children, Name);
+          if (result) {
+            break;
+          }
+        }
+      }
+    }
+    return result;
+  };
+
+  const defaultID = useMemo(() => {
+    if (defaultValue !== undefined) {
+      return findDepID(depUserTree, defaultValue[0]);
+    } else {
+      return null;
+    }
+  }, [defaultValue]);
+
   const onChangeValue = newValue => {
     const depName = findDepName(depUserTree, newValue);
     onChange(depName);
@@ -57,7 +84,7 @@ function DepartmentField(props) {
       showSearch
       // // multiple
       allowClear
-      defaultValue={value}
+      defaultValue={defaultID}
       dropdownStyle={{
         maxHeight: 400,
         overflow: 'auto',

+ 2 - 2
src/components/DDComponents/ManufacturerField/index.js

@@ -3,7 +3,7 @@ import { Select } from 'antd';
 import { querySupplierList } from '@/services/contract';
 
 function ManufacturerField(props) {
-  const { value, disabled = false, onChange } = props;
+  const { value, disabled = false, defaultValue, onChange } = props;
   const user = JSON.parse(localStorage.getItem('currentUser'));
 
   const [option, setOption] = useState([]);
@@ -41,7 +41,7 @@ function ManufacturerField(props) {
       loading={loading}
       style={{ width: '100%' }}
       disabled={disabled}
-      defaultValue={value ? Number(value) : undefined}
+      defaultValue={defaultValue}
       onChange={val => {
         const supplier = option.find(item => item.value === val).label;
         onChange(supplier);

+ 10 - 3
src/components/DDComponents/index.js

@@ -98,7 +98,14 @@ export default function DDComponents(props) {
       component = <DDMultiSelectField disabled={disabled} options={options} onChange={onChange} />;
       break;
     case 'DDDateField': // 日期控件
-      component = <DDDateField format={format} disabled={disabled} onChange={onChange} />;
+      component = (
+        <DDDateField
+          defaultValue={defaultValue}
+          format={format}
+          disabled={disabled}
+          onChange={onChange}
+        />
+      );
       break;
     case 'DDDateRangeField': // 时间区间控件
       component = <DDDateRangeField format={format} disabled={disabled} onChange={onChange} />;
@@ -129,7 +136,7 @@ export default function DDComponents(props) {
       component = <InnerContactField onChange={onChange} />;
       break;
     case 'DepartmentField': // 部门控件
-      component = <DepartmentField onChange={onChange} />;
+      component = <DepartmentField defaultValue={defaultValue} onChange={onChange} />;
       break;
     case 'CodeField': // 合同编号控件
       component = <CodeField depId={depId} onChange={onChange} />;
@@ -144,7 +151,7 @@ export default function DDComponents(props) {
       component = <FormulaField evalStr={evalStr} onChange={onChange} />;
       break;
     case 'ManufacturerField':
-      component = <ManufacturerField onChange={onChange} />;
+      component = <ManufacturerField defaultValue={defaultValue} onChange={onChange} />;
       break;
     case 'RelateField': // 关联审批单
       component = '关联审批单控件未渲染!';

+ 3 - 0
src/pages/Detail/AuditDetailed.js

@@ -5,6 +5,7 @@ import { FormulaType } from '@/components/AuditForm/FormulaModal';
 
 const AuditDetailed = props => {
   const { allValues = [], items, form, onValuesChange, onTableValChange } = props;
+
   const depId = useMemo(() => {
     const id = items.find(item => item.componentName === 'DepartmentField')?.props.id;
     const value = allValues.find(item => item.id === id)?.value;
@@ -19,6 +20,7 @@ const AuditDetailed = props => {
         linkedData = { ...linkedData, [item.id]: item.linked };
       }
     });
+
     const linkedList =
       items
         ?.map(item => {
@@ -26,6 +28,7 @@ const AuditDetailed = props => {
           return linked ? Object.values(linked).flat() : [];
         })
         .flat() || [];
+
     return { linkedData, linkedList };
   }, [items]);
 

+ 20 - 5
src/pages/Detail/CommitAuditModal.js

@@ -196,7 +196,7 @@ function CommitAuditModal(props) {
   const getFromData = idList => {
     const data = JSON.parse(JSON.stringify(formComponentValues));
     const result = [];
-    //获取流转节点的层级关系
+    // 获取流转节点的层级关系
     let len = 0;
     let list = [];
     idList.forEach(item => {
@@ -566,8 +566,8 @@ function CommitAuditModal(props) {
       }
       // 没找到就给默认值
       if (!componentValue.find(item => item.id === tempFormItem.props.id)) {
-        // placeholder
         if (tempFormItem.componentName === 'TextNote') {
+          // 记录TextNote
           componentValue.push({
             name: tempFormItem.props.label,
             id: tempFormItem.props.id,
@@ -592,14 +592,18 @@ function CommitAuditModal(props) {
           componentValue[index] = {
             name: formItem.props.label,
             id: currentFieldID,
-            value: [changedFields[currentFieldID]],
+            value: Array.isArray(changedFields[currentFieldID])
+              ? changedFields[currentFieldID]
+              : [changedFields[currentFieldID]],
           };
           break;
         } else if (index === componentValue.length - 1) {
           componentValue.push({
             name: formItem.props.label,
             id: currentFieldID,
-            value: [changedFields[currentFieldID]],
+            value: Array.isArray(changedFields[currentFieldID])
+              ? changedFields[currentFieldID]
+              : [changedFields[currentFieldID]],
           });
         }
       }
@@ -646,7 +650,7 @@ function CommitAuditModal(props) {
   // };
 
   const onFinish = async () => {
-    if(loading) return;
+    if (loading) return;
     const isOk = Object.values(approvalProcess).every(item => {
       return item.every(cur => {
         if (cur[0].type == 'role') return cur[0].nowValue;
@@ -700,6 +704,17 @@ function CommitAuditModal(props) {
     const flowPath = result.map(item => getFlowPath(item));
     setLoading(true);
     try {
+      // 检查Audit form是否为空
+      const tempFormData = await aduitDetailForm.validateFields().catch(err => {
+        message.error(err.errorFields[0].errors);
+        setLoading(false);
+        return false;
+      });
+      if (tempFormData === undefined || tempFormData === false) {
+        return;
+      }
+      // 检查之后调用一次,防止什么都没改导致获取不到值
+      onFormValueChange(tempFormData, tempFormData);
       const formList = getFromData(result);
       let params = {
         desc: fieldsValue.desc,