mirror of
https://github.com/nocobase/nocobase
synced 2024-11-15 08:36:44 +00:00
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:
parent
d9e7ba6e8d
commit
b11c22798a
@ -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 },
|
||||
{
|
||||
|
@ -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>
|
||||
)
|
||||
);
|
||||
};
|
||||
|
@ -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 };
|
@ -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}
|
||||
|
@ -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 }}>
|
||||
|
Loading…
Reference in New Issue
Block a user