From 7b47533036f79404d1a4f9ce26b639ffb38fb093 Mon Sep 17 00:00:00 2001 From: laura stordeur Date: Tue, 30 Apr 2024 17:23:17 +0200 Subject: [PATCH] Propagate suggestions to embedded entities and allow 0 as valid default input (Issue #5717, PR #5725) # Description 1. Propagate suggested values to embedded fields 2. Allow 0 as a valid default value. closes : #5717 and #5713 https://github.com/inmanta/web-console/assets/44098050/33b19622-55b4-48b0-b242-a1e0522262a8 --- ...717-suggested-values-embedded-entities.yml | 6 ++++++ src/Test/Data/ServiceInstance/index.ts | 2 +- .../Components/FieldInput.tsx | 20 ++++++++++++++++++- .../Components/TextFormInput.tsx | 6 +++--- .../Helpers/createFormState.spec.ts | 2 +- 5 files changed, 30 insertions(+), 6 deletions(-) create mode 100644 changelogs/unreleased/5717-suggested-values-embedded-entities.yml diff --git a/changelogs/unreleased/5717-suggested-values-embedded-entities.yml b/changelogs/unreleased/5717-suggested-values-embedded-entities.yml new file mode 100644 index 000000000..0e33b41ac --- /dev/null +++ b/changelogs/unreleased/5717-suggested-values-embedded-entities.yml @@ -0,0 +1,6 @@ +description: "Propagate suggestions to embedded entities and allow 0 as valid default input" +issue-nr: 5717 +change-type: patch +destination-branches: [master, iso7] +sections: + minor-improvement: "{{description}}" 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 3b2f29a34..2bc330314 100644 --- a/src/UI/Components/ServiceInstanceForm/Components/FieldInput.tsx +++ b/src/UI/Components/ServiceInstanceForm/Components/FieldInput.tsx @@ -87,6 +87,20 @@ export const FieldInput: React.FC = ({ ).useOneTime(); 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) { + 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 const getEnumUpdate = useCallback( (value) => { @@ -205,7 +219,9 @@ export const FieldInput: React.FC = ({ = ({ originalState={originalState} getUpdate={getUpdate} path={makePath(path, field.name)} + suggestions={childField.suggestion} /> ))} @@ -595,6 +612,7 @@ const DictListFieldInput: React.FC = ({ isNew={addedItemsPaths.includes( `${makePath(path, field.name)}.${index}`, )} + suggestions={childField.suggestion} /> ))} diff --git a/src/UI/Components/ServiceInstanceForm/Components/TextFormInput.tsx b/src/UI/Components/ServiceInstanceForm/Components/TextFormInput.tsx index 0b4581c7d..e0cc49e9c 100644 --- a/src/UI/Components/ServiceInstanceForm/Components/TextFormInput.tsx +++ b/src/UI/Components/ServiceInstanceForm/Components/TextFormInput.tsx @@ -32,7 +32,7 @@ interface Props { * @component * @param {Props} props - The props for the TextListFormInput component. * @prop {string} attributeName - The name of the attribute. - * @prop {string[]} attributeValue - The value of the attribute. + * @prop {string} attributeValue - The value of the attribute. * @prop {string} description - The description of the attribute. * @prop {boolean} isOptional - Whether the attribute is optional. * @prop {boolean} shouldBeDisabled - Whether the attribute should be disabled. Default is false. @@ -58,7 +58,7 @@ export const TextFormInput: React.FC = ({ }) => { const inputRef = React.useRef(null); const [isOpen, setIsOpen] = React.useState(false); - const [inputValue, setInputValue] = React.useState(attributeValue || ""); + const [inputValue, setInputValue] = React.useState(attributeValue); /** * Handles the input change. @@ -124,7 +124,7 @@ export const TextFormInput: React.FC = ({ placeholder={placeholder} aria-describedby={`${attributeName}-helper`} aria-label={`TextInput-${attributeName}`} - value={inputValue || ""} + value={inputValue} onChange={(_event, value) => handleChange(value)} isDisabled={shouldBeDisabled} onFocus={() => setIsOpen(true)} 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: "{}", }, ], });