🐛(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

@@ -1,9 +1,20 @@
import userEvent from "@testing-library/user-event";
import React from "react";
import { render, screen } from "@testing-library/react";
import { Radio, RadioGroup } from ":/components/Forms/Radio/index";
import { FieldProps } from "../Field";
import {
Radio,
RadioGroup,
RadioOnlyProps,
} from ":/components/Forms/Radio/index";
const spyError = vi.spyOn(global.console, "error");
describe("<Radio/>", () => {
afterAll(() => {
spyError.mockRestore();
});
it("should render", async () => {
render(<Radio label="Yes" />);
expect(screen.getByLabelText("Yes")).toBeInTheDocument();
@@ -111,4 +122,20 @@ describe("<Radio/>", () => {
document.querySelector(".c__radio__group.c__field.c__field--error"),
).toBeInTheDocument();
});
it("checks the props doesn't create error warning", async () => {
const propsInput: Required<FieldProps & RadioOnlyProps> = {
label: "First name",
fullWidth: true,
className: "c__field--full-width",
compact: false,
state: "default",
text: "my text",
textItems: ["my text item 1", "my text item 2"],
rightText: "my right text",
};
render(<Radio {...propsInput} />);
expect(spyError).not.toHaveBeenCalled();
});
});

View File

@@ -6,13 +6,26 @@ import React, {
import classNames from "classnames";
import { Field, FieldProps } from ":/components/Forms/Field";
export type RadioOnlyProps = {
label?: string;
};
export type RadioProps = InputHTMLAttributes<HTMLInputElement> &
FieldProps & {
label?: string;
};
FieldProps &
RadioOnlyProps;
export const Radio = forwardRef<HTMLInputElement, RadioProps>(
({ label, ...props }: RadioProps, ref) => {
const {
compact,
fullWidth,
rightText,
state,
text,
textItems,
...inputProps
} = props;
return (
<label
className={classNames("c__checkbox", "c__radio", {
@@ -22,7 +35,7 @@ export const Radio = forwardRef<HTMLInputElement, RadioProps>(
>
<Field compact={true} {...props}>
<div className="c__checkbox__container">
<input type="radio" {...props} ref={ref} />
<input type="radio" {...inputProps} ref={ref} />
{label && <div className="c__checkbox__label">{label}</div>}
</div>
</Field>