From b30f9ddf7f2017c008ee0f9ada13f5aa915ac506 Mon Sep 17 00:00:00 2001 From: Cristhian Zanforlin Lousa Date: Mon, 18 Sep 2023 21:51:22 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(parameterComponent):=20conve?= =?UTF-8?q?rt=20parameter=20values=20to=20numbers=20before=20assigning=20t?= =?UTF-8?q?hem=20to=20improve=20data=20consistency=20=F0=9F=94=A7=20chore(?= =?UTF-8?q?reactflowUtils):=20add=20utility=20function=20to=20convert=20va?= =?UTF-8?q?lues=20to=20numbers=20in=20an=20array=20of=20objects?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../components/parameterComponent/index.tsx | 8 +++++--- src/frontend/src/utils/reactflowUtils.ts | 19 ++++++++++++++++++- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/frontend/src/CustomNodes/GenericNode/components/parameterComponent/index.tsx b/src/frontend/src/CustomNodes/GenericNode/components/parameterComponent/index.tsx index c61cadc2b..a9e279e21 100644 --- a/src/frontend/src/CustomNodes/GenericNode/components/parameterComponent/index.tsx +++ b/src/frontend/src/CustomNodes/GenericNode/components/parameterComponent/index.tsx @@ -28,6 +28,7 @@ import { ParameterComponentType } from "../../../../types/components"; import { TabsState } from "../../../../types/tabs"; import { convertObjToArray, + convertValuesToNumbers, hasDuplicateKeys, isValidConnection, } from "../../../../utils/reactflowUtils"; @@ -399,9 +400,10 @@ export default function ParameterComponent({ } duplicateKey={errorDuplicateKey} onChange={(newValue) => { - data.node!.template[name].value = newValue; - setErrorDuplicateKey(hasDuplicateKeys(newValue)); - handleOnNewValue(newValue); + const valueToNumbers = convertValuesToNumbers(newValue); + data.node!.template[name].value = valueToNumbers; + setErrorDuplicateKey(hasDuplicateKeys(valueToNumbers)); + handleOnNewValue(valueToNumbers); }} /> diff --git a/src/frontend/src/utils/reactflowUtils.ts b/src/frontend/src/utils/reactflowUtils.ts index da503befc..0e0e07155 100644 --- a/src/frontend/src/utils/reactflowUtils.ts +++ b/src/frontend/src/utils/reactflowUtils.ts @@ -317,4 +317,21 @@ export function hasDuplicateKeys(array) { } } return false; -} \ No newline at end of file +} + +export function convertValuesToNumbers(arr) { + return arr.map((obj) => { + const newObj = {}; + for (const key in obj) { + if (obj.hasOwnProperty(key)) { + let value = obj[key]; + if (/\s/g.test(value)) { + value = value.trim(); + } + newObj[key] = value === "" || isNaN(value) ? value.toString() : Number(value); + } + } + return newObj; + }); +} +