🐛(react) fix props not recognized on the input element

The props propagation was adding unknown props to the input element,
which was causing an error warning to be displayed in the console.
This commit is contained in:
Anthony Le Courric
2023-09-11 11:40:15 +02:00
committed by Anthony LC
parent 8086a48672
commit c7000f37d2
9 changed files with 190 additions and 27 deletions

View File

@@ -2,10 +2,17 @@ import { render, screen, waitFor } from "@testing-library/react";
import React, { useRef } from "react";
import userEvent from "@testing-library/user-event";
import { expect } from "vitest";
import { Input } from ":/components/Forms/Input/index";
import { FieldProps } from "../Field";
import { Input, InputOnlyProps } from ":/components/Forms/Input/index";
import { Button } from ":/components/Button";
const spyError = vi.spyOn(global.console, "error");
describe("<Input/>", () => {
afterAll(() => {
spyError.mockRestore();
});
it("renders and can type", async () => {
const user = userEvent.setup();
render(<Input label="First name" />);
@@ -208,4 +215,24 @@ describe("<Input/>", () => {
await user.clear(input);
screen.getByText("Value: .");
});
it("checks the props doesn't create error warning", async () => {
const propsInput: Required<FieldProps & InputOnlyProps> = {
label: "First name",
fullWidth: true,
charCounter: true,
charCounterMax: 15,
className: "c__field--full-width",
compact: false,
state: "default",
icon: "my icon",
rightIcon: "my right icon",
text: "my text",
textItems: ["my text item 1", "my text item 2"],
rightText: "my right text",
};
render(<Input {...propsInput} />);
expect(spyError).not.toHaveBeenCalled();
});
});

View File

@@ -11,14 +11,17 @@ import { randomString } from ":/utils";
import { Field, FieldProps } from ":/components/Forms/Field";
import { LabelledBox } from ":/components/Forms/LabelledBox";
export type InputOnlyProps = {
label?: string;
icon?: ReactNode;
rightIcon?: ReactNode;
charCounter?: boolean;
charCounterMax?: number;
};
export type InputProps = InputHTMLAttributes<HTMLInputElement> &
FieldProps & {
label?: string;
icon?: ReactNode;
rightIcon?: ReactNode;
charCounter?: boolean;
charCounterMax?: number;
};
FieldProps &
InputOnlyProps;
export const Input = forwardRef<HTMLInputElement, InputProps>(
(
@@ -68,6 +71,16 @@ export const Input = forwardRef<HTMLInputElement, InputProps>(
setValue(props.value || "");
}, [props.value]);
const {
compact,
fullWidth,
rightText,
state,
text,
textItems,
...inputProps
} = props;
return (
<Field {...props} rightText={rightTextToUse}>
{/* We disabled linting for this specific line because we consider that the onClick props is only used for */}
@@ -76,7 +89,7 @@ export const Input = forwardRef<HTMLInputElement, InputProps>(
<div
className={classNames(
"c__input__wrapper",
"c__input__wrapper--" + props.state,
props.state && "c__input__wrapper--" + props.state,
{
"c__input__wrapper--disabled": props.disabled,
},
@@ -95,7 +108,7 @@ export const Input = forwardRef<HTMLInputElement, InputProps>(
<input
type="text"
className={classes.join(" ")}
{...props}
{...inputProps}
id={idToUse.current}
value={value}
onFocus={(e) => {