Merge branch 'main' into next

This commit is contained in:
GitHub Actions Bot 2024-08-28 02:52:23 +00:00
commit 1b9bc53754
3 changed files with 21 additions and 16 deletions

View File

@ -87,7 +87,7 @@ const InternalFormBlockProvider = (props) => {
updateAssociationValues,
]);
if (service.loading && Object.keys(form?.initialValues)?.length === 0 && action) {
if (service.loading && Object.keys(form?.initialValues || {})?.length === 0 && action) {
return <Spin />;
}
@ -151,9 +151,10 @@ export const useFormBlockContext = () => {
/**
* @internal
*/
export const useFormBlockProps = (shouldClearInitialValues = false) => {
export const useFormBlockProps = () => {
const ctx = useFormBlockContext();
const treeParentRecord = useTreeParentRecord();
useEffect(() => {
if (treeParentRecord) {
ctx.form?.query('parent').take((field) => {
@ -164,19 +165,14 @@ export const useFormBlockProps = (shouldClearInitialValues = false) => {
});
useEffect(() => {
if (!ctx?.service?.loading) {
const form: Form = ctx.form;
if (form) {
// form 字段中可能一开始就存在一些默认值(比如设置了字段默认值的模板区块)。在编辑表单中,
// 这些默认值是不需要的需要清除掉不然会导致一些问题。比如https://github.com/nocobase/nocobase/issues/4868
if (shouldClearInitialValues === true) {
form.initialValues = {};
form.reset();
}
form.setInitialValues(ctx.service?.data?.data);
}
const form: Form = ctx.form;
if (!form || ctx.service?.loading) {
return;
}
}, [ctx?.service?.loading]);
form.setInitialValues(ctx.service?.data?.data);
}, [ctx.form, ctx.service?.data?.data, ctx.service?.loading]);
return {
form: ctx.form,
};

View File

@ -10,5 +10,5 @@
import { useFormBlockProps } from '../../../../../block-provider/FormBlockProvider';
export function useEditFormBlockProps() {
return useFormBlockProps(true);
return useFormBlockProps();
}

View File

@ -14,6 +14,7 @@ import { getValuesByPath } from '@nocobase/utils/client';
import _ from 'lodash';
import { useCallback, useEffect } from 'react';
import { useRecordIndex } from '../../../../../src/record-provider';
import { useFormBlockContext } from '../../../../block-provider/FormBlockProvider';
import { useCollection_deprecated } from '../../../../collection-manager';
import { useCollectionRecord } from '../../../../data-source/collection-record/CollectionRecordProvider';
import { useFlag } from '../../../../flag-provider';
@ -39,6 +40,7 @@ const useParseDefaultValue = () => {
const { getField } = useCollection_deprecated();
const { isSpecialCase, setDefaultValue } = useSpecialCase();
const index = useRecordIndex();
const { type, form } = useFormBlockContext();
/**
* name: $user
@ -55,6 +57,13 @@ const useParseDefaultValue = () => {
);
useEffect(() => {
// fix https://github.com/nocobase/nocobase/issues/4868
// fix http://localhost:12000/admin/ugmnj2ycfgg/popups/1qlw5c38t3b/puid/dz42x7ffr7i/filterbytk/182
// to clear the default value of the field
if (type === 'update' && fieldSchema.default && field.form === form) {
field.setValue?.(record?.data?.[fieldSchema.name]);
}
if (
fieldSchema.default == null ||
isInSetDefaultValueDialog ||
@ -154,7 +163,7 @@ const useParseDefaultValue = () => {
// 解决子表格(或子表单)中新增一行数据时,默认值不生效的问题
field.setValue(fieldSchema.default);
}
}, [fieldSchema.default, localVariables]);
}, [fieldSchema.default, localVariables, type]);
};
export default useParseDefaultValue;