Skip to content

Commit b28f7c4

Browse files
authored
fix(ui5-input): Announce selected item (#1578)
1 parent ba1ee78 commit b28f7c4

File tree

4 files changed

+53
-0
lines changed

4 files changed

+53
-0
lines changed

packages/main/src/Input.hbs

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040

4141
{{#if showSuggestions}}
4242
<span id="{{_id}}-suggestionsText" class="ui5-hidden-text">{{suggestionsText}}</span>
43+
<span id="{{_id}}-selectionText" class="ui5-hidden-text" aria-live="polite" role="status"></span>
4344
{{/if}}
4445

4546
{{#if accInfo.input.ariaDescription}}

packages/main/src/Input.js

+17
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,8 @@ class Input extends UI5Element {
623623
}
624624

625625
_afterClosePopover() {
626+
this._announceSelectedItem();
627+
626628
// close device's keyboard and prevent further typing
627629
if (isPhone()) {
628630
this.blur();
@@ -707,6 +709,7 @@ class Input extends UI5Element {
707709
previewSuggestion(item) {
708710
this.valueBeforeItemSelection = this.value;
709711
this.value = item.group ? "" : item.textContent;
712+
this._announceSelectedItem();
710713
}
711714

712715
async fireEventByAction(action) {
@@ -798,6 +801,16 @@ class Input extends UI5Element {
798801
};
799802
}
800803

804+
_announceSelectedItem() {
805+
const invisibleText = this.shadowRoot.querySelector(`#${this._id}-selectionText`);
806+
807+
if (this.Suggestions && this.Suggestions._isItemOnTarget()) {
808+
invisibleText.textContent = this.itemSelectionAnnounce;
809+
} else {
810+
invisibleText.textContent = "";
811+
}
812+
}
813+
801814
get _readonly() {
802815
return this.readonly && !this.disabled;
803816
}
@@ -837,6 +850,10 @@ class Input extends UI5Element {
837850
};
838851
}
839852

853+
get itemSelectionAnnounce() {
854+
return this.Suggestions ? this.Suggestions.itemSelectionAnnounce : undefined;
855+
}
856+
840857
get classes() {
841858
return {
842859
popoverValueState: {

packages/main/src/features/InputSuggestions.js

+29
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import { registerFeature } from "@ui5/webcomponents-base/dist/FeaturesRegistry.js";
2+
import { getI18nBundle } from "@ui5/webcomponents-base/dist/i18nBundle.js";
23

34
import List from "../List.js";
45
import ResponsivePopover from "../ResponsivePopover.js";
56
import "../SuggestionItem.js";
67

8+
import {
9+
LIST_ITEM_POSITION,
10+
LIST_ITEM_SELECTED,
11+
} from "../generated/i18n/i18n-defaults.js";
712
/**
813
* A class to manage the <code>Input</code suggestion items.
914
*
@@ -29,6 +34,10 @@ class Suggestions {
2934
// An integer value to store the currently selected item position,
3035
// that changes due to user interaction.
3136
this.selectedItemIndex = null;
37+
38+
this.i18nBundle = getI18nBundle("@ui5/webcomponents");
39+
40+
this.accInfo = {};
3241
}
3342

3443
/* Public methods */
@@ -115,6 +124,12 @@ class Suggestions {
115124

116125
this.selectedItemIndex = this._getItems().indexOf(item);
117126

127+
this.accInfo = {
128+
currentPos: this.selectedItemIndex + 1,
129+
listSize: this._getItems().length,
130+
itemText: item.textContent,
131+
};
132+
118133
this._getComponent().onItemSelected(this._getRealItems()[this.selectedItemIndex], keyboardUsed);
119134
item.selected = false;
120135
this.close();
@@ -220,6 +235,12 @@ class Suggestions {
220235
const currentItem = items[nextIdx];
221236
const previousItem = items[previousIdx];
222237

238+
this.accInfo = {
239+
currentPos: nextIdx + 1,
240+
listSize: items.length,
241+
itemText: currentItem.textContent,
242+
};
243+
223244
if (previousItem) {
224245
previousItem.selected = false;
225246
}
@@ -292,6 +313,14 @@ class Suggestions {
292313
this.responsivePopover = staticAreaItem.querySelector("ui5-responsive-popover");
293314
return this.responsivePopover;
294315
}
316+
317+
get itemSelectionAnnounce() {
318+
const i18nBundle = this.i18nBundle,
319+
itemPositionText = i18nBundle.getText(LIST_ITEM_POSITION, [this.accInfo.currentPos], [this.accInfo.listSize]),
320+
itemSelectionText = i18nBundle.getText(LIST_ITEM_SELECTED);
321+
322+
return `${itemPositionText} ${this.accInfo.itemText} ${itemSelectionText}`;
323+
}
295324
}
296325

297326
Suggestions.SCROLL_STEP = 48;

packages/main/src/i18n/messagebundle.properties

+6
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,12 @@ LINK_SUBTLE=Subtle
247247
#XFLD: Emphasized link description label
248248
LINK_EMPHASIZED=Emphasized
249249

250+
#XACT: ARIA announcement for the position of the list items in an entire list
251+
LIST_ITEM_POSITION=List item {0} of {1}
252+
253+
#XACT: ARIA announcement for the list item selection.
254+
LIST_ITEM_SELECTED=Is Selected
255+
250256
#XTOL: Tooltip of messgae strip close button
251257
MESSAGE_STRIP_CLOSE_BUTTON=Message Strip Close
252258

0 commit comments

Comments
 (0)