This commit is contained in:
dream2023 2024-01-17 17:34:59 +08:00
parent 855db5a830
commit 2724521000
7 changed files with 51 additions and 27 deletions

View File

@ -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);

View File

@ -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 (
<DataBlockInitializer
@ -24,7 +26,7 @@ export const FilterCollapseBlockInitializer = () => {
});
insert(schema);
}}
items={items}
filter={filter}
/>
);
};

View File

@ -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') {

View File

@ -13,6 +13,7 @@ export const FormBlockInitializer = () => {
icon={<FormOutlined />}
componentType={'FormItem'}
templateWrap={(templateSchema, { item }) => {
console.log('item', item);
const s = createFormBlockSchema({
isCusomeizeCreate,
namespace: item.namespace,

View File

@ -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,

View File

@ -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;
};

View File

@ -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 (
<DataBlockInitializer
{...itemConfig}
items={items}
filter={filter}
icon={<BarChartOutlined />}
componentType={'Chart'}
onCreateBlockSchema={async ({ item }) => {