From b59f74813d90fac302c664f2bce7bc44cf4e9b77 Mon Sep 17 00:00:00 2001 From: laura stordeur Date: Tue, 30 Apr 2024 14:43:13 +0200 Subject: [PATCH] update method for dicts that aren't strings --- src/Test/Data/ServiceInstance/index.ts | 2 +- .../ServiceInstanceForm/Components/FieldInput.tsx | 11 ++++++++++- .../Helpers/createFormState.spec.ts | 2 +- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/Test/Data/ServiceInstance/index.ts b/src/Test/Data/ServiceInstance/index.ts index 2aec33310..01b5e7937 100644 --- a/src/Test/Data/ServiceInstance/index.ts +++ b/src/Test/Data/ServiceInstance/index.ts @@ -66,7 +66,7 @@ export const nestedEditable: ServiceInstanceModelWithTargetStates = { { my_attr: 0, bool_attr: null, - dict_attr: { a: "b" }, + dict_attr: {}, embedded_single: { attr4: [2, 4] }, }, ], diff --git a/src/UI/Components/ServiceInstanceForm/Components/FieldInput.tsx b/src/UI/Components/ServiceInstanceForm/Components/FieldInput.tsx index 1964570dd..2bc330314 100644 --- a/src/UI/Components/ServiceInstanceForm/Components/FieldInput.tsx +++ b/src/UI/Components/ServiceInstanceForm/Components/FieldInput.tsx @@ -88,8 +88,17 @@ export const FieldInput: React.FC = ({ const [suggestionsList, setSuggestionsList] = useState(null); // Get the controlled value for the field + // If the value is an object or an array, it needs to be converted. function getControlledValue(value) { - return value !== null && value !== undefined ? value : ""; + if (value === null || value === undefined) { + return ""; + } else if (Array.isArray(value)) { + return value.join(", "); + } else if (typeof value === "object") { + return JSON.stringify(value); + } else { + return value; + } } //callback was used to avoid re-render in useEffect used in SelectFormInput diff --git a/src/UI/Components/ServiceInstanceForm/Helpers/createFormState.spec.ts b/src/UI/Components/ServiceInstanceForm/Helpers/createFormState.spec.ts index 252f15396..00f0246af 100644 --- a/src/UI/Components/ServiceInstanceForm/Helpers/createFormState.spec.ts +++ b/src/UI/Components/ServiceInstanceForm/Helpers/createFormState.spec.ts @@ -47,7 +47,7 @@ test("Given createEditFormState v1 WHEN passed editable nested fields and curren }, my_attr: 0, bool_attr: null, - dict_attr: '{"a":"b"}', + dict_attr: "{}", }, ], });