From 250ad7007c36ffbc313db2e984c4ab34dc277166 Mon Sep 17 00:00:00 2001 From: Maxime Lalo Date: Thu, 27 Apr 2023 17:20:16 +0200 Subject: [PATCH] :bug: Checkboxes in form --- .../Components/DesignSystem/Form/index.tsx | 26 +++++++++++++++---- .../Layouts/Folder/AskDocuments/index.tsx | 2 +- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/front/Components/DesignSystem/Form/index.tsx b/src/front/Components/DesignSystem/Form/index.tsx index e37ada66..39a0bfd2 100644 --- a/src/front/Components/DesignSystem/Form/index.tsx +++ b/src/front/Components/DesignSystem/Form/index.tsx @@ -92,10 +92,11 @@ export default class Form extends React.Component { if (this.props.onValidated) this.props.onValidated(); const allChildren = this.getAllChildrenFields(e); - const elementsValues = allChildren.reduce( - (obj, element) => ({ ...obj, [element.getAttribute("name") ?? ""]: (element as any).value }), - {}, - ); + const elementsValues = allChildren + .filter((e) => { + return e.getAttribute("type") !== "radio" && e.getAttribute("type") !== "checkbox"; + }) + .reduce((obj, element) => ({ ...obj, [element.getAttribute("name") ?? ""]: (element as any).value }), {}); const radioInputs = allChildren.filter((e) => e.getAttribute("type") === "radio").filter((e) => (e as any).checked); const radioInputsValues = radioInputs.reduce( @@ -103,11 +104,26 @@ export default class Form extends React.Component { {}, ); + const checkBoxesInput = allChildren.filter((e) => e.getAttribute("type") === "checkbox").filter((e) => (e as any).checked); + const checkBoxesValues = checkBoxesInput.reduce((obj, element) => { + const inputName = element.getAttribute("name") ?? ""; + const inputValue = (element as any).value as string; + const newValue = (obj as any)[inputName] as string[] ?? []; + newValue.push(inputValue); + return { + ...obj, + [inputName]: newValue, + }; + }, {}); + const allInputs = { ...elementsValues, ...radioInputsValues, - } + ...checkBoxesValues, + }; + // Deleting empty input + delete (allInputs as any)[""]; if (this.props.onSubmit) { this.props.onSubmit(e, allInputs, this.onSubmitErrorApi); } diff --git a/src/front/Components/Layouts/Folder/AskDocuments/index.tsx b/src/front/Components/Layouts/Folder/AskDocuments/index.tsx index 7e93a30b..2dfbd2a8 100644 --- a/src/front/Components/Layouts/Folder/AskDocuments/index.tsx +++ b/src/front/Components/Layouts/Folder/AskDocuments/index.tsx @@ -66,7 +66,7 @@ export default class AskDocuments extends BasePage {
{this.documentsType.map((documentType) => ( - + ))}