Skip to content

Commit 760ba6d

Browse files
fix(material-experimental/mdc-chips): Mirror aria-describedby to matChipInput
Updates mat-chip-grid to associate any ids set for aria-describedby to the matChipInput instance within the grid, if one exists. Fixes #24542
1 parent 14f5b6e commit 760ba6d

File tree

4 files changed

+37
-0
lines changed

4 files changed

+37
-0
lines changed

src/material-experimental/mdc-chips/chip-grid.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,10 @@ export class MatChipGrid
337337
/** Associates an HTML input element with this chip grid. */
338338
registerInput(inputElement: MatChipTextControl): void {
339339
this._chipInput = inputElement;
340+
341+
if (this._ariaDescribedby) {
342+
this._chipInput.setDescribedByIds(this._ariaDescribedby.split(' '));
343+
}
340344
}
341345

342346
/**
@@ -379,6 +383,10 @@ export class MatChipGrid
379383
*/
380384
setDescribedByIds(ids: string[]) {
381385
this._ariaDescribedby = ids.join(' ');
386+
387+
if (this._chipInput) {
388+
this._chipInput.setDescribedByIds(ids);
389+
}
382390
}
383391

384392
/**

src/material-experimental/mdc-chips/chip-input.spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,24 @@ describe('MDC-based MatChipInput', () => {
230230
dispatchKeyboardEvent(inputNativeElement, 'keydown', ENTER, undefined, {shift: true});
231231
expect(testChipInput.add).not.toHaveBeenCalled();
232232
});
233+
234+
it('should set aria-describedby correctly when a non-empty list of ids is passed to setDescribedByIds', () => {
235+
const ids = ['a', 'b', 'c'];
236+
237+
testChipInput.chipGridInstance.setDescribedByIds(ids);
238+
fixture.detectChanges();
239+
240+
expect(inputNativeElement.getAttribute('aria-describedby')).toEqual('a b c');
241+
});
242+
243+
it('should set aria-describedby correctly when an empty list of ids is passed to setDescribedByIds', () => {
244+
const ids: string[] = [];
245+
246+
testChipInput.chipGridInstance.setDescribedByIds(ids);
247+
fixture.detectChanges();
248+
249+
expect(inputNativeElement.getAttribute('aria-describedby')).toBeNull();
250+
});
233251
});
234252
});
235253

src/material-experimental/mdc-chips/chip-input.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ let nextUniqueId = 0;
6565
'[attr.disabled]': 'disabled || null',
6666
'[attr.placeholder]': 'placeholder || null',
6767
'[attr.aria-invalid]': '_chipGrid && _chipGrid.ngControl ? _chipGrid.ngControl.invalid : null',
68+
'[attr.aria-describedby]': '_ariaDescribedby || null',
6869
'[attr.aria-required]': '_chipGrid && _chipGrid.required || null',
6970
'[attr.required]': '_chipGrid && _chipGrid.required || null',
7071
},
@@ -73,6 +74,9 @@ export class MatChipInput implements MatChipTextControl, AfterContentInit, OnCha
7374
/** Used to prevent focus moving to chips while user is holding backspace */
7475
private _focusLastChipOnBackspace: boolean;
7576

77+
/** Value for ariaDescribedby property */
78+
_ariaDescribedby?: string;
79+
7680
/** Whether the control is focused. */
7781
focused: boolean = false;
7882
_chipGrid: MatChipGrid;
@@ -240,6 +244,10 @@ export class MatChipInput implements MatChipTextControl, AfterContentInit, OnCha
240244
this._focusLastChipOnBackspace = true;
241245
}
242246

247+
setDescribedByIds(ids: string[]): void {
248+
this._ariaDescribedby = ids.join(' ');
249+
}
250+
243251
/** Checks whether a keycode is one of the configured separators. */
244252
private _isSeparatorKey(event: KeyboardEvent) {
245253
return !hasModifierKey(event) && new Set(this.separatorKeyCodes).has(event.keyCode);

src/material-experimental/mdc-chips/chip-text-control.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,7 @@ export interface MatChipTextControl {
2222

2323
/** Focuses the text control. */
2424
focus(): void;
25+
26+
/** Sets the list of ids the input is described by. */
27+
setDescribedByIds(ids: string[]): void;
2528
}

0 commit comments

Comments
 (0)