Skip to content

Commit 2356cd0

Browse files
authored
feat(ui5-input): Support ariaLabelledBy (#1873)
1 parent 16b72be commit 2356cd0

File tree

4 files changed

+52
-25
lines changed

4 files changed

+52
-25
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import findNodeOwner from "./findNodeOwner.js";
2+
3+
const getEffectiveAriaLabelText = el => {
4+
if (!el.ariaLabelledby) {
5+
if (el.ariaLabel) {
6+
return el.ariaLabel;
7+
}
8+
9+
return undefined;
10+
}
11+
12+
const ids = el.ariaLabelledby.split(" ");
13+
const owner = findNodeOwner(el);
14+
let result = "";
15+
16+
ids.forEach((elementId, index) => {
17+
const element = owner.querySelector(`[id='${elementId}']`);
18+
result += `${element ? element.textContent : ""}`;
19+
20+
if (index < ids.length - 1) {
21+
result += " ";
22+
}
23+
});
24+
25+
return result;
26+
};
27+
28+
export default getEffectiveAriaLabelText;

packages/main/src/Button.js

+2-23
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import litRender from "@ui5/webcomponents-base/dist/renderer/LitRenderer.js";
33
import { isSpace, isEnter } from "@ui5/webcomponents-base/dist/Keys.js";
44
import { getFeature } from "@ui5/webcomponents-base/dist/FeaturesRegistry.js";
55
import { fetchI18nBundle, getI18nBundle } from "@ui5/webcomponents-base/dist/i18nBundle.js";
6-
import findNodeOwner from "@ui5/webcomponents-base/dist/util/findNodeOwner.js";
6+
import getEffectiveAriaLabelText from "@ui5/webcomponents-base/dist/util/getEffectiveAriaLabelText.js";
77
import ButtonDesign from "./types/ButtonDesign.js";
88
import ButtonTemplate from "./generated/templates/ButtonTemplate.lit.js";
99
import Icon from "./Icon.js";
@@ -358,28 +358,7 @@ class Button extends UI5Element {
358358
}
359359

360360
get ariaLabelText() {
361-
if (!this.ariaLabelledby) {
362-
if (this.ariaLabel) {
363-
return this.ariaLabel;
364-
}
365-
366-
return undefined;
367-
}
368-
369-
const ids = this.ariaLabelledby.split(" ");
370-
const owner = findNodeOwner(this);
371-
let result = "";
372-
373-
ids.forEach((elementId, index) => {
374-
const element = owner.querySelector(`[id='${elementId}']`);
375-
result += `${element ? element.textContent : ""}`;
376-
377-
if (index < ids.length - 1) {
378-
result += " ";
379-
}
380-
});
381-
382-
return result;
361+
return getEffectiveAriaLabelText(this);
383362
}
384363

385364
static typeTextMappings() {

packages/main/src/Input.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
} from "@ui5/webcomponents-base/dist/Keys.js";
1414
import Integer from "@ui5/webcomponents-base/dist/types/Integer.js";
1515
import { fetchI18nBundle, getI18nBundle } from "@ui5/webcomponents-base/dist/i18nBundle.js";
16+
import getEffectiveAriaLabelText from "@ui5/webcomponents-base/dist/util/getEffectiveAriaLabelText.js";
1617
import "@ui5/webcomponents-icons/dist/icons/decline.js";
1718
import InputType from "./types/InputType.js";
1819
import Popover from "./Popover.js";
@@ -280,6 +281,19 @@ const metadata = {
280281
type: String,
281282
},
282283

284+
/**
285+
* Receives id(or many ids) of the elements that label the input
286+
*
287+
* @type {String}
288+
* @defaultvalue ""
289+
* @private
290+
* @since 1.0.0-rc.8
291+
*/
292+
ariaLabelledby: {
293+
type: String,
294+
defaultValue: "",
295+
},
296+
283297
/**
284298
* @private
285299
*/
@@ -939,7 +953,7 @@ class Input extends UI5Element {
939953
"ariaOwns": this._inputAccInfo && this._inputAccInfo.ariaOwns,
940954
"ariaExpanded": this._inputAccInfo && this._inputAccInfo.ariaExpanded,
941955
"ariaDescription": this._inputAccInfo && this._inputAccInfo.ariaDescription,
942-
"ariaLabel": this.ariaLabel,
956+
"ariaLabel": getEffectiveAriaLabelText(this),
943957
},
944958
};
945959
}

packages/main/test/pages/Input.html

+7-1
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,12 @@ <h3> Test scroll pos</h3>
289289
<br><br>
290290
<ui5-input id="scrollResult"></ui5-input>
291291

292+
<h3>Test ariaLabel and ariaLabelledBy</h3>
293+
<ui5-input aria-label="Very important input"></ui5-input>
294+
<br>
295+
<ui5-label id="enterNameLabel">Enter name: </ui5-label>
296+
<ui5-input aria-labelledby="enterNameLabel"></ui5-input>
297+
292298
<script>
293299
var sap_database_entries = [{ key: "A", text: "A" }, { key: "Afg", text: "Afghanistan" }, { key: "Arg", text: "Argentina" }, { key: "Alb", text: "Albania" }, { key: "Arm", text: "Armenia" }, { key: "Alg", text: "Algeria" }, { key: "And", text: "Andorra" }, { key: "Ang", text: "Angola" }, { key: "Ast", text: "Austria" }, { key: "Aus", text: "Australia" }, { key: "Aze", text: "Azerbaijan" }, { key: "Aruba", text: "Aruba" }, { key: "Antigua", text: "Antigua and Barbuda" }, { key: "B", text: "B" }, { key: "Bel", text: "Belarus" }, { key: "Bel", text: "Belgium" }, { key: "Bg", text: "Bulgaria" }, { key: "Bra", text: "Brazil" }, { key: "C", text: "C" }, { key: "Ch", text: "China" }, { key: "Cub", text: "Cuba" }, { key: "Chil", text: "Chili" }, { key: "L", text: "L" }, { key: "Lat", text: "Latvia" }, { key: "Lit", text: "Litva" }, { key: "P", text: "P" }, { key: "Prt", text: "Portugal" }, { key: "S", text: "S" }, { key: "Sen", text: "Senegal" }, { key: "Ser", text: "Serbia" }, { key: "Sey", text: "Seychelles" }, { key: "Sierra", text: "Sierra Leone" }, { key: "Sgp", text: "Singapore" }, { key: "Sint", text: "Sint Maarten" }, { key: "Slv", text: "Slovakia" }, { key: "Slo", text: "Slovenia" }];
294300

@@ -415,7 +421,7 @@ <h3> Test scroll pos</h3>
415421
mouseoverResult.value = targetRef.textContent;
416422
quickViewCard.openBy(targetRef, true);
417423
});
418-
424+
419425
el.addEventListener("mouseout", function (event) {
420426
const targetRef = event.detail.targetRef;
421427

0 commit comments

Comments
 (0)