From 2b0e1fa5737bf840675dc531688ad179fd22ed8e Mon Sep 17 00:00:00 2001 From: wjh Date: Thu, 9 May 2024 23:02:57 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E6=94=B9=E5=BD=95=E5=8D=95?= =?UTF-8?q?=E7=9A=84=E7=BB=B4=E4=BF=AE=E8=B5=94=E5=81=BF=E7=BA=A7=E8=81=94?= =?UTF-8?q?=E7=82=B9=E5=87=BB=E8=83=BD=E6=98=BE=E7=A4=BA=E5=90=8D=E7=A7=B0?= =?UTF-8?q?=EF=BC=8C=E4=BF=AE=E6=94=B9=E7=BA=A7=E8=81=94=E7=BB=84=E4=BB=B7?= =?UTF-8?q?=E6=9F=A5=E7=9C=8B=E6=98=BE=E7=A4=BA=E6=A0=87=E9=A2=98=20(#958)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Reviewed-on: https://git.daoyoucloud.com/daoyoucloud/tachybase/pulls/958 Reviewed-by: sealday Co-authored-by: wjh Co-committed-by: wjh --- .../AssociationFieldProvider.tsx | 2 +- .../association-field/InternalCascader.tsx | 64 +++++++++++++++---- .../antd/association-field/ReadPretty.tsx | 4 +- .../custom-components/RecordDetails.tsx | 2 +- 4 files changed, 55 insertions(+), 17 deletions(-) diff --git a/packages/core/client/src/schema-component/antd/association-field/AssociationFieldProvider.tsx b/packages/core/client/src/schema-component/antd/association-field/AssociationFieldProvider.tsx index a2c6aa77e..67e888071 100644 --- a/packages/core/client/src/schema-component/antd/association-field/AssociationFieldProvider.tsx +++ b/packages/core/client/src/schema-component/antd/association-field/AssociationFieldProvider.tsx @@ -32,7 +32,7 @@ export const AssociationFieldProvider = observer( const currentMode = useMemo( () => fieldSchema['x-component-props']?.mode || - (isTreeCollection ? 'Cascader' : isFileCollection ? 'FileManager' : 'Select'), + (isFileCollection ? 'FileManager' : isTreeCollection ? 'Cascader' : 'Select'), // eslint-disable-next-line react-hooks/exhaustive-deps [fieldSchema['x-component-props']?.mode], ); diff --git a/packages/core/client/src/schema-component/antd/association-field/InternalCascader.tsx b/packages/core/client/src/schema-component/antd/association-field/InternalCascader.tsx index 10834f2c5..90739c205 100644 --- a/packages/core/client/src/schema-component/antd/association-field/InternalCascader.tsx +++ b/packages/core/client/src/schema-component/antd/association-field/InternalCascader.tsx @@ -15,7 +15,7 @@ import { Input, Space } from 'antd'; import React, { useEffect, useMemo, useState } from 'react'; import { useTranslation } from 'react-i18next'; import { CustomCascader, SchemaComponent } from '../..'; -import { css, useAPIClient } from '../../..'; +import { css, useAPIClient, useCollection, useRequest } from '../../..'; import { mergeFilter } from '../../../filter-provider/utils'; import useServiceOptions, { useAssociationFieldContext } from './hooks'; @@ -43,22 +43,47 @@ const Cascade = connect((props) => { const field: any = useField(); const [selectedOptions, setSelectedOptions] = useState([]); const [chang, setChang] = useState(false); + const [dataList, setDataList] = useState({}); useEffect(() => { const propsValue = props.value || fieldSchema['x-component-props'].value; - if (!chang) { - const values = Array.isArray(propsValue) - ? extractLastNonNullValueObjects( - propsValue?.filter((v) => v.value), - true, - ) - : transformNestedData(propsValue); - const defaultData = values?.map?.((v) => { - return v.id; - }); - setSelectedOptions(defaultData); - onDropdownVisibleChange(true); + if (!chang && propsValue) { + if (!propsValue.parent && !Object.keys(dataList).length) { + resource + .list({ + pageSize: 9999, + filter: { id: { $eq: propsValue.id } }, + sort, + tree: true, + }) + .then((res) => { + setDataList(res.data?.data[0]); + }) + .catch(() => {}); + } else { + const values = []; + if (!propsValue.parent && Object.keys(dataList).length) { + const defValue = transformChildrenData(dataList, [], propsValue.id); + values.push(...defValue); + } else { + const defValue = Array.isArray(propsValue) + ? extractLastNonNullValueObjects( + propsValue?.filter((v) => v.value), + true, + ) + : transformNestedData(propsValue); + values.push(...defValue); + } + const defaultData = values?.map?.((v) => { + return v.id; + }); + onDropdownVisibleChange(true); + setSelectedOptions(defaultData); + } } - }, [fieldSchema['x-component-props'].value, fieldSchema['x-component-props']?.changOnSelect]); + }, [fieldSchema['x-component-props'].value, fieldSchema['x-component-props']?.changOnSelect, dataList]); + useEffect(() => { + onDropdownVisibleChange(true); + }, [fieldFilter]); const handleGetOptions = async () => { const response = await resource.list({ pageSize: 9999, @@ -328,3 +353,14 @@ export function transformNestedData(inputData) { } return resultArray; } + +export const transformChildrenData = (inputData, result, itemId) => { + const { children, ...other } = inputData; + result.push(other); + if (children && inputData?.id !== itemId && children.length) { + const { children } = inputData; + return transformChildrenData(children[0], result, itemId); + } else { + return result; + } +}; diff --git a/packages/core/client/src/schema-component/antd/association-field/ReadPretty.tsx b/packages/core/client/src/schema-component/antd/association-field/ReadPretty.tsx index a18296c77..ee097aa16 100644 --- a/packages/core/client/src/schema-component/antd/association-field/ReadPretty.tsx +++ b/packages/core/client/src/schema-component/antd/association-field/ReadPretty.tsx @@ -13,7 +13,9 @@ const ReadPrettyAssociationField = observer( const { currentMode } = useAssociationFieldContext(); return ( <> - {['Select', 'Picker', 'CascadeSelect'].includes(currentMode) && } + {['Select', 'Picker', 'CascadeSelect', 'Cascader'].includes(currentMode) && ( + + )} {currentMode === 'Tag' && } {currentMode === 'Nester' && } {currentMode === 'SubTable' && } diff --git a/packages/plugins/@hera/plugin-rental/src/client/custom-components/RecordDetails.tsx b/packages/plugins/@hera/plugin-rental/src/client/custom-components/RecordDetails.tsx index f66f180f5..4c8568bc2 100644 --- a/packages/plugins/@hera/plugin-rental/src/client/custom-components/RecordDetails.tsx +++ b/packages/plugins/@hera/plugin-rental/src/client/custom-components/RecordDetails.tsx @@ -43,7 +43,7 @@ export const RecordDetails: CustomFunctionComponent = () => { const feeItems = {}; // 根据关联产品名称来合并赔偿 - if (reqRecordItemFeeItems.data) { + if (reqRecordItemFeeItems.data && products) { reqRecordItemFeeItems.data.data?.forEach((contract, index) => { if (!contract.fees.length) return; const movement = contract.movement === '-1' ? '出库合同' : '入库合同';