From 27245210004c7263da15421402b6e1051503d7b4 Mon Sep 17 00:00:00 2001 From: dream2023 <1098626505@qq.com> Date: Wed, 17 Jan 2024 17:34:59 +0800 Subject: [PATCH] fix: bug --- .../items/DataBlockInitializer.tsx | 15 ++++++++--- .../items/FilterCollapseBlockInitializer.tsx | 8 +++--- .../items/FilterFormBlockInitializer.tsx | 1 + .../items/FormBlockInitializer.tsx | 1 + .../client/src/schema-initializer/utils.ts | 27 ++++++++++++++++--- .../src/client/schema-initializer/utils.ts | 5 ++-- .../client/block/ChartBlockInitializer.tsx | 21 +++++---------- 7 files changed, 51 insertions(+), 27 deletions(-) diff --git a/packages/core/client/src/schema-initializer/items/DataBlockInitializer.tsx b/packages/core/client/src/schema-initializer/items/DataBlockInitializer.tsx index 078dfd86d2..70b6a9d6f3 100644 --- a/packages/core/client/src/schema-initializer/items/DataBlockInitializer.tsx +++ b/packages/core/client/src/schema-initializer/items/DataBlockInitializer.tsx @@ -3,6 +3,7 @@ import { Divider, Empty, Input, MenuProps, Spin } from 'antd'; import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react'; import { useTranslation } from 'react-i18next'; import { + CollectionV2, SchemaInitializerItem, SchemaInitializerMenu, useSchemaInitializer, @@ -349,7 +350,7 @@ export interface DataBlockInitializerProps { icon?: string | React.ReactNode; name: string; title: string; - items?: any[]; + filter?: (collection: CollectionV2) => boolean; componentType: string; } @@ -363,7 +364,7 @@ export const DataBlockInitializer = (props: DataBlockInitializerProps) => { icon = TableOutlined, name, title, - items, + filter, } = props; const { insert } = useSchemaInitializer(); const compile = useCompile(); @@ -377,14 +378,20 @@ export const DataBlockInitializer = (props: DataBlockInitializerProps) => { if (onCreateBlockSchema) { onCreateBlockSchema({ item }); } else if (createBlockSchema) { - insert(createBlockSchema({ collection: item.collectionName || item.name, isCusomeizeCreate })); + insert( + createBlockSchema({ + collection: item.collectionName || item.name, + namespace: item.namespace, + isCusomeizeCreate, + }), + ); } } }, [createBlockSchema, getTemplateSchemaByMode, insert, isCusomeizeCreate, onCreateBlockSchema, templateWrap], ); // const defaultItems = useCollectionDataSourceItemsV2(componentType); - const defaultItemsV2 = useCollectionDataSourceItemsV3(componentType); + const defaultItemsV2 = useCollectionDataSourceItemsV3(componentType, filter); // const menuChildren = useMemo(() => items || defaultItems, [items, defaultItems]); // const childItems = useSchemaInitializerMenuItems(menuChildren, name, onClick); const getMenuItems = useSchemaInitializerMenuItemsV2(onClick); diff --git a/packages/core/client/src/schema-initializer/items/FilterCollapseBlockInitializer.tsx b/packages/core/client/src/schema-initializer/items/FilterCollapseBlockInitializer.tsx index 58d0d3dbe7..9c23d3322a 100644 --- a/packages/core/client/src/schema-initializer/items/FilterCollapseBlockInitializer.tsx +++ b/packages/core/client/src/schema-initializer/items/FilterCollapseBlockInitializer.tsx @@ -1,5 +1,5 @@ import { TableOutlined } from '@ant-design/icons'; -import React from 'react'; +import React, { useCallback } from 'react'; import { useSchemaInitializer, useSchemaInitializerItem } from '../../application'; import { createCollapseBlockSchema } from '../utils'; @@ -8,7 +8,9 @@ import { DataBlockInitializer } from './DataBlockInitializer'; export const FilterCollapseBlockInitializer = () => { const itemConfig = useSchemaInitializerItem(); const { insert } = useSchemaInitializer(); - const items = itemConfig?.name === 'filterCollapseBlockInTableSelector' ? [] : undefined; + const filter = useCallback(() => { + return itemConfig?.name !== 'filterCollapseBlockInTableSelector'; + }, [itemConfig?.name]); return ( { }); insert(schema); }} - items={items} + filter={filter} /> ); }; diff --git a/packages/core/client/src/schema-initializer/items/FilterFormBlockInitializer.tsx b/packages/core/client/src/schema-initializer/items/FilterFormBlockInitializer.tsx index 4de4e8f26e..6ac0635e51 100644 --- a/packages/core/client/src/schema-initializer/items/FilterFormBlockInitializer.tsx +++ b/packages/core/client/src/schema-initializer/items/FilterFormBlockInitializer.tsx @@ -16,6 +16,7 @@ export const FilterFormBlockInitializer = (props) => { templateWrap={(templateSchema, { item }) => { const s = createFilterFormBlockSchema({ template: templateSchema, + namespace: item.namespace, collection: item.collectionName, }); if (item.template && item.mode === 'reference') { diff --git a/packages/core/client/src/schema-initializer/items/FormBlockInitializer.tsx b/packages/core/client/src/schema-initializer/items/FormBlockInitializer.tsx index 7533a33b3f..9704f90b7a 100644 --- a/packages/core/client/src/schema-initializer/items/FormBlockInitializer.tsx +++ b/packages/core/client/src/schema-initializer/items/FormBlockInitializer.tsx @@ -13,6 +13,7 @@ export const FormBlockInitializer = () => { icon={} componentType={'FormItem'} templateWrap={(templateSchema, { item }) => { + console.log('item', item); const s = createFormBlockSchema({ isCusomeizeCreate, namespace: item.namespace, diff --git a/packages/core/client/src/schema-initializer/utils.ts b/packages/core/client/src/schema-initializer/utils.ts index eb42415aca..cd42e852ee 100644 --- a/packages/core/client/src/schema-initializer/utils.ts +++ b/packages/core/client/src/schema-initializer/utils.ts @@ -5,6 +5,7 @@ import { useMemo } from 'react'; import { useTranslation } from 'react-i18next'; import { CollectionManagerV2, + CollectionV2, SchemaInitializerItemType, useCollectionManagerV2, useFormActiveFields, @@ -906,10 +907,14 @@ export const useCollectionDataSourceItemsV2 = (componentName) => { return res; }; -export const useCollectionDataSourceItemsV3 = (componentName) => { +export const useCollectionDataSourceItemsV3 = ( + componentName, + filter: (collection: CollectionV2) => boolean = () => true, +) => { const { t } = useTranslation(); const cm = useCollectionManagerV2(); - const allCollections = cm.getAllCollections((collection) => !collection.isLocal); + const notLocal = (collection) => !collection.isLocal; + const allCollections = cm.getAllCollections((collection) => notLocal(collection) && filter(collection)); const { getTemplatesByCollection } = useSchemaTemplateManager(); const res = useMemo(() => { return allCollections.map(({ nsName, nsTitle, collections }) => ({ @@ -936,6 +941,7 @@ export const createDetailsBlockSchema = (options) => { formItemInitializers = 'ReadPrettyFormItemInitializers', actionInitializers = 'DetailsActionInitializers', collection, + namespace, association, resource, template, @@ -948,6 +954,7 @@ export const createDetailsBlockSchema = (options) => { 'x-decorator': 'DetailsBlockProvider', 'x-decorator-props': { resource: resourceName, + namespace, collection, association, readPretty: true, @@ -1006,6 +1013,7 @@ export const createListBlockSchema = (options) => { actionInitializers = 'ListActionInitializers', itemActionInitializers = 'ListItemActionInitializers', collection, + namespace, association, resource, template, @@ -1019,6 +1027,7 @@ export const createListBlockSchema = (options) => { 'x-decorator-props': { resource: resourceName, collection, + namespace, association, readPretty: true, action: 'list', @@ -1095,6 +1104,7 @@ export const createGridCardBlockSchema = (options) => { association, resource, template, + namespace, ...others } = options; const resourceName = resource || association || collection; @@ -1106,6 +1116,7 @@ export const createGridCardBlockSchema = (options) => { resource: resourceName, collection, association, + namespace, readPretty: true, action: 'list', params: { @@ -1180,6 +1191,7 @@ export const createFormBlockSchema = (options) => { actionInitializers = 'FormActionInitializers', collection, resource, + namespace, association, action, actions = {}, @@ -1199,6 +1211,7 @@ export const createFormBlockSchema = (options) => { 'x-decorator-props': { ...others, action, + namespace, resource: resourceName, collection, association, @@ -1250,6 +1263,7 @@ export const createFilterFormBlockSchema = (options) => { collection, resource, association, + namespace, action, template, ...others @@ -1262,6 +1276,7 @@ export const createFilterFormBlockSchema = (options) => { ...others, action, resource: resourceName, + namespace, collection, association, }, @@ -1310,6 +1325,7 @@ export const createReadPrettyFormBlockSchema = (options) => { actionInitializers = 'ReadPrettyFormActionInitializers', collection, association, + namespace, resource, template, ...others @@ -1323,6 +1339,7 @@ export const createReadPrettyFormBlockSchema = (options) => { resource: resourceName, collection, association, + namespace, readPretty: true, action: 'get', useParams: '{{ useParamsFromRecord }}', @@ -1455,12 +1472,13 @@ export const createTableBlockSchema = (options) => { }; export const createCollapseBlockSchema = (options) => { - const { collection, blockType } = options; + const { collection, namespace, blockType } = options; const schema: ISchema = { type: 'void', 'x-decorator': 'AssociationFilter.Provider', 'x-decorator-props': { collection, + namespace, blockType, associationFilterStyle: { width: '100%', @@ -1485,7 +1503,7 @@ export const createCollapseBlockSchema = (options) => { }; export const createTableSelectorSchema = (options) => { - const { collection, resource, rowKey, ...others } = options; + const { collection, namespace, resource, rowKey, ...others } = options; const schema: ISchema = { type: 'void', 'x-acl-action': `${resource || collection}:list`, @@ -1493,6 +1511,7 @@ export const createTableSelectorSchema = (options) => { 'x-decorator-props': { collection, resource: resource || collection, + namespace, action: 'list', params: { pageSize: 20, diff --git a/packages/plugins/@nocobase/plugin-calendar/src/client/schema-initializer/utils.ts b/packages/plugins/@nocobase/plugin-calendar/src/client/schema-initializer/utils.ts index e54c1e8f65..4db3c4caa9 100644 --- a/packages/plugins/@nocobase/plugin-calendar/src/client/schema-initializer/utils.ts +++ b/packages/plugins/@nocobase/plugin-calendar/src/client/schema-initializer/utils.ts @@ -3,13 +3,14 @@ import { uid } from '@formily/shared'; import { generateNTemplate } from '../../locale'; export const createCalendarBlockSchema = (options) => { - const { collection, resource, fieldNames, ...others } = options; + const { collection, namespace, resource, fieldNames, ...others } = options; const schema: ISchema = { type: 'void', 'x-acl-action': `${resource || collection}:list`, 'x-decorator': 'CalendarBlockProvider', 'x-decorator-props': { collection: collection, + namespace, resource: resource || collection, action: 'list', fieldNames: { @@ -91,6 +92,6 @@ export const createCalendarBlockSchema = (options) => { }, }, }; - console.log(JSON.stringify(schema, null, 2)); + return schema; }; diff --git a/packages/plugins/@nocobase/plugin-data-visualization/src/client/block/ChartBlockInitializer.tsx b/packages/plugins/@nocobase/plugin-data-visualization/src/client/block/ChartBlockInitializer.tsx index fb9dc98e1a..098431d769 100644 --- a/packages/plugins/@nocobase/plugin-data-visualization/src/client/block/ChartBlockInitializer.tsx +++ b/packages/plugins/@nocobase/plugin-data-visualization/src/client/block/ChartBlockInitializer.tsx @@ -5,37 +5,30 @@ import { SchemaInitializer, SchemaInitializerItem, useACLRoleContext, - useCollectionDataSourceItemsV2, useSchemaInitializer, useSchemaInitializerItem, } from '@nocobase/client'; -import React, { useContext } from 'react'; +import React, { useCallback, useContext } from 'react'; import { ChartConfigContext } from '../configure'; import { FilterBlockInitializer } from '../filter'; import { lang } from '../locale'; const ChartInitializer = () => { const { setVisible, setCurrent } = useContext(ChartConfigContext); - const collections = useCollectionDataSourceItemsV2('Chart'); const { allowAll, parseAction } = useACLRoleContext(); const itemConfig = useSchemaInitializerItem(); - - const items = collections - .filter((item) => { - if (allowAll) { - return true; - } + const filter = useCallback( + (item) => { const params = parseAction(`${item.name}:list`); return params; - }) - .map((item) => ({ - ...item, - })); + }, + [allowAll, parseAction], + ); return ( } componentType={'Chart'} onCreateBlockSchema={async ({ item }) => {