fix: updateAssociationValues

This commit is contained in:
katherinehhh 2023-05-08 15:28:23 +08:00
parent f0740dca5b
commit 1dd388c7b7

View File

@ -2,18 +2,19 @@ import { createForm } from '@formily/core';
import { RecursionField, Schema, useField, useFieldSchema } from '@formily/react'; import { RecursionField, Schema, useField, useFieldSchema } from '@formily/react';
import { Spin } from 'antd'; import { Spin } from 'antd';
import { isEmpty } from 'lodash'; import { isEmpty } from 'lodash';
import React, { createContext, useContext, useEffect, useMemo, useRef } from 'react'; import React, { createContext, useCallback, useContext, useEffect, useMemo, useRef } from 'react';
import { useCollection } from '../collection-manager'; import { useCollection } from '../collection-manager';
import { RecordProvider, useRecord } from '../record-provider'; import { RecordProvider, useRecord } from '../record-provider';
import { useCollectionManager } from '../collection-manager'; import { useCollectionManager } from '../collection-manager';
import { useActionContext, useDesignable } from '../schema-component'; import { useActionContext, useDesignable } from '../schema-component';
import { Templates as DataTemplateSelect } from '../schema-component/antd/form-v2/Templates'; import { Templates as DataTemplateSelect } from '../schema-component/antd/form-v2/Templates';
import { BlockProvider, useBlockRequestContext } from './BlockProvider'; import { BlockProvider, useBlockRequestContext } from './BlockProvider';
import app from '@nocobase/app-server';
export const FormBlockContext = createContext<any>({}); export const FormBlockContext = createContext<any>({});
const InternalFormBlockProvider = (props) => { const InternalFormBlockProvider = (props) => {
const { action, readPretty, params } = props; const { action, readPretty, params, updateAssociationValues } = props;
const field = useField(); const field = useField();
const form = useMemo( const form = useMemo(
() => () =>
@ -37,7 +38,7 @@ const InternalFormBlockProvider = (props) => {
field, field,
service, service,
resource, resource,
updateAssociationValues: [], updateAssociationValues,
formBlockRef, formBlockRef,
}} }}
> >
@ -72,7 +73,7 @@ const getAssociationAppends = (schema, arr = []) => {
if (s['x-component-props'].mode === 'Nester') { if (s['x-component-props'].mode === 'Nester') {
return getAssociationAppends(s, buf); return getAssociationAppends(s, buf);
} }
return buf return buf;
} else { } else {
return getAssociationAppends(s, buf); return getAssociationAppends(s, buf);
} }
@ -104,12 +105,21 @@ const useAssociationNames = (collection) => {
const gridSchema = formSchema.properties.grid; const gridSchema = formSchema.properties.grid;
const data = []; const data = [];
gridSchema.reduceProperties((buf, s) => { gridSchema.reduceProperties((buf, s) => {
buf.push(getAssociationAppends(s).join('.')); buf.push(getAssociationAppends(s));
return buf; return buf;
}, data); }, data);
const appends = data.filter((g) => g.length); const associations = data.filter((g) => g.length);
console.log(appends); const appends = associations.map((v) => {
return appends; return v.join('.');
});
const updateAssociationValues = associations.map((k) => {
return k.map((v, index) => {
const s = index > 0 ? k.slice(0, index + 1) : [v];
return s.join('.');
});
});
return { appends, updateAssociationValues };
}; };
export const FormBlockProvider = (props) => { export const FormBlockProvider = (props) => {
@ -120,7 +130,7 @@ export const FormBlockProvider = (props) => {
const currentCollection = useCollection(); const currentCollection = useCollection();
const { designable } = useDesignable(); const { designable } = useDesignable();
const isEmptyRecord = useIsEmptyRecord(); const isEmptyRecord = useIsEmptyRecord();
const appends = useAssociationNames(collection); const { appends, updateAssociationValues } = useAssociationNames(collection);
if (!Object.keys(params).includes('appends')) { if (!Object.keys(params).includes('appends')) {
params['appends'] = appends; params['appends'] = appends;
} }
@ -136,7 +146,7 @@ export const FormBlockProvider = (props) => {
return ( return (
(detailFlag || createFlag) && ( (detailFlag || createFlag) && (
<BlockProvider {...props} block={'form'} params={params}> <BlockProvider {...props} block={'form'} params={params}>
<InternalFormBlockProvider {...props} params={params} /> <InternalFormBlockProvider {...props} params={params} updateAssociationValues={updateAssociationValues} />
</BlockProvider> </BlockProvider>
) )
); );