diff --git a/packages/core/client-v2/src/collection-field-interface/CollectionFieldInterface.ts b/packages/core/client-v2/src/collection-field-interface/CollectionFieldInterface.ts index b1b1b0fc41d..e2569fe7df0 100644 --- a/packages/core/client-v2/src/collection-field-interface/CollectionFieldInterface.ts +++ b/packages/core/client-v2/src/collection-field-interface/CollectionFieldInterface.ts @@ -115,6 +115,7 @@ export abstract class CollectionFieldInterface { titleUsable?: boolean; usePathOptions?(field: any): any; hidden?: boolean; + creatable?: boolean; addComponentOption(componentOption: CollectionFieldInterfaceComponentOption) { if (!this.componentOptions) { diff --git a/packages/core/client-v2/src/collection-field-interface/CollectionFieldInterfaceManager.ts b/packages/core/client-v2/src/collection-field-interface/CollectionFieldInterfaceManager.ts index c208685dcc8..618aa2de557 100644 --- a/packages/core/client-v2/src/collection-field-interface/CollectionFieldInterfaceManager.ts +++ b/packages/core/client-v2/src/collection-field-interface/CollectionFieldInterfaceManager.ts @@ -31,6 +31,7 @@ interface FieldInterfaceLike { title?: ReactNode; group?: string; order?: number; + creatable?: boolean; isAssociation?: boolean; supportDataSourceType?: string[]; notSupportDataSourceType?: string[]; diff --git a/packages/plugins/@nocobase/plugin-data-source-manager/src/client-v2/pages/components/FieldForm.tsx b/packages/plugins/@nocobase/plugin-data-source-manager/src/client-v2/pages/components/FieldForm.tsx index a444ce10e31..c709b903797 100644 --- a/packages/plugins/@nocobase/plugin-data-source-manager/src/client-v2/pages/components/FieldForm.tsx +++ b/packages/plugins/@nocobase/plugin-data-source-manager/src/client-v2/pages/components/FieldForm.tsx @@ -101,23 +101,23 @@ function filterFieldInterfacesByTemplate( fieldInterfaces: FieldInterfaceOption[], collection: Record, ctx: any, - mode: 'create' | 'edit', databaseDialect?: string, ) { - if (mode !== 'create') { - return fieldInterfaces; - } const plugin = ctx.app.pm.get(PluginDataSourceManagerClientV2); const template = plugin?.getCollectionTemplate?.(collection.template || 'general'); - const templateFieldInterfaces = filterFieldInterfacesByCollectionTemplate( - fieldInterfaces, - template, - collection, - { - databaseDialect, - }, - ); - return filterCreateFieldInterfacesByCollectionTemplate(templateFieldInterfaces, template); + return filterFieldInterfacesByCollectionTemplate(fieldInterfaces, template, collection, { + databaseDialect, + }); +} + +function filterCreateFieldInterfacesByTemplate( + fieldInterfaces: FieldInterfaceOption[], + collection: Record, + ctx: any, +) { + const plugin = ctx.app.pm.get(PluginDataSourceManagerClientV2); + const template = plugin?.getCollectionTemplate?.(collection.template || 'general'); + return filterCreateFieldInterfacesByCollectionTemplate(fieldInterfaces, template); } function normalizeListResponse(response: any) { @@ -1712,19 +1712,22 @@ export function FieldForm(props: FieldFormProps) { const lastInitialValuesKeyRef = useRef(); const dataSource = ctx.dataSourceManager.getDataSource(props.dataSourceKey); const databaseDialect = getAppInfoDatabaseDialect(appInfo); - const fieldInterfaces = useMemo( + const fieldInterfaces = useMemo(() => { + const allFieldInterfaces = getFieldInterfaces(ctx, dataSource?.options?.type); + if (props.mode !== 'create') { + return allFieldInterfaces; + } + return filterFieldInterfacesByTemplate(allFieldInterfaces, props.collection, ctx, databaseDialect); + }, [databaseDialect, ctx, dataSource?.options?.type, props.collection, props.mode]); + const creatableFieldInterfaces = useMemo( () => - filterFieldInterfacesByTemplate( - getFieldInterfaces(ctx, dataSource?.options?.type), - props.collection, - ctx, - props.mode, - databaseDialect, - ), - [databaseDialect, ctx, dataSource?.options?.type, props.collection, props.mode], + props.mode === 'create' + ? filterCreateFieldInterfacesByTemplate(fieldInterfaces, props.collection, ctx) + : fieldInterfaces, + [ctx, fieldInterfaces, props.collection, props.mode], ); const [interfaceName, setInterfaceName] = useState( - props.field?.interface || props.interfaceName || fieldInterfaces[0]?.name, + props.field?.interface || props.interfaceName || creatableFieldInterfaces[0]?.name, ); const fieldInterface = useMemo( () => fieldInterfaces.find((item) => item.name === interfaceName), @@ -1910,10 +1913,10 @@ export function FieldForm(props: FieldFormProps) { setInterfaceName(props.interfaceName); return; } - if (!interfaceName && fieldInterfaces[0]?.name) { - setInterfaceName(fieldInterfaces[0].name); + if (!interfaceName && creatableFieldInterfaces[0]?.name) { + setInterfaceName(creatableFieldInterfaces[0].name); } - }, [fieldInterfaces, interfaceName, props.interfaceName]); + }, [creatableFieldInterfaces, interfaceName, props.interfaceName]); const handleInterfaceChange = useCallback( (nextInterface: string) => { @@ -2072,7 +2075,7 @@ export function FieldForm(props: FieldFormProps) { {!props.interfaceName && props.mode === 'create' ? (