Skip to content

Commit

Permalink
fix(material-experimental/mdc-chips): Mirror aria-describedby to matC…
Browse files Browse the repository at this point in the history
…hipInput

Updates mat-chip-grid to associate any ids set for aria-describedby to
the matChipInput instance within the grid, if one exists.

Fixes #24542
  • Loading branch information
ByzantineFailure committed Mar 15, 2022
1 parent 14f5b6e commit 760ba6d
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/material-experimental/mdc-chips/chip-grid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,10 @@ export class MatChipGrid
/** Associates an HTML input element with this chip grid. */
registerInput(inputElement: MatChipTextControl): void {
this._chipInput = inputElement;

if (this._ariaDescribedby) {
this._chipInput.setDescribedByIds(this._ariaDescribedby.split(' '));
}
}

/**
Expand Down Expand Up @@ -379,6 +383,10 @@ export class MatChipGrid
*/
setDescribedByIds(ids: string[]) {
this._ariaDescribedby = ids.join(' ');

if (this._chipInput) {
this._chipInput.setDescribedByIds(ids);
}
}

/**
Expand Down
18 changes: 18 additions & 0 deletions src/material-experimental/mdc-chips/chip-input.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,24 @@ describe('MDC-based MatChipInput', () => {
dispatchKeyboardEvent(inputNativeElement, 'keydown', ENTER, undefined, {shift: true});
expect(testChipInput.add).not.toHaveBeenCalled();
});

it('should set aria-describedby correctly when a non-empty list of ids is passed to setDescribedByIds', () => {
const ids = ['a', 'b', 'c'];

testChipInput.chipGridInstance.setDescribedByIds(ids);
fixture.detectChanges();

expect(inputNativeElement.getAttribute('aria-describedby')).toEqual('a b c');
});

it('should set aria-describedby correctly when an empty list of ids is passed to setDescribedByIds', () => {
const ids: string[] = [];

testChipInput.chipGridInstance.setDescribedByIds(ids);
fixture.detectChanges();

expect(inputNativeElement.getAttribute('aria-describedby')).toBeNull();
});
});
});

Expand Down
8 changes: 8 additions & 0 deletions src/material-experimental/mdc-chips/chip-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ let nextUniqueId = 0;
'[attr.disabled]': 'disabled || null',
'[attr.placeholder]': 'placeholder || null',
'[attr.aria-invalid]': '_chipGrid && _chipGrid.ngControl ? _chipGrid.ngControl.invalid : null',
'[attr.aria-describedby]': '_ariaDescribedby || null',
'[attr.aria-required]': '_chipGrid && _chipGrid.required || null',
'[attr.required]': '_chipGrid && _chipGrid.required || null',
},
Expand All @@ -73,6 +74,9 @@ export class MatChipInput implements MatChipTextControl, AfterContentInit, OnCha
/** Used to prevent focus moving to chips while user is holding backspace */
private _focusLastChipOnBackspace: boolean;

/** Value for ariaDescribedby property */
_ariaDescribedby?: string;

/** Whether the control is focused. */
focused: boolean = false;
_chipGrid: MatChipGrid;
Expand Down Expand Up @@ -240,6 +244,10 @@ export class MatChipInput implements MatChipTextControl, AfterContentInit, OnCha
this._focusLastChipOnBackspace = true;
}

setDescribedByIds(ids: string[]): void {
this._ariaDescribedby = ids.join(' ');
}

/** Checks whether a keycode is one of the configured separators. */
private _isSeparatorKey(event: KeyboardEvent) {
return !hasModifierKey(event) && new Set(this.separatorKeyCodes).has(event.keyCode);
Expand Down
3 changes: 3 additions & 0 deletions src/material-experimental/mdc-chips/chip-text-control.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,7 @@ export interface MatChipTextControl {

/** Focuses the text control. */
focus(): void;

/** Sets the list of ids the input is described by. */
setDescribedByIds(ids: string[]): void;
}

0 comments on commit 760ba6d

Please sign in to comment.