From 2f1cc6c9386c6ee440965fa664cfda252ed24b1f Mon Sep 17 00:00:00 2001 From: katherinehhh Date: Thu, 18 May 2023 15:23:00 +0800 Subject: [PATCH] fix: dynamic appends not support template schema --- .../block-provider/DetailsBlockProvider.tsx | 3 +- .../src/block-provider/FormBlockProvider.tsx | 2 +- .../client/src/block-provider/hooks/index.ts | 43 ++++++++++++++----- 3 files changed, 35 insertions(+), 13 deletions(-) diff --git a/packages/core/client/src/block-provider/DetailsBlockProvider.tsx b/packages/core/client/src/block-provider/DetailsBlockProvider.tsx index 211ecc30b1..4772169eb1 100644 --- a/packages/core/client/src/block-provider/DetailsBlockProvider.tsx +++ b/packages/core/client/src/block-provider/DetailsBlockProvider.tsx @@ -40,8 +40,7 @@ const InternalDetailsBlockProvider = (props) => { export const DetailsBlockProvider = (props) => { const params = { ...props.params }; - const { collection } = props; - const { appends } = useAssociationNames(collection); + const { appends } = useAssociationNames(); if (!Object.keys(params).includes('appends')) { params['appends'] = appends; } diff --git a/packages/core/client/src/block-provider/FormBlockProvider.tsx b/packages/core/client/src/block-provider/FormBlockProvider.tsx index cbb8ad2a4e..7bc80e35f2 100644 --- a/packages/core/client/src/block-provider/FormBlockProvider.tsx +++ b/packages/core/client/src/block-provider/FormBlockProvider.tsx @@ -72,7 +72,7 @@ export const FormBlockProvider = (props) => { const currentCollection = useCollection(); const { designable } = useDesignable(); const isEmptyRecord = useIsEmptyRecord(); - const { appends, updateAssociationValues } = useAssociationNames(collection); + const { appends, updateAssociationValues } = useAssociationNames(); if (!Object.keys(params).includes('appends')) { params['appends'] = appends; } diff --git a/packages/core/client/src/block-provider/hooks/index.ts b/packages/core/client/src/block-provider/hooks/index.ts index 7284152c60..095f674715 100644 --- a/packages/core/client/src/block-provider/hooks/index.ts +++ b/packages/core/client/src/block-provider/hooks/index.ts @@ -1073,10 +1073,20 @@ export const useAssociationFilterBlockProps = () => { }; }; -export const useAssociationNames = (collection) => { +const getTemplateSchema = ({ uid }) => { + const conf = { + url: `/uiSchemas:getJsonSchema/${uid}`, + }; + const { data, loading } = useRequest(conf); + if (loading) { + // return; + } + return new Schema(data?.data || {}); +}; + +export const useAssociationNames = () => { const { getCollectionJoinField } = useCollectionManager(); const { getTemplateById } = useSchemaTemplateManager(); - const fieldSchema = useFieldSchema(); const associationValues = []; const formSchema = fieldSchema.reduceProperties((buf, schema) => { @@ -1086,6 +1096,13 @@ export const useAssociationNames = (collection) => { return buf; }, new Schema({})); + const templateSchema = formSchema.reduceProperties((buf, schema) => { + if (schema['x-component'] === 'BlockTemplate') { + return schema; + } + return buf; + }, null); + const getAssociationAppends = (schema, arr = []) => { const data = schema.reduceProperties((buf, s) => { const collectionfield = s['x-collection-field'] && getCollectionJoinField(s['x-collection-field']); @@ -1106,9 +1123,6 @@ export const useAssociationNames = (collection) => { if (s['x-component'] === 'Grid.Row') { const kk = buf?.concat?.(); return getNesterAppends(s, kk || []); - } else if (s['x-component'] === 'BlockTemplate') { - const templateSchema = getTemplateById(s['x-component-props']?.templateId); - return getAssociationAppends(templateSchema, buf); } else { return !s['x-component']?.includes('Action.') && s['x-component'] !== 'TableField' ? getAssociationAppends(s, buf) @@ -1138,7 +1152,7 @@ export const useAssociationNames = (collection) => { for (let i = 0; i < nestedList.length; i++) { flattenHelper(nestedList[i], nestedList[i][0]); } - return uniq(flattenedList.filter((obj) => !obj.startsWith('.'))); + return uniq(flattenedList.filter((obj) => !obj?.startsWith('.'))); } const getNesterAppends = (gridSchema, data) => { gridSchema.reduceProperties((buf, s) => { @@ -1147,8 +1161,17 @@ export const useAssociationNames = (collection) => { }, data); return data.filter((g) => g.length); }; - - const associations = getAssociationAppends(formSchema); - const appends = flattenNestedList(associations); - return { appends, updateAssociationValues: appends.filter((v) => associationValues.includes(v)) }; + if (templateSchema) { + const template = getTemplateById(templateSchema['x-component-props']?.templateId); + const schema = getTemplateSchema(template); + if (schema) { + const associations = getAssociationAppends(schema); + const appends = flattenNestedList(associations); + return { appends, updateAssociationValues: appends.filter((v) => associationValues.includes(v)) }; + } + } else { + const associations = getAssociationAppends(formSchema); + const appends = flattenNestedList(associations); + return { appends, updateAssociationValues: appends.filter((v) => associationValues.includes(v)) }; + } };