From c1d48fd69a89635b6d7dacc9bdc634417aa18a9a Mon Sep 17 00:00:00 2001 From: Nikola Anachkov Date: Wed, 16 Jul 2025 12:50:07 +0300 Subject: [PATCH] feat(ui5-checkbox): support associated labels with 'for' attribute --- packages/main/cypress/specs/CheckBox.cy.tsx | 19 +++++++++++++++++++ packages/main/src/CheckBox.ts | 4 ++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/packages/main/cypress/specs/CheckBox.cy.tsx b/packages/main/cypress/specs/CheckBox.cy.tsx index 950c2fd17c50..c4e92ace74c2 100644 --- a/packages/main/cypress/specs/CheckBox.cy.tsx +++ b/packages/main/cypress/specs/CheckBox.cy.tsx @@ -19,3 +19,22 @@ describe('CheckBox Component', () => { }); +describe("Accessibility", () => { + it("should announce the associated label when CheckBox is focused", () => { + cy.mount( + <> + + + + ); + + cy.get('label[for="cb"]') + .invoke('text') + .then((labelText) => { + cy.get("[ui5-checkbox]") + .shadow() + .find(".ui5-checkbox-root") + .should("have.attr", "aria-label", labelText); + }); + }); +}); \ No newline at end of file diff --git a/packages/main/src/CheckBox.ts b/packages/main/src/CheckBox.ts index e1d2fd81907a..6bc833a34321 100644 --- a/packages/main/src/CheckBox.ts +++ b/packages/main/src/CheckBox.ts @@ -7,7 +7,7 @@ import jsxRenderer from "@ui5/webcomponents-base/dist/renderer/JsxRenderer.js"; import i18n from "@ui5/webcomponents-base/dist/decorators/i18n.js"; import type I18nBundle from "@ui5/webcomponents-base/dist/i18nBundle.js"; import ValueState from "@ui5/webcomponents-base/dist/types/ValueState.js"; -import { getEffectiveAriaLabelText } from "@ui5/webcomponents-base/dist/util/AccessibilityTextsHelper.js"; +import { getEffectiveAriaLabelText, getAssociatedLabelForTexts } from "@ui5/webcomponents-base/dist/util/AccessibilityTextsHelper.js"; import { isSpace, isEnter } from "@ui5/webcomponents-base/dist/Keys.js"; import type { IFormInputElement } from "@ui5/webcomponents-base/dist/features/InputElementsFormSupport.js"; import type WrappingType from "./types/WrappingType.js"; @@ -378,7 +378,7 @@ class CheckBox extends UI5Element implements IFormInputElement { } get ariaLabelText() { - return getEffectiveAriaLabelText(this); + return getEffectiveAriaLabelText(this) || getAssociatedLabelForTexts(this); } get classes() {