Skip to content

Commit

Permalink
fix(pyramide): problème de validation des attributs lors de la création
Browse files Browse the repository at this point in the history
  • Loading branch information
tonai committed Dec 2, 2024
1 parent b348118 commit 95c6dbd
Showing 1 changed file with 5 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ const TableAttributeSelection: FC<TableAttributeSelectionProps> = ({ visible, fo
setValue: setFormValue,
getValues: getFormValues,
formState: { errors },
watch,
} = form;

const tableAttributes: Record<string, string[]> = watch("table_attributes") ?? {};
const tableAttributes: Record<string, string[]> = getFormValues("table_attributes") ?? {};

useEffect(() => {
const prevTableAttributes = getFormValues("table_attributes") ?? {};
Expand All @@ -33,9 +32,8 @@ const TableAttributeSelection: FC<TableAttributeSelectionProps> = ({ visible, fo
setFormValue("table_attributes", tableAttributes);
}, [getFormValues, setFormValue, selectedTables]);

const toggleAttributes = (tableName: string, attrName: string) => {
const prevTableAttributes = { ...tableAttributes };
let tableAttr: string[] = prevTableAttributes[tableName];
const toggleAttributes = (tableName: string, attrName: string, tableAttributes: string[]) => {
let tableAttr = [...tableAttributes];

if (tableAttr.includes(attrName)) {
tableAttr = tableAttr.filter((el) => el !== attrName);
Expand All @@ -44,9 +42,7 @@ const TableAttributeSelection: FC<TableAttributeSelectionProps> = ({ visible, fo
tableAttr = Array.from(new Set(tableAttr));
}

prevTableAttributes[tableName] = tableAttr;

setFormValue("table_attributes", { ...prevTableAttributes }, { shouldValidate: true });
setFormValue(`table_attributes.${tableName}`, tableAttr, { shouldValidate: true });
};

return (
Expand All @@ -67,7 +63,7 @@ const TableAttributeSelection: FC<TableAttributeSelectionProps> = ({ visible, fo
nativeInputProps: {
value: attrName,
checked: tableAttributes?.[table.name]?.includes(attrName) ?? false,
onChange: () => toggleAttributes(table.name, attrName),
onChange: () => toggleAttributes(table.name, attrName, tableAttributes[table.name]),
},
}))}
state={errors?.table_attributes?.[table.name]?.message ? "error" : "default"}
Expand Down

0 comments on commit 95c6dbd

Please sign in to comment.