fix: fix default value is invalid in subform (#1989)

* fix: fix default value is invalid in subform

* perf: use useMemo

* fix: change null to {}
This commit is contained in:
被雨水过滤的空气-Rairn 2023-06-06 14:42:37 +08:00 committed by GitHub
parent d7e6b7b320
commit b33c00be8f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 11 deletions

View File

@ -36,7 +36,7 @@ export const AssociationFieldProvider = observer((props) => {
// Nester 子表单时,如果没数据初始化一个 [null] 的占位
if (currentMode === 'Nester' && Array.isArray(field.value)) {
if (field.value.length === 0 && ['belongsToMany', 'hasMany'].includes(collectionField.type)) {
field.value = [null];
field.value = [{}];
}
}
setLoading(false);
@ -46,7 +46,7 @@ export const AssociationFieldProvider = observer((props) => {
if (['belongsTo', 'hasOne'].includes(collectionField.type)) {
field.value = {};
} else if (['belongsToMany', 'hasMany'].includes(collectionField.type)) {
field.value = [null];
field.value = [{}];
}
}
setLoading(false);

View File

@ -60,7 +60,7 @@ const ToManyNester = observer((props) => {
startIndex: index + 1,
insertCount: 1,
});
field.value.splice(index + 1, 0, null);
field.value.splice(index + 1, 0, {});
return field.onInput(field.value);
});
}}

View File

@ -1,6 +1,7 @@
import flat from 'flat';
import _, { every, findIndex, some, isArray } from 'lodash';
import _, { every, findIndex, isArray, some } from 'lodash';
import moment from 'moment';
import { useMemo } from 'react';
import { useCurrentUserContext } from '../../../user';
import jsonLogic from '../../common/utils/logic';
@ -13,12 +14,14 @@ type VariablesCtx = {
export const useVariablesCtx = (): VariablesCtx => {
const { data } = useCurrentUserContext() || {};
return {
$user: data?.data || {},
$date: {
now: () => moment().toISOString(),
},
};
return useMemo(() => {
return {
$user: data?.data || {},
$date: {
now: () => moment().toISOString(),
},
};
}, [data]);
};
export const isVariable = (str: unknown) => {
@ -63,7 +66,7 @@ const getFieldValue = (fieldPath, values) => {
const regex = new RegExp('^' + v + '\\..+\\.' + h + '$'); // 构建匹配的正则表达式
const matchedValues = [];
const data = flat.flatten(values, { maxDepth: 3 });
for (var key in data) {
for (const key in data) {
if (regex.test(key)) {
matchedValues.push(data[key]);
}