From 929dd9b22e3b73538adf686ece65a1d25723ba27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=91=E6=98=B6?= Date: Wed, 22 Jun 2022 10:37:18 +0800 Subject: [PATCH] fix: template with fields only (#517) --- .../core/client/src/schema-initializer/utils.ts | 14 +++++++++----- .../SchemaTemplateManagerProvider.tsx | 2 +- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/packages/core/client/src/schema-initializer/utils.ts b/packages/core/client/src/schema-initializer/utils.ts index 2c955c6949..ac35ddeb32 100644 --- a/packages/core/client/src/schema-initializer/utils.ts +++ b/packages/core/client/src/schema-initializer/utils.ts @@ -201,8 +201,12 @@ export const useRecordCollectionDataSourceItems = (componentName, item = null, c const { t } = useTranslation(); const collection = useCollection(); const { getTemplatesByCollection } = useSchemaTemplateManager(); - const templates = getTemplatesByCollection(collectionName || collection.name, resourceName || collectionName || collection.name).filter((template) => { + const templates = getTemplatesByCollection(collectionName || collection.name) + .filter((template) => { return componentName && template.componentName === componentName; + }) + .filter((template) => { + return ['FormItem', 'ReadPrettyFormItem'].includes(componentName) || (template.resourceName === resourceName); }); if (!templates.length) { return []; @@ -226,7 +230,7 @@ export const useRecordCollectionDataSourceItems = (componentName, item = null, c title: t('Duplicate template'), children: templates.map((template) => { const templateName = - template?.componentName === 'ReadPrettyFormItem' ? `${template?.name} ${t('(Fields only)')}` : template?.name; + ['FormItem', 'ReadPrettyFormItem'].includes(template?.componentName) ? `${template?.name} ${t('(Fields only)')}` : template?.name; return { type: 'item', mode: 'copy', @@ -244,7 +248,7 @@ export const useRecordCollectionDataSourceItems = (componentName, item = null, c title: t('Reference template'), children: templates.map((template) => { const templateName = - template?.componentName === 'ReadPrettyFormItem' ? `${template?.name} ${t('(Fields only)')}` : template?.name; + ['FormItem', 'ReadPrettyFormItem'].includes(template?.componentName) ? `${template?.name} ${t('(Fields only)')}` : template?.name; return { type: 'item', mode: 'reference', @@ -270,8 +274,8 @@ export const useCollectionDataSourceItems = (componentName) => { children: collections ?.filter((item) => !item.inherit) ?.map((item, index) => { - const templates = getTemplatesByCollection(item.name, item.name).filter((template) => { - return componentName && template.componentName === componentName; + const templates = getTemplatesByCollection(item.name).filter((template) => { + return componentName && template.componentName === componentName && (!template.resourceName || template.resourceName === item.name); }); if (!templates.length) { return { diff --git a/packages/core/client/src/schema-templates/SchemaTemplateManagerProvider.tsx b/packages/core/client/src/schema-templates/SchemaTemplateManagerProvider.tsx index 6c94ba2f99..b5dc09304e 100644 --- a/packages/core/client/src/schema-templates/SchemaTemplateManagerProvider.tsx +++ b/packages/core/client/src/schema-templates/SchemaTemplateManagerProvider.tsx @@ -120,7 +120,7 @@ export const useSchemaTemplateManager = () => { return templates?.find((template) => template.key === key); }, getTemplatesByCollection(collectionName: string, resourceName: string = null) { - const items = templates?.filter?.((template) => (!template.resourceName && template.collectionName === collectionName) || (template.resourceName && template.resourceName === resourceName)); + const items = templates?.filter?.((template) => (template.collectionName === collectionName)); return items || []; },