Skip to content

Commit 3a65d35

Browse files
authored
Merge pull request #1365 from dxc-technology/aida-form-input
Fixed text input's action that executed form submit when clicked
2 parents 336cd81 + 5169570 commit 3a65d35

File tree

5 files changed

+72
-7
lines changed

5 files changed

+72
-7
lines changed

lib/src/number-input/NumberInput.test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,4 +262,28 @@ describe("Number input component tests", () => {
262262
const increment = getAllByRole("button")[1];
263263
expect(increment.getAttribute("aria-label")).toBe("Increment value");
264264
});
265+
266+
test("Number input submits correct values in a form", () => {
267+
const handlerOnSubmit = jest.fn((e) => {
268+
e.preventDefault();
269+
const formData = new FormData(e.target);
270+
const formProps = Object.fromEntries(formData);
271+
expect(formProps).toStrictEqual({ data: "0" });
272+
});
273+
const { getByText, getAllByRole } = render(
274+
<form onSubmit={handlerOnSubmit}>
275+
<DxcNumberInput label="Number input label" name="data" />
276+
<button type="submit">Submit</button>
277+
</form>
278+
);
279+
const less = getAllByRole("button")[0];
280+
const more = getAllByRole("button")[1];
281+
const submit = getByText("Submit");
282+
userEvent.click(more);
283+
expect(handlerOnSubmit).not.toHaveBeenCalled();
284+
userEvent.click(less);
285+
expect(handlerOnSubmit).not.toHaveBeenCalled();
286+
userEvent.click(submit);
287+
expect(handlerOnSubmit).toHaveBeenCalled();
288+
});
265289
});

lib/src/password-input/PasswordInput.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,14 @@ const DxcPasswordInput = React.forwardRef<RefType, PasswordInputPropsType>(
6666
}
6767
}, [isPasswordVisible]);
6868

69-
const viewPassword = (event) => {
69+
const viewPassword = () => {
7070
setInputType("text");
7171
setIsPasswordVisible(true);
72-
event.preventDefault();
7372
};
7473

75-
const hidePassword = (event) => {
74+
const hidePassword = () => {
7675
setInputType("password");
7776
setIsPasswordVisible(false);
78-
event.preventDefault();
7977
};
8078

8179
const action = {

lib/src/text-input/TextInput.test.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,47 @@ describe("TextInput component tests", () => {
296296
userEvent.click(getByRole("button"));
297297
expect(onClick).toHaveBeenCalled();
298298
});
299+
test("Text input submit correctly value in form", () => {
300+
const onClick = jest.fn();
301+
const action = {
302+
onClick: onClick,
303+
icon: (
304+
<svg
305+
data-testid="image"
306+
xmlns="http://www.w3.org/2000/svg"
307+
height="24px"
308+
viewBox="0 0 24 24"
309+
width="24px"
310+
fill="currentColor"
311+
>
312+
<path d="M0 0h24v24H0V0z" fill="none" />
313+
<path d="M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z" />
314+
</svg>
315+
),
316+
title: "Search",
317+
};
318+
const handlerOnSubmit = jest.fn((e) => {
319+
e.preventDefault();
320+
const formData = new FormData(e.target);
321+
const formProps = Object.fromEntries(formData);
322+
expect(formProps).toStrictEqual({ data: "test" });
323+
});
324+
const { getByText, getAllByRole, getByRole } = render(
325+
<form onSubmit={handlerOnSubmit}>
326+
<DxcTextInput label="Input label" name="data" action={action} />
327+
<button type="submit">Submit</button>
328+
</form>
329+
);
330+
const search = getAllByRole("button")[0];
331+
const submit = getByText("Submit");
332+
const input = getByRole("textbox");
333+
userEvent.type(input, "test");
334+
expect(input.value).toBe("test");
335+
userEvent.click(search);
336+
expect(handlerOnSubmit).not.toHaveBeenCalled();
337+
userEvent.click(submit);
338+
expect(handlerOnSubmit).toHaveBeenCalled();
339+
});
299340
test("Renders with correct prefix and suffix", () => {
300341
const { getByText } = render(<DxcTextInput label="Input label" prefix="+34" suffix="USD" />);
301342
expect(getByText("+34")).toBeTruthy();

lib/src/text-input/TextInput.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,7 @@ const DxcTextInput = React.forwardRef<RefType, TextInputPropsType>(
463463
tabIndex={tabIndex}
464464
title={translatedLabels.textInput.clearFieldActionTitle}
465465
aria-label={translatedLabels.textInput.clearFieldActionTitle}
466+
type="button"
466467
>
467468
{icons.clear}
468469
</Action>
@@ -480,6 +481,7 @@ const DxcTextInput = React.forwardRef<RefType, TextInputPropsType>(
480481
tabIndex={tabIndex}
481482
title={translatedLabels.numberInput.decrementValueTitle}
482483
aria-label={translatedLabels.numberInput.decrementValueTitle}
484+
type="button"
483485
>
484486
{icons.decrement}
485487
</Action>
@@ -494,6 +496,7 @@ const DxcTextInput = React.forwardRef<RefType, TextInputPropsType>(
494496
tabIndex={tabIndex}
495497
title={translatedLabels.numberInput.incrementValueTitle}
496498
aria-label={translatedLabels.numberInput.incrementValueTitle}
499+
type="button"
497500
>
498501
{icons.increment}
499502
</Action>
@@ -511,6 +514,7 @@ const DxcTextInput = React.forwardRef<RefType, TextInputPropsType>(
511514
aria-label={action.title}
512515
backgroundType={backgroundType}
513516
tabIndex={tabIndex}
517+
type="button"
514518
>
515519
{typeof action.icon === "string" ? <ActionIcon src={action.icon}></ActionIcon> : action.icon}
516520
</Action>

lib/src/text-input/types.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import React from "react";
2-
31
type Space = "xxsmall" | "xsmall" | "small" | "medium" | "large" | "xlarge" | "xxlarge";
42
type Margin = {
53
top?: Space;
@@ -13,7 +11,7 @@ type Action = {
1311
/**
1412
* This function will be called when the user clicks the action.
1513
*/
16-
onClick: (event?: React.MouseEvent<HTMLInputElement>) => void;
14+
onClick: () => void;
1715
/**
1816
* Icon to be shown in the action.
1917
*/

0 commit comments

Comments
 (0)