From b11c22798a24ce6f80e6734c4f0f2228d6f45c30 Mon Sep 17 00:00:00 2001 From: katherinehhh Date: Fri, 10 Nov 2023 15:54:55 +0800 Subject: [PATCH] fix: association field in reference block failed to append (#2998) * fix: reference block association data failed to append * feat: default value should not set in edit form * chore: block template provider --- .../src/block-provider/BlockProvider.tsx | 10 +++++---- .../src/block-provider/FormBlockProvider.tsx | 13 ++++++----- .../block-provider/TemplateBlockProvider.tsx | 22 +++++++++++++++++++ .../src/schema-component/antd/grid/Grid.tsx | 13 ++++++++++- .../src/schema-templates/BlockTemplate.tsx | 4 ++++ 5 files changed, 52 insertions(+), 10 deletions(-) create mode 100644 packages/core/client/src/block-provider/TemplateBlockProvider.tsx diff --git a/packages/core/client/src/block-provider/BlockProvider.tsx b/packages/core/client/src/block-provider/BlockProvider.tsx index 95a8b2193a..416717cec2 100644 --- a/packages/core/client/src/block-provider/BlockProvider.tsx +++ b/packages/core/client/src/block-provider/BlockProvider.tsx @@ -21,6 +21,7 @@ import { FilterBlockRecord } from '../filter-provider/FilterProvider'; import { useRecordIndex } from '../record-provider'; import { SharedFilterProvider } from './SharedFilterProvider'; import { useAssociationNames } from './hooks'; +import { useTemplateBlockContext } from './TemplateBlockProvider'; export const BlockResourceContext = createContext(null); export const BlockAssociationContext = createContext(null); @@ -115,21 +116,23 @@ const useResourceAction = (props, opts = {}) => { const api = useAPIClient(); const fieldSchema = useFieldSchema(); const { snapshot } = useActionContext(); + const { templateFinshed } = useTemplateBlockContext(); const record = useRecord(); - + const isTemplate = fieldSchema['x-template-key']; if (!Reflect.has(params, 'appends')) { const appends = fields?.filter((field) => field.target).map((field) => field.name); if (appends?.length) { params['appends'] = appends; } } + console.log(templateFinshed); const result = useRequest( snapshot ? async () => ({ data: record[tableFieldName] ?? [], }) : (opts) => { - if (!action) { + if (!action || (isTemplate && !templateFinshed)) { return Promise.resolve({}); } const actionParams = { ...params, ...opts }; @@ -147,7 +150,7 @@ const useResourceAction = (props, opts = {}) => { } }, defaultParams: [params], - refreshDeps: [runWhenParamsChanged ? null : JSON.stringify(params.appends)], + refreshDeps: [runWhenParamsChanged ? null : JSON.stringify(params.appends), templateFinshed], }, ); // automatic run service when params has changed @@ -180,7 +183,6 @@ export const BlockRequestProvider = (props) => { const field = useField(); const resource = useBlockResource(); const [allowedActions, setAllowedActions] = useState({}); - const service = useResourceAction( { ...props, resource }, { diff --git a/packages/core/client/src/block-provider/FormBlockProvider.tsx b/packages/core/client/src/block-provider/FormBlockProvider.tsx index cc14db3a20..3e21cbc2f9 100644 --- a/packages/core/client/src/block-provider/FormBlockProvider.tsx +++ b/packages/core/client/src/block-provider/FormBlockProvider.tsx @@ -9,6 +9,7 @@ import { useActionContext, useDesignable } from '../schema-component'; import { Templates as DataTemplateSelect } from '../schema-component/antd/form-v2/Templates'; import { BlockProvider, useBlockRequestContext } from './BlockProvider'; import { FormActiveFieldsProvider } from './hooks'; +import { TemplateBlockProvider } from './TemplateBlockProvider'; export const FormBlockContext = createContext({}); @@ -103,11 +104,13 @@ export const FormBlockProvider = (props) => { (currentCollection.name === (collection?.name || collection) && !isDetailBlock) || !currentCollection.name; return ( (detailFlag || createFlag || isCusomeizeCreate) && ( - - - - - + + + + + + + ) ); }; diff --git a/packages/core/client/src/block-provider/TemplateBlockProvider.tsx b/packages/core/client/src/block-provider/TemplateBlockProvider.tsx new file mode 100644 index 0000000000..429ba603b2 --- /dev/null +++ b/packages/core/client/src/block-provider/TemplateBlockProvider.tsx @@ -0,0 +1,22 @@ +import React, { createContext, useContext, useState } from 'react'; + +const TemplateBlockContext = createContext<{ + // 模板是否已经请求结束 + templateFinshed?: boolean; + onTemplateSuccess?: Function; +}>({}); + +export const useTemplateBlockContext = () => { + return useContext(TemplateBlockContext); +}; + +const TemplateBlockProvider = (props) => { + const [templateFinshed, setTemplateFinshed] = useState(false); + return ( + setTemplateFinshed(true) }}> + {props.children} + + ); +}; + +export { TemplateBlockProvider }; diff --git a/packages/core/client/src/schema-component/antd/grid/Grid.tsx b/packages/core/client/src/schema-component/antd/grid/Grid.tsx index fd05c3e788..5e89ce13cc 100644 --- a/packages/core/client/src/schema-component/antd/grid/Grid.tsx +++ b/packages/core/client/src/schema-component/antd/grid/Grid.tsx @@ -8,6 +8,7 @@ import { useDesignable, useFormBlockContext, useSchemaInitializer } from '../../ import { DndContext } from '../../common/dnd-context'; import { useToken } from '../__builtins__'; import useStyles from './Grid.style'; +import { useFormBlockType } from '../../../block-provider'; const GridRowContext = createContext({}); const GridColContext = createContext({}); @@ -368,6 +369,7 @@ Grid.Row = observer( const addr = field.address.toString(); const cols = useColProperties(); const { showDivider } = useGridContext(); + const { type } = useFormBlockType(); return ( @@ -392,7 +394,16 @@ Grid.Row = observer( {cols.map((schema, index) => { return ( - + { + if (type === 'update') { + schema.default = null; + } + return schema; + }} + /> {showDivider && ( ({}); @@ -17,9 +18,12 @@ export const BlockTemplate = observer( const fieldSchema = useFieldSchema(); const { dn } = useDesignable(); const template = useMemo(() => getTemplateById(templateId), [templateId]); + const { onTemplateSuccess } = useTemplateBlockContext(); + const onSuccess = (data) => { fieldSchema['x-linkage-rules'] = data?.data?.['x-linkage-rules'] || []; fieldSchema.setProperties(data?.data?.properties); + onTemplateSuccess(); }; return template ? (