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
This commit is contained in:
katherinehhh 2023-11-10 15:54:55 +08:00 committed by GitHub
parent d9e7ba6e8d
commit b11c22798a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 52 additions and 10 deletions

View File

@ -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<Field>();
const resource = useBlockResource();
const [allowedActions, setAllowedActions] = useState({});
const service = useResourceAction(
{ ...props, resource },
{

View File

@ -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<any>({});
@ -103,11 +104,13 @@ export const FormBlockProvider = (props) => {
(currentCollection.name === (collection?.name || collection) && !isDetailBlock) || !currentCollection.name;
return (
(detailFlag || createFlag || isCusomeizeCreate) && (
<BlockProvider name={props.name || 'form'} {...props} block={'form'}>
<FormActiveFieldsProvider name="form">
<InternalFormBlockProvider {...props} />
</FormActiveFieldsProvider>
</BlockProvider>
<TemplateBlockProvider>
<BlockProvider name={props.name || 'form'} {...props} block={'form'}>
<FormActiveFieldsProvider name="form">
<InternalFormBlockProvider {...props} />
</FormActiveFieldsProvider>
</BlockProvider>
</TemplateBlockProvider>
)
);
};

View File

@ -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 (
<TemplateBlockContext.Provider value={{ templateFinshed, onTemplateSuccess: () => setTemplateFinshed(true) }}>
{props.children}
</TemplateBlockContext.Provider>
);
};
export { TemplateBlockProvider };

View File

@ -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<any>({});
const GridColContext = createContext<any>({});
@ -368,6 +369,7 @@ Grid.Row = observer(
const addr = field.address.toString();
const cols = useColProperties();
const { showDivider } = useGridContext();
const { type } = useFormBlockType();
return (
<GridRowContext.Provider value={{ schema: fieldSchema, cols }}>
@ -392,7 +394,16 @@ Grid.Row = observer(
{cols.map((schema, index) => {
return (
<React.Fragment key={index}>
<RecursionField name={schema.name} schema={schema} />
<RecursionField
name={schema.name}
schema={schema}
mapProperties={(schema) => {
if (type === 'update') {
schema.default = null;
}
return schema;
}}
/>
{showDivider && (
<ColDivider
cols={cols}

View File

@ -2,6 +2,7 @@ import { observer, useField, useFieldSchema } from '@formily/react';
import React, { createContext, useContext, useMemo } from 'react';
import { RemoteSchemaComponent, useDesignable } from '..';
import { useSchemaTemplateManager } from './SchemaTemplateManagerProvider';
import { useTemplateBlockContext } from '../block-provider/TemplateBlockProvider';
const BlockTemplateContext = createContext<any>({});
@ -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 ? (
<BlockTemplateContext.Provider value={{ dn, field, fieldSchema, template }}>