mirror of
https://github.com/nocobase/nocobase
synced 2024-11-15 06:46:38 +00:00
fix: fix linkage style not updating in form (#5539)
* refactor: update useDataFormItemProps hook * refactor: update useDataFormItemProps hook to handle null values in form and subForm
This commit is contained in:
parent
7323cefcdd
commit
0c034c104e
@ -7,11 +7,18 @@
|
||||
* For more information, please refer to: https://www.nocobase.com/agreement.
|
||||
*/
|
||||
|
||||
import { useForm } from '@formily/react';
|
||||
import { useCollectionRecordData } from '../../../../../data-source/collection-record/CollectionRecordProvider';
|
||||
import { useSatisfiedActionValues } from '../../../../../schema-settings/LinkageRules/useActionValues';
|
||||
import { useFormBlockContext } from '../../../../../block-provider';
|
||||
import { useSubFormValue } from '../../../../../schema-component/antd/association-field/hooks';
|
||||
export function useDataFormItemProps() {
|
||||
const data = useCollectionRecordData();
|
||||
const { valueMap: style } = useSatisfiedActionValues({ category: 'style', formValues: data });
|
||||
const record = useCollectionRecordData();
|
||||
const { form } = useFormBlockContext();
|
||||
const subForm = useSubFormValue();
|
||||
const { valueMap: style } = useSatisfiedActionValues({
|
||||
category: 'style',
|
||||
formValues: subForm?.formValue || form?.values || record,
|
||||
form,
|
||||
});
|
||||
return { wrapperStyle: style };
|
||||
}
|
||||
|
@ -7,7 +7,9 @@
|
||||
* For more information, please refer to: https://www.nocobase.com/agreement.
|
||||
*/
|
||||
|
||||
import { useState, useEffect } from 'react';
|
||||
import { useState, useEffect, useCallback } from 'react';
|
||||
import { uid } from '@formily/shared';
|
||||
import { Form, onFormValuesChange } from '@formily/core';
|
||||
import { useVariables, useLocalVariables } from '../../variables';
|
||||
import { useFieldSchema } from '@formily/react';
|
||||
import { LinkageRuleCategory, LinkageRuleDataKeyMap } from './type';
|
||||
@ -18,11 +20,13 @@ export function useSatisfiedActionValues({
|
||||
category = 'default',
|
||||
rules,
|
||||
schema,
|
||||
form,
|
||||
}: {
|
||||
category: `${LinkageRuleCategory}`;
|
||||
formValues: Record<string, any>;
|
||||
rules?: any;
|
||||
schema?: any;
|
||||
form?: Form;
|
||||
}) {
|
||||
const [valueMap, setValueMap] = useState({});
|
||||
const fieldSchema = useFieldSchema();
|
||||
@ -30,8 +34,7 @@ export function useSatisfiedActionValues({
|
||||
const localVariables = useLocalVariables({ currentForm: { values: formValues } as any });
|
||||
const localSchema = schema ?? fieldSchema;
|
||||
const linkageRules = rules ?? localSchema[LinkageRuleDataKeyMap[category]];
|
||||
|
||||
useEffect(() => {
|
||||
const compute = useCallback(() => {
|
||||
if (linkageRules && formValues) {
|
||||
getSatisfiedValueMap({ rules: linkageRules, variables, localVariables })
|
||||
.then((valueMap) => {
|
||||
@ -43,6 +46,22 @@ export function useSatisfiedActionValues({
|
||||
throw new Error(err.message);
|
||||
});
|
||||
}
|
||||
}, [variables, localVariables, formValues, linkageRules]);
|
||||
}, [variables, localVariables, linkageRules, formValues]);
|
||||
useEffect(() => {
|
||||
compute();
|
||||
}, [compute]);
|
||||
useEffect(() => {
|
||||
if (form) {
|
||||
const id = uid();
|
||||
form.addEffects(id, () => {
|
||||
onFormValuesChange(() => {
|
||||
compute();
|
||||
});
|
||||
});
|
||||
return () => {
|
||||
form.removeEffects(id);
|
||||
};
|
||||
}
|
||||
}, [form, compute]);
|
||||
return { valueMap };
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user