Skip to content
Merged
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
133 changes: 106 additions & 27 deletions packages/main/cypress/specs/Form.cy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,35 @@ describe("Accessibility", () => {
.should("have.attr", "aria-label", "basic form");
});

it("tests 'aria-label' via 'accessibleNameRef'", () => {
cy.mount(
<>
<span id="lbl">basic form</span>
<Form headerText="Form header text" accessibleNameRef="lbl">
<FormItem>
<Label>Name:</Label>
<Text>Red Point Stores</Text>
</FormItem>
<FormItem>
<Label>Twitter:</Label>
<Text>@sap</Text>
</FormItem>
<FormItem>
<Label>Name:</Label>
<Text>Red Point Stores</Text>
</FormItem>
</Form>
</>);

cy.get("[ui5-form]")
.as("form");

cy.get("@form")
.shadow()
.find(".ui5-form-root")
.should("have.attr", "aria-label", "basic form");
});

describe("FormGroup accessibility", () => {
it("tests 'aria-label' default", () => {
cy.mount(<Form>
Expand All @@ -866,7 +895,7 @@ describe("Accessibility", () => {
.should("have.attr", "aria-label", Form.i18nBundle.getText(FORM_GROUP_ACCESSIBLE_NAME, "1"));
});

it("tests 'aria-label' via accessible-name", () => {
it("tests 'aria-label', 'aria-labelledby' via 'accessibleName'", () => {
const EXPECTED_LABEL = "Custom group label";
cy.mount(<Form>
<FormGroup accessibleName={EXPECTED_LABEL}>
Expand All @@ -884,10 +913,38 @@ describe("Accessibility", () => {
.shadow()
.find(".ui5-form-group-layout")
.eq(0)
.should("have.attr", "aria-label", EXPECTED_LABEL);
// 'aria-label' is rendered in Shadow DOM when accessibleName or accessibleNameRef is set
.should("have.attr", "aria-label", EXPECTED_LABEL)
.should("not.have.attr", "aria-labelledby");
});

it("tests 'aria-label', 'aria-labelledby' via 'accessibleNameRef'", () => {
const EXPECTED_LABEL = "Custom group label";
cy.mount(<>
<span id="lbl">{EXPECTED_LABEL}</span>
<Form>
<FormGroup accessibleNameRef="lbl">
<FormItem>
<Label>Name:</Label>
<Text>Red Point Stores</Text>
</FormItem>
</FormGroup>
</Form>
</>);

cy.get("[ui5-form]")
.as("form");

cy.get("@form")
.shadow()
.find(".ui5-form-group-layout")
.eq(0)
// 'aria-label' is rendered in Shadow DOM when accessibleName or accessibleNameRef is set
.should("have.attr", "aria-label", EXPECTED_LABEL)
.should("not.have.attr", "aria-labelledby");
});

it("tests 'aria-labelledby' via header-text", () => {
it("tests 'aria-label', 'aria-labelledby' when 'headerText' present", () => {
cy.mount(<Form>
<FormGroup headerText="Custom header text">
<FormItem>
Expand All @@ -914,48 +971,70 @@ describe("Accessibility", () => {
.find(".ui5-form-group-heading [ui5-title]")
.invoke("attr", "id")
.should(id => {
// aria-labelledby is used pointing to the header title ID
expect(ariaLabelledBy).to.equal(id);
});
});

// no 'aria-label' when headerText is present, aria-labelledby is used instead
cy.get("@group")
.should("not.have.attr", "aria-label");
});

it("tests 'aria-label' via accessible-name and header-text", () => {
it("tests 'aria-label', 'aria-labelledby' via 'accessibleName' when 'headerText' present", () => {
const EXPECTED_LABEL = "Custom group header";
cy.mount(<Form>
<FormGroup headerText="Custom header text" accessibleName={EXPECTED_LABEL}>
<FormItem>
<Label>Name:</Label>
<Text>Red Point Stores</Text>
</FormItem>
</FormGroup>
</Form>);
cy.mount(
<Form>
<FormGroup headerText="Custom header text" accessibleName={EXPECTED_LABEL}>
<FormItem>
<Label>Name:</Label>
<Text>Red Point Stores</Text>
</FormItem>
</FormGroup>
</Form>
);

cy.get("[ui5-form]")
.as("form");

// accessibleName has higher priority than headerText
cy.get("@form")
.shadow()
.find(".ui5-form-group-layout")
.eq(0)
.as("group")
.invoke("attr", "aria-labelledby")
.then(ariaLabelledBy => {
cy.get("@form")
.shadow()
.find(".ui5-form-group")
.eq(0)
.find(".ui5-form-group-heading [ui5-title]")
.invoke("attr", "id")
.should(id => {
expect(ariaLabelledBy).to.equal(id);
});
});
// 'aria-label' is rendered in Shadow DOM when accessibleName or accessibleNameRef is set
.should("have.attr", "aria-label", EXPECTED_LABEL)
.should("not.have.attr", "aria-labelledby");
});

cy.get("@group")
.should("have.attr", "aria-label", EXPECTED_LABEL);
it("tests 'aria-label', 'aria-labelledby' via 'accessibleNameRef' when 'headerText' present", () => {
const EXPECTED_LABEL = "Custom group header";
cy.mount(
<>
<span id="lbl">{EXPECTED_LABEL}</span>
<Form>
<FormGroup headerText="Custom header text" accessibleNameRef="lbl">

<FormItem>
<Label>Name:</Label>
<Text>Red Point Stores</Text>
</FormItem>
</FormGroup>
</Form>
</>
);

cy.get("[ui5-form]")
.as("form");

// accessibleNameReg has higher priority than headerText
cy.get("@form")
.shadow()
.find(".ui5-form-group-layout")
.eq(0)
// 'aria-label' is rendered in Shadow DOM when accessibleName or accessibleNameRef is set
.should("have.attr", "aria-label", EXPECTED_LABEL)
.should("not.have.attr", "aria-labelledby");
});
});

Expand Down
16 changes: 13 additions & 3 deletions packages/main/src/Form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import slot from "@ui5/webcomponents-base/dist/decorators/slot.js";
import jsxRenderer from "@ui5/webcomponents-base/dist/renderer/JsxRenderer.js";
import type { AriaRole } from "@ui5/webcomponents-base";
import type I18nBundle from "@ui5/webcomponents-base/dist/i18nBundle.js";
import { getEffectiveAriaLabelText } from "@ui5/webcomponents-base/dist/util/AccessibilityTextsHelper.js";

// Template
import FormTemplate from "./FormTemplate.js";
Expand Down Expand Up @@ -229,6 +230,15 @@ class Form extends UI5Element {
@property()
accessibleName?: string;

/**
* Defines id (or many ids) of the element (or elements) that label the component.
* @default undefined
* @public
* @since 2.16.0
*/
@property()
accessibleNameRef?: string;

/**
* Defines the accessibility mode of the component in "edit" and "display" use-cases.
*
Expand Down Expand Up @@ -569,15 +579,15 @@ class Form extends UI5Element {
}

get effectiveAccessibleName() {
if (this.accessibleName) {
return this.accessibleName;
if (this.accessibleName || this.accessibleNameRef) {
return getEffectiveAriaLabelText(this);
}

return this.hasHeader ? undefined : Form.i18nBundle.getText(FORM_ACCESSIBLE_NAME);
}

get effectiveАccessibleNameRef(): string | undefined {
if (this.accessibleName) {
if (this.accessibleName || this.accessibleNameRef) {
return;
}

Expand Down
17 changes: 15 additions & 2 deletions packages/main/src/FormGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type I18nBundle from "@ui5/webcomponents-base/dist/i18nBundle.js";
import customElement from "@ui5/webcomponents-base/dist/decorators/customElement.js";
import property from "@ui5/webcomponents-base/dist/decorators/property.js";
import slot from "@ui5/webcomponents-base/dist/decorators/slot.js";
import { getEffectiveAriaLabelText } from "@ui5/webcomponents-base/dist/util/AccessibilityTextsHelper.js";

import type FormItem from "./FormItem.js";
import type { IFormItem } from "./Form.js";
Expand Down Expand Up @@ -81,6 +82,15 @@ class FormGroup extends UI5Element implements IFormItem {
@property()
accessibleName?: string;

/**
* Defines id (or many ids) of the element (or elements) that label the component.
* @default undefined
* @public
* @since 2.16.0
*/
@property()
accessibleNameRef?: string;

/**
* Defines the items of the component.
* @public
Expand Down Expand Up @@ -123,8 +133,8 @@ class FormGroup extends UI5Element implements IFormItem {
}

getEffectiveAccessibleName(index: number) {
if (this.accessibleName) {
return this.accessibleName;
if (this.accessibleName || this.accessibleNameRef) {
return getEffectiveAriaLabelText(this);
}

if (this.headerText) {
Expand All @@ -135,6 +145,9 @@ class FormGroup extends UI5Element implements IFormItem {
}

get effectiveAccessibleNameRef() {
if (this.accessibleName || this.accessibleNameRef) {
return undefined;
}
return this.headerText ? `${this._id}-group-header-text` : undefined;
}

Expand Down
Loading