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:
parent
d7e6b7b320
commit
b33c00be8f
@ -36,7 +36,7 @@ export const AssociationFieldProvider = observer((props) => {
|
|||||||
// Nester 子表单时,如果没数据初始化一个 [null] 的占位
|
// Nester 子表单时,如果没数据初始化一个 [null] 的占位
|
||||||
if (currentMode === 'Nester' && Array.isArray(field.value)) {
|
if (currentMode === 'Nester' && Array.isArray(field.value)) {
|
||||||
if (field.value.length === 0 && ['belongsToMany', 'hasMany'].includes(collectionField.type)) {
|
if (field.value.length === 0 && ['belongsToMany', 'hasMany'].includes(collectionField.type)) {
|
||||||
field.value = [null];
|
field.value = [{}];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
@ -46,7 +46,7 @@ export const AssociationFieldProvider = observer((props) => {
|
|||||||
if (['belongsTo', 'hasOne'].includes(collectionField.type)) {
|
if (['belongsTo', 'hasOne'].includes(collectionField.type)) {
|
||||||
field.value = {};
|
field.value = {};
|
||||||
} else if (['belongsToMany', 'hasMany'].includes(collectionField.type)) {
|
} else if (['belongsToMany', 'hasMany'].includes(collectionField.type)) {
|
||||||
field.value = [null];
|
field.value = [{}];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
|
@ -60,7 +60,7 @@ const ToManyNester = observer((props) => {
|
|||||||
startIndex: index + 1,
|
startIndex: index + 1,
|
||||||
insertCount: 1,
|
insertCount: 1,
|
||||||
});
|
});
|
||||||
field.value.splice(index + 1, 0, null);
|
field.value.splice(index + 1, 0, {});
|
||||||
return field.onInput(field.value);
|
return field.onInput(field.value);
|
||||||
});
|
});
|
||||||
}}
|
}}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import flat from 'flat';
|
import flat from 'flat';
|
||||||
import _, { every, findIndex, some, isArray } from 'lodash';
|
import _, { every, findIndex, isArray, some } from 'lodash';
|
||||||
import moment from 'moment';
|
import moment from 'moment';
|
||||||
|
import { useMemo } from 'react';
|
||||||
import { useCurrentUserContext } from '../../../user';
|
import { useCurrentUserContext } from '../../../user';
|
||||||
import jsonLogic from '../../common/utils/logic';
|
import jsonLogic from '../../common/utils/logic';
|
||||||
|
|
||||||
@ -13,12 +14,14 @@ type VariablesCtx = {
|
|||||||
export const useVariablesCtx = (): VariablesCtx => {
|
export const useVariablesCtx = (): VariablesCtx => {
|
||||||
const { data } = useCurrentUserContext() || {};
|
const { data } = useCurrentUserContext() || {};
|
||||||
|
|
||||||
return {
|
return useMemo(() => {
|
||||||
$user: data?.data || {},
|
return {
|
||||||
$date: {
|
$user: data?.data || {},
|
||||||
now: () => moment().toISOString(),
|
$date: {
|
||||||
},
|
now: () => moment().toISOString(),
|
||||||
};
|
},
|
||||||
|
};
|
||||||
|
}, [data]);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const isVariable = (str: unknown) => {
|
export const isVariable = (str: unknown) => {
|
||||||
@ -63,7 +66,7 @@ const getFieldValue = (fieldPath, values) => {
|
|||||||
const regex = new RegExp('^' + v + '\\..+\\.' + h + '$'); // 构建匹配的正则表达式
|
const regex = new RegExp('^' + v + '\\..+\\.' + h + '$'); // 构建匹配的正则表达式
|
||||||
const matchedValues = [];
|
const matchedValues = [];
|
||||||
const data = flat.flatten(values, { maxDepth: 3 });
|
const data = flat.flatten(values, { maxDepth: 3 });
|
||||||
for (var key in data) {
|
for (const key in data) {
|
||||||
if (regex.test(key)) {
|
if (regex.test(key)) {
|
||||||
matchedValues.push(data[key]);
|
matchedValues.push(data[key]);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user