diff --git a/packages/core/client/src/block-provider/FormBlockProvider.tsx b/packages/core/client/src/block-provider/FormBlockProvider.tsx index 3f98241c3..9fe77d221 100644 --- a/packages/core/client/src/block-provider/FormBlockProvider.tsx +++ b/packages/core/client/src/block-provider/FormBlockProvider.tsx @@ -59,7 +59,7 @@ export const FormBlockProvider = (props) => { !designable && __tableName && !inheritCollections.includes(props.collection) && __tableName !== props.collection; return ( !flag && ( - + ) diff --git a/packages/core/client/src/block-provider/hooks/index.ts b/packages/core/client/src/block-provider/hooks/index.ts index 55d496797..f9fd0e53d 100644 --- a/packages/core/client/src/block-provider/hooks/index.ts +++ b/packages/core/client/src/block-provider/hooks/index.ts @@ -3,6 +3,7 @@ import { message, Modal } from 'antd'; import parse from 'json-templates'; import { cloneDeep } from 'lodash'; import get from 'lodash/get'; +import omit from 'lodash/omit'; import { useTranslation } from 'react-i18next'; import { useHistory } from 'react-router-dom'; import { useReactToPrint } from 'react-to-print'; @@ -69,6 +70,16 @@ const filterValue = (value) => { }; function getFormValues(filterByTk, field, form, fieldNames, getField, resource) { + if (filterByTk) { + const actionFields = field?.data?.activeFields as Set; + if (actionFields) { + const keys = Object.keys(form.values).filter((key) => { + const f = getField(key); + return !actionFields.has(key) && ['hasOne', 'hasMany', 'belongsTo', 'belongsToMany'].includes(f?.type); + }); + return omit({ ...form.values }, keys); + } + } return form.values; let values = {}; for (const key in form.values) { @@ -640,4 +651,4 @@ export const useDetailsPaginationProps = () => { textAlign: 'center', }, }; -}; \ No newline at end of file +}; diff --git a/packages/core/client/src/schema-component/antd/form-item/FormItem.tsx b/packages/core/client/src/schema-component/antd/form-item/FormItem.tsx index 81d7a9385..33d4e71ff 100644 --- a/packages/core/client/src/schema-component/antd/form-item/FormItem.tsx +++ b/packages/core/client/src/schema-component/antd/form-item/FormItem.tsx @@ -4,10 +4,10 @@ import { Field } from '@formily/core'; import { ISchema, useField, useFieldSchema } from '@formily/react'; import { uid } from '@formily/shared'; import _ from 'lodash'; -import React from 'react'; +import React, { useContext, useEffect } from 'react'; import { useTranslation } from 'react-i18next'; import { ACLCollectionFieldProvider } from '../../../acl/ACLProvider'; -import { useFilterByTk, useFormBlockContext } from '../../../block-provider'; +import { BlockRequestContext, useFilterByTk, useFormBlockContext } from '../../../block-provider'; import { useCollection, useCollectionManager } from '../../../collection-manager'; import { GeneralSchemaDesigner, SchemaSettings } from '../../../schema-settings'; import { useCompile, useDesignable, useFieldComponentOptions } from '../../hooks'; @@ -26,6 +26,15 @@ const divWrap = (schema: ISchema) => { export const FormItem: any = (props) => { const field = useField(); + const ctx = useContext(BlockRequestContext); + const schema = useFieldSchema(); + useEffect(() => { + if (ctx?.block === 'form') { + ctx.field.data = ctx.field.data || {}; + ctx.field.data.activeFields = ctx.field.data.activeFields || new Set(); + ctx.field.data.activeFields.add(schema.name); + } + }, []); return ( diff --git a/packages/core/client/src/schema-initializer/utils.ts b/packages/core/client/src/schema-initializer/utils.ts index 4c6ca59d1..8694e06a3 100644 --- a/packages/core/client/src/schema-initializer/utils.ts +++ b/packages/core/client/src/schema-initializer/utils.ts @@ -1,8 +1,8 @@ import { ISchema, Schema, useFieldSchema, useForm } from '@formily/react'; import { uid } from '@formily/shared'; -import React, { useState } from 'react'; +import React, { useContext, useState } from 'react'; import { useTranslation } from 'react-i18next'; -import { SchemaInitializerItemOptions } from '../'; +import { BlockRequestContext, SchemaInitializerItemOptions } from '../'; import { useCollection, useCollectionManager } from '../collection-manager'; import { useActionContext, useDesignable } from '../schema-component'; import { useSchemaTemplateManager } from '../schema-templates'; @@ -427,10 +427,18 @@ export const useCurrentSchema = (action: string, key: string, find = findSchema, } const { remove } = useDesignable(); const schema = find(fieldSchema, key, action); + const ctx = useContext(BlockRequestContext); + const exists = !!schema; + return { schema, exists: !!schema, remove() { + if (ctx.field) { + ctx.field.data = ctx.field.data || {}; + ctx.field.data.activeFields = ctx.field.data.activeFields || new Set(); + ctx.field.data.activeFields.delete(schema.name); + } schema && rm(schema, remove); }, };