Skip to content

Commit

Permalink
feat(ui5-select): add ariaLabel and ariaLabelledby properties (#2125)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ilhan007 committed Aug 20, 2020
1 parent c005478 commit a58bf49
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/main/src/Select.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
id="{{_id}}-select"
role="button"
aria-haspopup="listbox"
aria-label="{{ariaLabelText}}"
aria-labelledby="{{_id}}-label"
aria-describedby="{{valueStateTextId}}"
aria-disabled="{{isDisabled}}"
Expand Down
30 changes: 30 additions & 0 deletions packages/main/src/Select.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from "@ui5/webcomponents-base/dist/Keys.js";
import Integer from "@ui5/webcomponents-base/dist/types/Integer.js";
import { getFeature } from "@ui5/webcomponents-base/dist/FeaturesRegistry.js";
import getEffectiveAriaLabelText from "@ui5/webcomponents-base/dist/util/getEffectiveAriaLabelText.js";
import ValueState from "@ui5/webcomponents-base/dist/types/ValueState.js";
import "@ui5/webcomponents-icons/dist/icons/slim-arrow-down.js";
import { isPhone } from "@ui5/webcomponents-base/dist/Device.js";
Expand Down Expand Up @@ -157,6 +158,31 @@ const metadata = {
type: Boolean,
},

/**
* Defines the aria-label attribute for the select.
*
* @type {String}
* @since 1.0.0-rc.9
* @private
* @defaultvalue ""
*/
ariaLabel: {
type: String,
},

/**
* Receives id(or many ids) of the elements that label the select.
*
* @type {String}
* @defaultvalue ""
* @private
* @since 1.0.0-rc.9
*/
ariaLabelledby: {
type: String,
defaultValue: "",
},

_text: {
type: String,
noAttribute: true,
Expand Down Expand Up @@ -576,6 +602,10 @@ class Select extends UI5Element {
};
}

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

get valueStateMessageText() {
return this.getSlottedNodes("valueStateMessage").map(el => el.cloneNode(true));
}
Expand Down
18 changes: 18 additions & 0 deletions packages/main/test/pages/Select.html
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,24 @@ <h2>Selection not cycling</h2>
<h2> Change event counter holder</h2>
<ui5-input id="inputResult"></ui5-input>

<section>
<h3>Select aria-label and aria-labelledby</h3>
<span id="infoText">info text</span>
<div>
<ui5-select id="textAreaAriaLabel" aria-label="Hello World">
<ui5-option selected>First</ui5-option>
<ui5-option selected>Second</ui5-option>
<ui5-option selected>Third</ui5-option>
</ui5-select>

<ui5-select id="textAreaAriaLabelledBy" aria-labelledby="infoText">
<ui5-option selected>One</ui5-option>
<ui5-option selected>Two</ui5-option>
<ui5-option selected>Three</ui5-option>
</ui5-select>
</div>
</section>

<section class="ui5-content-density-compact">
<h3>Select in Compact</h3>
<div>
Expand Down
13 changes: 13 additions & 0 deletions packages/main/test/specs/Select.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,4 +293,17 @@ describe("Select general interaction", () => {

assert.ok(selectText.getHTML(false).indexOf(EXPECTED_SELECTION_TEXT2) !== -1, "Select label is correct.");
});


it("Tests aria-label and aria-labelledby", () => {
const select1 = browser.$("#textAreaAriaLabel").shadow$(".ui5-select-root");
const select2 = browser.$("#textAreaAriaLabelledBy").shadow$(".ui5-select-root");
const EXPECTED_ARIA_LABEL1 = "Hello World";
const EXPECTED_ARIA_LABEL2 = "info text";

assert.strictEqual(select1.getAttribute("aria-label"), EXPECTED_ARIA_LABEL1,
"The aria-label is correctly set internally.");
assert.strictEqual(select2.getAttribute("aria-label"), EXPECTED_ARIA_LABEL2,
"The aria-label is correctly set internally.");
});
});

0 comments on commit a58bf49

Please sign in to comment.