From 0ea84a6b7b1b921d95fb7027f501ca3fa885c93c Mon Sep 17 00:00:00 2001 From: Dmytro Klymenko Date: Mon, 7 Apr 2025 14:12:51 -0400 Subject: [PATCH 1/3] fix(input): prevent accessibility label duplication --- .../components/input/__tests__/input.test.tsx | 32 ++++++++++++++++++- packages/components/input/src/use-input.ts | 8 ++--- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/packages/components/input/__tests__/input.test.tsx b/packages/components/input/__tests__/input.test.tsx index 8493de8191..d9642699ff 100644 --- a/packages/components/input/__tests__/input.test.tsx +++ b/packages/components/input/__tests__/input.test.tsx @@ -86,7 +86,37 @@ describe("Input", () => { const labelId = container.querySelector("label")?.id; const labelledBy = container.querySelector("input")?.getAttribute("aria-labelledby"); - expect(labelledBy?.includes(labelId as string)).toBeTruthy(); + expect(labelledBy).toBe(labelId); + }); + + it("should be labelled by placeholder when no label is provided", () => { + const {getByRole} = render(); + + expect(getByRole("textbox", {name: "test input"})).toBeInTheDocument(); + }); + + it("should be labelled by aria-label when no label is provided", () => { + const {getByRole} = render(); + + expect(getByRole("textbox", {name: "test input"})).toBeInTheDocument(); + }); + + it("should be labelled by label when label is provided", () => { + const {getByRole} = render(); + + expect(getByRole("textbox", {name: "test input"})).toBeInTheDocument(); + }); + + it("should be labelled by label and aria-label when both label and aria-label are provided", () => { + const {getByRole} = render(); + + expect(getByRole("textbox", {name: "test input test input"})).toBeInTheDocument(); + }); + + it("should be labelled by label when both label and placeholder are provided", () => { + const {getByRole} = render(); + + expect(getByRole("textbox", {name: "test input"})).toBeInTheDocument(); }); it("should have the correct type attribute", () => { diff --git a/packages/components/input/src/use-input.ts b/packages/components/input/src/use-input.ts index 33f57abd0c..cefe5fcdb6 100644 --- a/packages/components/input/src/use-input.ts +++ b/packages/components/input/src/use-input.ts @@ -190,11 +190,9 @@ export function useInput Date: Mon, 7 Apr 2025 14:26:37 -0400 Subject: [PATCH 2/3] chore(chageset): add changeset --- .changeset/light-cups-melt.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/light-cups-melt.md diff --git a/.changeset/light-cups-melt.md b/.changeset/light-cups-melt.md new file mode 100644 index 0000000000..9e034f0105 --- /dev/null +++ b/.changeset/light-cups-melt.md @@ -0,0 +1,5 @@ +--- +"@heroui/input": patch +--- + +fix `Input` accessibility label duplication From 46680e0db3788b6cf1a849f9dcc55248595eb804 Mon Sep 17 00:00:00 2001 From: WK Date: Mon, 19 May 2025 22:11:48 +0800 Subject: [PATCH 3/3] chore(changeset): include issue number --- .changeset/light-cups-melt.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/light-cups-melt.md b/.changeset/light-cups-melt.md index 9e034f0105..c91924e40b 100644 --- a/.changeset/light-cups-melt.md +++ b/.changeset/light-cups-melt.md @@ -2,4 +2,4 @@ "@heroui/input": patch --- -fix `Input` accessibility label duplication +fix `Input` accessibility label duplication (#5150)