Skip to content

Commit 1069676

Browse files
feat(suggest): announce status of current item
1 parent 0560f5c commit 1069676

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

projects/angular/components/ui-suggest/src/ui-suggest.component.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,6 +1171,7 @@ const sharedSpecifications = (
11711171

11721172
expect(fixture.debugElement.query(By.css('.mat-chip.mat-standard-chip span'))).toBeFalsy();
11731173

1174+
flush();
11741175
discardPeriodicTasks();
11751176
}));
11761177

projects/angular/components/ui-suggest/src/ui-suggest.component.ts

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,6 +1056,8 @@ export class UiSuggestComponent extends UiSuggestMatFormFieldDirective
10561056
this._focusChipInput();
10571057
}
10581058
this._pushEntry(value);
1059+
1060+
this._announceSelectStatus(value.text, true);
10591061
}
10601062

10611063
const alreadySelectedNormalValue = this.multiple &&
@@ -1065,6 +1067,8 @@ export class UiSuggestComponent extends UiSuggestMatFormFieldDirective
10651067

10661068
if (alreadySelectedNormalValue) {
10671069
this._removeEntry(value);
1070+
1071+
this._announceSelectStatus(value.text, false);
10681072
}
10691073

10701074
if (closeAfterSelect) {
@@ -1248,14 +1252,26 @@ export class UiSuggestComponent extends UiSuggestMatFormFieldDirective
12481252
this._liveAnnouncer.announce(this.intl.noResultsLabel);
12491253
return;
12501254
}
1255+
1256+
const item = this.activeIndex < this.headerItems!.length
1257+
? this.headerItems![this.activeIndex]
1258+
: this.items[this.activeIndex - this.headerItems!.length];
1259+
1260+
const isCurrentItemSelected = !this._isOnCustomValueIndex
1261+
? this.isItemSelected(item)
1262+
: undefined;
1263+
12511264
const textToAnnounce = !this._isOnCustomValueIndex
1252-
? this.activeIndex < this.headerItems!.length
1253-
? this.headerItems![this.activeIndex].text
1254-
: this.items[this.activeIndex - this.headerItems!.length].text
1265+
? item.text
12551266
: `${this.intl.customValueLiveLabel} ${this.customValueLabelTranslator(this.inputControl.value)}`;
12561267

12571268
this._liveAnnouncer.announce(
1258-
this.intl.currentItemLabel(textToAnnounce, this.activeIndex + 1, this.headerItems!.length + this._items.length),
1269+
this.intl.currentItemLabel(
1270+
textToAnnounce,
1271+
this.activeIndex + 1,
1272+
this.headerItems!.length + this._items.length,
1273+
isCurrentItemSelected,
1274+
),
12591275
);
12601276
}
12611277

@@ -1434,4 +1450,10 @@ export class UiSuggestComponent extends UiSuggestMatFormFieldDirective
14341450
private _getValueSummary() {
14351451
return this.value.map(v => this.intl.translateLabel(v.text)).join(', ');
14361452
}
1453+
1454+
private _announceSelectStatus(text: string, status: boolean) {
1455+
if (text) {
1456+
this._liveAnnouncer.announce(this.intl.currentItemSelectionStatus(text, status));
1457+
}
1458+
}
14371459
}

projects/angular/components/ui-suggest/src/ui-suggest.intl.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ export class UiSuggestIntl implements OnDestroy {
5454
* @param itemNo The current item index.
5555
* @param itemCount The total item count.
5656
*/
57-
currentItemLabel = (text: string, itemNo: number, itemCount: number) => `${text} item ${itemNo} out of ${itemCount}`;
57+
currentItemLabel = (text: string, itemNo: number, itemCount: number, selectedStatus?: boolean) => `${text}${this._selectedStatusText(selectedStatus)} item ${itemNo} out of ${itemCount}`;
58+
59+
currentItemSelectionStatus = (text: string, isItemSelected: boolean) => `${text} item is ${isItemSelected ? 'selected' : 'removed from selection'}`;
5860

5961
/**
6062
* Custom value label generator function.
@@ -86,4 +88,12 @@ export class UiSuggestIntl implements OnDestroy {
8688
this._destroyed$.complete();
8789
this.changes.complete();
8890
}
91+
92+
private _selectedStatusText(selectedStatus?: boolean) {
93+
if (selectedStatus) {
94+
return ' (selected)';
95+
}
96+
97+
return '';
98+
}
8999
}

0 commit comments

Comments
 (0)