Skip to content

Commit a58bf49

Browse files
authored
feat(ui5-select): add ariaLabel and ariaLabelledby properties (#2125)
Similar to other components, the Select should also support aria-label and aria-labelledby set on the custom element. Note: internally we also set aria-label, using the base method "getEffectiveAriaLabelText" for the purpose. Related to #2107
1 parent c005478 commit a58bf49

File tree

4 files changed

+62
-0
lines changed

4 files changed

+62
-0
lines changed

packages/main/src/Select.hbs

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
id="{{_id}}-select"
66
role="button"
77
aria-haspopup="listbox"
8+
aria-label="{{ariaLabelText}}"
89
aria-labelledby="{{_id}}-label"
910
aria-describedby="{{valueStateTextId}}"
1011
aria-disabled="{{isDisabled}}"

packages/main/src/Select.js

+30
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
} from "@ui5/webcomponents-base/dist/Keys.js";
1313
import Integer from "@ui5/webcomponents-base/dist/types/Integer.js";
1414
import { getFeature } from "@ui5/webcomponents-base/dist/FeaturesRegistry.js";
15+
import getEffectiveAriaLabelText from "@ui5/webcomponents-base/dist/util/getEffectiveAriaLabelText.js";
1516
import ValueState from "@ui5/webcomponents-base/dist/types/ValueState.js";
1617
import "@ui5/webcomponents-icons/dist/icons/slim-arrow-down.js";
1718
import { isPhone } from "@ui5/webcomponents-base/dist/Device.js";
@@ -157,6 +158,31 @@ const metadata = {
157158
type: Boolean,
158159
},
159160

161+
/**
162+
* Defines the aria-label attribute for the select.
163+
*
164+
* @type {String}
165+
* @since 1.0.0-rc.9
166+
* @private
167+
* @defaultvalue ""
168+
*/
169+
ariaLabel: {
170+
type: String,
171+
},
172+
173+
/**
174+
* Receives id(or many ids) of the elements that label the select.
175+
*
176+
* @type {String}
177+
* @defaultvalue ""
178+
* @private
179+
* @since 1.0.0-rc.9
180+
*/
181+
ariaLabelledby: {
182+
type: String,
183+
defaultValue: "",
184+
},
185+
160186
_text: {
161187
type: String,
162188
noAttribute: true,
@@ -576,6 +602,10 @@ class Select extends UI5Element {
576602
};
577603
}
578604

605+
get ariaLabelText() {
606+
return getEffectiveAriaLabelText(this);
607+
}
608+
579609
get valueStateMessageText() {
580610
return this.getSlottedNodes("valueStateMessage").map(el => el.cloneNode(true));
581611
}

packages/main/test/pages/Select.html

+18
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,24 @@ <h2>Selection not cycling</h2>
7575
<h2> Change event counter holder</h2>
7676
<ui5-input id="inputResult"></ui5-input>
7777

78+
<section>
79+
<h3>Select aria-label and aria-labelledby</h3>
80+
<span id="infoText">info text</span>
81+
<div>
82+
<ui5-select id="textAreaAriaLabel" aria-label="Hello World">
83+
<ui5-option selected>First</ui5-option>
84+
<ui5-option selected>Second</ui5-option>
85+
<ui5-option selected>Third</ui5-option>
86+
</ui5-select>
87+
88+
<ui5-select id="textAreaAriaLabelledBy" aria-labelledby="infoText">
89+
<ui5-option selected>One</ui5-option>
90+
<ui5-option selected>Two</ui5-option>
91+
<ui5-option selected>Three</ui5-option>
92+
</ui5-select>
93+
</div>
94+
</section>
95+
7896
<section class="ui5-content-density-compact">
7997
<h3>Select in Compact</h3>
8098
<div>

packages/main/test/specs/Select.spec.js

+13
Original file line numberDiff line numberDiff line change
@@ -293,4 +293,17 @@ describe("Select general interaction", () => {
293293

294294
assert.ok(selectText.getHTML(false).indexOf(EXPECTED_SELECTION_TEXT2) !== -1, "Select label is correct.");
295295
});
296+
297+
298+
it("Tests aria-label and aria-labelledby", () => {
299+
const select1 = browser.$("#textAreaAriaLabel").shadow$(".ui5-select-root");
300+
const select2 = browser.$("#textAreaAriaLabelledBy").shadow$(".ui5-select-root");
301+
const EXPECTED_ARIA_LABEL1 = "Hello World";
302+
const EXPECTED_ARIA_LABEL2 = "info text";
303+
304+
assert.strictEqual(select1.getAttribute("aria-label"), EXPECTED_ARIA_LABEL1,
305+
"The aria-label is correctly set internally.");
306+
assert.strictEqual(select2.getAttribute("aria-label"), EXPECTED_ARIA_LABEL2,
307+
"The aria-label is correctly set internally.");
308+
});
296309
});

0 commit comments

Comments
 (0)