From b33c00be8fc04690ef78e8add1451eb1dc5d0420 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A2=AB=E9=9B=A8=E6=B0=B4=E8=BF=87=E6=BB=A4=E7=9A=84?= =?UTF-8?q?=E7=A9=BA=E6=B0=94-Rairn?= <958414905@qq.com> Date: Tue, 6 Jun 2023 14:42:37 +0800 Subject: [PATCH] fix: fix default value is invalid in subform (#1989) * fix: fix default value is invalid in subform * perf: use useMemo * fix: change null to {} --- .../AssociationFieldProvider.tsx | 4 ++-- .../antd/association-field/Nester.tsx | 2 +- .../schema-component/common/utils/uitls.tsx | 19 +++++++++++-------- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/packages/core/client/src/schema-component/antd/association-field/AssociationFieldProvider.tsx b/packages/core/client/src/schema-component/antd/association-field/AssociationFieldProvider.tsx index 89bf697c2..2c81951f3 100644 --- a/packages/core/client/src/schema-component/antd/association-field/AssociationFieldProvider.tsx +++ b/packages/core/client/src/schema-component/antd/association-field/AssociationFieldProvider.tsx @@ -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); diff --git a/packages/core/client/src/schema-component/antd/association-field/Nester.tsx b/packages/core/client/src/schema-component/antd/association-field/Nester.tsx index f2b81a8f5..2c01c2f33 100644 --- a/packages/core/client/src/schema-component/antd/association-field/Nester.tsx +++ b/packages/core/client/src/schema-component/antd/association-field/Nester.tsx @@ -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); }); }} diff --git a/packages/core/client/src/schema-component/common/utils/uitls.tsx b/packages/core/client/src/schema-component/common/utils/uitls.tsx index 34600060f..eaeeebf2a 100644 --- a/packages/core/client/src/schema-component/common/utils/uitls.tsx +++ b/packages/core/client/src/schema-component/common/utils/uitls.tsx @@ -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]); }