Skip to content

Commit 1ea6d5e

Browse files
authored
fix(ui5-step-input, ui5-switch): support label 'for' attribute in aria-label (#13057)
Adds support for 'for' attribute to `ui5-switch` and `ui5-step-input`. Usage ``` <ui5-label for="switch">Enable notifications</ui5-label> <ui5-switch id="switch"></ui5-switch> ``` This will set the aria-label of the `ui5-switch` to "Enable notifications", which can be read by screen readers. Similarly, you can do this for the `ui5-step-input`. Related to: #10710
1 parent 495e5e8 commit 1ea6d5e

6 files changed

Lines changed: 64 additions & 4 deletions

File tree

packages/main/cypress/specs/StepInput.cy.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import StepInput from "../../src/StepInput.js";
2+
import Label from "../../src/Label.js";
23
import { setLanguage } from "@ui5/webcomponents-base/dist/config/Language.js";
34
import "../../src/Assets.js";
45

@@ -950,4 +951,24 @@ describe("Validation inside form", () => {
950951
cy.get("#stepInput:invalid")
951952
.should("not.exist", "StepInput with value lower than max should not have :invalid CSS class");
952953
});
954+
});
955+
956+
describe("Accessibility", () => {
957+
it("should have correct aria-label when associated with a label via 'for' attribute", () => {
958+
const labelText = "Quantity";
959+
960+
cy.mount(
961+
<>
962+
<Label for="stepInput">{labelText}</Label>
963+
<StepInput id="stepInput"></StepInput>
964+
</>
965+
);
966+
967+
cy.get("[ui5-step-input]")
968+
.shadow()
969+
.find("[ui5-input]")
970+
.shadow()
971+
.find("input")
972+
.should("have.attr", "aria-label", labelText);
973+
});
953974
});

packages/main/cypress/specs/Switch.cy.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,3 +227,21 @@ describe("General interactions in form", () => {
227227
});
228228

229229
});
230+
231+
describe("Accessibility", () => {
232+
it("should have correct aria-label when associated with a label via 'for' attribute", () => {
233+
const labelText = "Enable notifications";
234+
235+
cy.mount(
236+
<>
237+
<Label for="switch">{labelText}</Label>
238+
<Switch id="switch"></Switch>
239+
</>
240+
);
241+
242+
cy.get("[ui5-switch]")
243+
.shadow()
244+
.find(".ui5-switch-root")
245+
.should("have.attr", "aria-label", labelText);
246+
});
247+
});

packages/main/src/StepInput.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {
2323
import i18n from "@ui5/webcomponents-base/dist/decorators/i18n.js";
2424
import type I18nBundle from "@ui5/webcomponents-base/dist/i18nBundle.js";
2525
import ValueState from "@ui5/webcomponents-base/dist/types/ValueState.js";
26-
import { getEffectiveAriaLabelText } from "@ui5/webcomponents-base/dist/util/AccessibilityTextsHelper.js";
26+
import { getEffectiveAriaLabelText, getAssociatedLabelForTexts } from "@ui5/webcomponents-base/dist/util/AccessibilityTextsHelper.js";
2727
import type { Timeout } from "@ui5/webcomponents-base/dist/types.js";
2828
import type { IFormInputElement } from "@ui5/webcomponents-base/dist/features/InputElementsFormSupport.js";
2929
import { submitForm } from "@ui5/webcomponents-base/dist/features/InputElementsFormSupport.js";
@@ -391,7 +391,7 @@ class StepInput extends UI5Element implements IFormInputElement {
391391
get accInfo(): InputAccInfo {
392392
return {
393393
"ariaRequired": this.required,
394-
"ariaLabel": getEffectiveAriaLabelText(this),
394+
"ariaLabel": getEffectiveAriaLabelText(this) || getAssociatedLabelForTexts(this),
395395
};
396396
}
397397

packages/main/src/Switch.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
} from "@ui5/webcomponents-base/dist/Keys.js";
1111
import i18n from "@ui5/webcomponents-base/dist/decorators/i18n.js";
1212
import type I18nBundle from "@ui5/webcomponents-base/dist/i18nBundle.js";
13-
import { getEffectiveAriaLabelText } from "@ui5/webcomponents-base/dist/util/AccessibilityTextsHelper.js";
13+
import { getEffectiveAriaLabelText, getAssociatedLabelForTexts } from "@ui5/webcomponents-base/dist/util/AccessibilityTextsHelper.js";
1414
import "@ui5/webcomponents-icons/dist/accept.js";
1515
import "@ui5/webcomponents-icons/dist/decline.js";
1616
import "@ui5/webcomponents-icons/dist/less.js";
@@ -320,7 +320,7 @@ class Switch extends UI5Element implements IFormInputElement {
320320
}
321321

322322
get ariaLabelText() {
323-
return [getEffectiveAriaLabelText(this), this.hiddenText].join(" ").trim();
323+
return [getEffectiveAriaLabelText(this) || getAssociatedLabelForTexts(this), this.hiddenText].join(" ").trim();
324324
}
325325
}
326326

packages/main/test/pages/StepInput.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,18 @@ <h3>Form validation</h3>
191191
</form>
192192
</section>
193193

194+
195+
<h3>Accessibility</h3>
196+
<section>
197+
<ui5-label for="stepInputLabel">In Cart:</ui5-label>
198+
<ui5-step-input id="stepInputLabel"
199+
value="3"
200+
min="0"
201+
max="10"
202+
step="1"
203+
></ui5-step-input>
204+
</section>
205+
194206
<script>
195207
const labelChange = document.getElementById('myLabelChange');
196208
const siCozy = document.getElementById('stepInputCozy');

packages/main/test/pages/Switch.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,15 @@ <h3>sap_horizon</h3>
108108
<ui5-switch></ui5-switch>
109109
<ui5-switch checked></ui5-switch>
110110
</div>
111+
112+
<h3>Accessibility</h3>
113+
<section>
114+
<ui5-label for="switchLabel">Do you Agree?</ui5-label>
115+
<ui5-switch id="switchLabel"
116+
text-on="Yes"
117+
text-off="No"
118+
></ui5-switch>
119+
</section>
111120
</body>
112121

113122
<script>

0 commit comments

Comments
 (0)