From c7000f37d2846f9487b05962c6a9fcb69f2b632c Mon Sep 17 00:00:00 2001 From: Anthony Le Courric Date: Mon, 11 Sep 2023 11:40:15 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B(react)=20fix=20props=20not=20recog?= =?UTF-8?q?nized=20on=20the=20input=20element?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The props propagation was adding unknown props to the input element, which was causing an error warning to be displayed in the console. --- .changeset/red-dragons-yell.md | 5 +++ .../components/Forms/Checkbox/index.spec.tsx | 30 +++++++++++++++++- .../src/components/Forms/Checkbox/index.tsx | 23 +++++++++++--- .../src/components/Forms/Input/index.spec.tsx | 29 ++++++++++++++++- .../src/components/Forms/Input/index.tsx | 31 +++++++++++++------ .../src/components/Forms/Radio/index.spec.tsx | 29 ++++++++++++++++- .../src/components/Forms/Radio/index.tsx | 21 ++++++++++--- .../components/Forms/Switch/index.spec.tsx | 26 +++++++++++++++- .../src/components/Forms/Switch/index.tsx | 23 +++++++++++--- 9 files changed, 190 insertions(+), 27 deletions(-) create mode 100644 .changeset/red-dragons-yell.md diff --git a/.changeset/red-dragons-yell.md b/.changeset/red-dragons-yell.md new file mode 100644 index 0000000..41840e5 --- /dev/null +++ b/.changeset/red-dragons-yell.md @@ -0,0 +1,5 @@ +--- +"@openfun/cunningham-react": minor +--- + +fix props not recognized on the input element diff --git a/packages/react/src/components/Forms/Checkbox/index.spec.tsx b/packages/react/src/components/Forms/Checkbox/index.spec.tsx index 2bbffae..a4fad2b 100644 --- a/packages/react/src/components/Forms/Checkbox/index.spec.tsx +++ b/packages/react/src/components/Forms/Checkbox/index.spec.tsx @@ -1,9 +1,20 @@ import userEvent from "@testing-library/user-event"; import React from "react"; import { render, screen } from "@testing-library/react"; -import { Checkbox, CheckboxGroup } from ":/components/Forms/Checkbox/index"; +import { FieldProps } from "../Field"; +import { + Checkbox, + CheckboxGroup, + CheckboxOnlyProps, +} from ":/components/Forms/Checkbox/index"; + +const spyError = vi.spyOn(global.console, "error"); describe("", () => { + afterAll(() => { + spyError.mockRestore(); + }); + it("renders and can be checked", async () => { const user = userEvent.setup(); render(); @@ -106,4 +117,21 @@ describe("", () => { document.querySelector(".c__checkbox__group.c__field.c__field--error"), ).toBeInTheDocument(); }); + + it("checks the props doesn't create error warning", async () => { + const propsInput: Required = { + 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", + indeterminate: true, + }; + + render(); + expect(spyError).not.toHaveBeenCalled(); + }); }); diff --git a/packages/react/src/components/Forms/Checkbox/index.tsx b/packages/react/src/components/Forms/Checkbox/index.tsx index 0a5f117..c1b7c7e 100644 --- a/packages/react/src/components/Forms/Checkbox/index.tsx +++ b/packages/react/src/components/Forms/Checkbox/index.tsx @@ -9,11 +9,14 @@ import React, { import classNames from "classnames"; import { Field, FieldProps } from ":/components/Forms/Field"; +export type CheckboxOnlyProps = { + indeterminate?: boolean; + label?: string; +}; + export type CheckboxProps = InputHTMLAttributes & - FieldProps & { - indeterminate?: boolean; - label?: string; - }; + FieldProps & + CheckboxOnlyProps; export const Checkbox = forwardRef( ( @@ -33,6 +36,16 @@ export const Checkbox = forwardRef( } }, [indeterminate]); + const { + compact, + fullWidth, + rightText, + state, + text, + textItems, + ...inputProps + } = props; + return (