From a42fe7a7718a510806f7d1543ccdb7822b479489 Mon Sep 17 00:00:00 2001 From: agonzalez97 Date: Wed, 2 Nov 2022 15:28:44 +0100 Subject: [PATCH 1/2] Fixed file input value when used on forms --- lib/src/file-input/FileInput.test.js | 27 ++++++++++++++++++++++++++ lib/src/file-input/FileInput.tsx | 29 ++++++++++++++++++++++------ 2 files changed, 50 insertions(+), 6 deletions(-) diff --git a/lib/src/file-input/FileInput.test.js b/lib/src/file-input/FileInput.test.js index ae790064f..13c06f5f9 100644 --- a/lib/src/file-input/FileInput.test.js +++ b/lib/src/file-input/FileInput.test.js @@ -281,4 +281,31 @@ describe("FileInput component tests", () => { ]); }); }); + + test("File input sends value when submitted in a form", () => { + const newFile = new File(["newFile"], "newFile.pdf", { type: "pdf" }); + const handlerOnSubmit = jest.fn((e) => { + e.preventDefault(); + const formData = new FormData(e.target); + const formProps = Object.fromEntries(formData); + expect(formProps).toStrictEqual({ file: newFile }); + }); + const { getByText, getByLabelText } = render( +
+ + + + ); + const inputFile = getByLabelText("File input label", { hidden: true }); + const submit = getByText("Submit"); + fireEvent.change(inputFile, { target: { files: [newFile] } }); + userEvent.click(submit); + }); }); diff --git a/lib/src/file-input/FileInput.tsx b/lib/src/file-input/FileInput.tsx index 89a068872..d99f55deb 100644 --- a/lib/src/file-input/FileInput.tsx +++ b/lib/src/file-input/FileInput.tsx @@ -186,7 +186,17 @@ const DxcFileInput = ({ {helperText} {mode === "file" ? ( - +