Skip to content

feat(ui5-checkbox): support associated labels with 'for' attribute #11925

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions packages/main/cypress/specs/CheckBox.cy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,22 @@ describe('CheckBox Component', () => {

});

describe("Accessibility", () => {
it("should announce the associated label when CheckBox is focused", () => {
cy.mount(
<>
<label for="cb">Should be the aria-label</label>
<CheckBox id="cb"></CheckBox>
</>
);

cy.get('label[for="cb"]')
.invoke('text')
.then((labelText) => {
cy.get("[ui5-checkbox]")
.shadow()
.find(".ui5-checkbox-root")
.should("have.attr", "aria-label", labelText);
});
});
});
4 changes: 2 additions & 2 deletions packages/main/src/CheckBox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -378,7 +378,7 @@ class CheckBox extends UI5Element implements IFormInputElement {
}

get ariaLabelText() {
return getEffectiveAriaLabelText(this);
return getEffectiveAriaLabelText(this) || getAssociatedLabelForTexts(this);
}

get classes() {
Expand Down
Loading