From 41c2947c2cd2b7a2147f569f10971f4ff5ff6069 Mon Sep 17 00:00:00 2001 From: selankon Date: Tue, 29 Oct 2024 16:12:58 +0100 Subject: [PATCH] chore(meshconfig): fix form --- .../src/components/FormEdit.tsx | 49 ++++++---- .../src/components/FormOption.tsx | 8 +- .../src/components/FormSection.tsx | 43 ++------- .../src/components/modals.tsx | 96 +++++++++++-------- .../src/containers/LimeConfigEditForm.tsx | 2 + src/components/inputs/InputField.tsx | 2 - 6 files changed, 96 insertions(+), 104 deletions(-) diff --git a/plugins/lime-plugin-mesh-wide-config/src/components/FormEdit.tsx b/plugins/lime-plugin-mesh-wide-config/src/components/FormEdit.tsx index 49cab621..902d5e88 100644 --- a/plugins/lime-plugin-mesh-wide-config/src/components/FormEdit.tsx +++ b/plugins/lime-plugin-mesh-wide-config/src/components/FormEdit.tsx @@ -1,5 +1,5 @@ import { Trans, t } from "@lingui/macro"; -import { StateUpdater, useEffect, useState } from "preact/hooks"; +import { useEffect, useState } from "preact/hooks"; import { Controller, useFormContext } from "react-hook-form"; import { v4 as uuidv4 } from "uuid"; @@ -18,30 +18,37 @@ import { IMeshWideConfig } from "plugins/lime-plugin-mesh-wide-config/src/meshCo export const EditableField = ({ isList, name, - setIsEditing, }: { isList: boolean; name: string; - setIsEditing?: StateUpdater; }) => { const { control, setValue, watch, getValues } = useFormContext(); const value = watch(name); - const [initialState] = useState(value); // Hack to force re-render when the list changes const [uniqueKeys, setUniqueKeys] = useState( - isList ? value.map(() => uuidv4()) : "" + isList && value?.length ? value.map(() => uuidv4()) : [] ); + const syncKeysWithValues = () => { + // Ensure uniqueKeys matches the length of value array + setUniqueKeys((keys) => [ + ...keys, + ...Array(value.length - keys.length) + .fill(null) + .map(() => uuidv4()), + ]); + }; + const removeListItem = (index) => { const updatedValues = value.filter((_, i) => i !== index); - setValue(name, updatedValues); // Update form values - setUniqueKeys((keys) => keys.filter((_, i) => i !== index)); // Update keys to match the new array + setValue(name, updatedValues); + setUniqueKeys((keys) => keys.filter((_, i) => i !== index)); }; const addListItem = () => { - setValue(name, [...value, ""]); // Update form values - setUniqueKeys((keys) => [...keys, uuidv4()]); // Add a new unique key + setValue(name, [...value, ""]); + setUniqueKeys((keys) => [...keys, uuidv4()]); }; // Ensure the list has at least one item at the start @@ -49,6 +56,8 @@ export const EditableField = ({ if (isList && value.length === 0) { setValue(name, [""]); setUniqueKeys([uuidv4()]); // Reset keys for new list + } else { + syncKeysWithValues(); // Sync keys with values length on every render } }, [isList, value, name, setValue]); @@ -67,10 +76,7 @@ export const EditableField = ({ }, required: t`This field cannot be empty`, }} - render={({ - field: { value, ...rest }, - fieldState: { error }, - }) => { + render={({ field, fieldState: { error } }) => { return (
removeListItem(index)} @@ -126,10 +131,11 @@ export const AddNewConfigSection = ({ }: { sectionName?: string; }) => { + const { watch, setValue } = useFormContext(); + const { open, onOpen, onClose } = useDisclosure(); const { showToast } = useToast(); - const { watch, setValue } = useFormContext(); const section = watch(sectionName); const onSuccess = (data: AddNewSectionFormProps) => { @@ -166,8 +172,13 @@ export const AddNewConfigSection = ({ export const AddElementButton = (props: ButtonProps) => { return ( - +
+ +
); }; diff --git a/plugins/lime-plugin-mesh-wide-config/src/components/FormOption.tsx b/plugins/lime-plugin-mesh-wide-config/src/components/FormOption.tsx index 5a9eae2b..0c2c5c54 100644 --- a/plugins/lime-plugin-mesh-wide-config/src/components/FormOption.tsx +++ b/plugins/lime-plugin-mesh-wide-config/src/components/FormOption.tsx @@ -82,12 +82,8 @@ export const OptionContainer = ({ {!isEditing &&
{_value}
} {isEditing && ( -
- + +