Skip to content

Commit

Permalink
feat(material/chips): update chip-list describedby to match input (#2…
Browse files Browse the repository at this point in the history
…4292)

Fixes #16209: ExpressionChangedAfterItHasBeenCheckedError that occurs when chip-lists are content-projected into a mat-form-field
  • Loading branch information
RobertAKARobin committed Feb 14, 2022
1 parent f868e33 commit 06e65c6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
16 changes: 11 additions & 5 deletions src/material/chips/chip-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ export class MatChipListChange {
exportAs: 'matChipList',
host: {
'[attr.tabindex]': 'disabled ? null : _tabIndex',
'[attr.aria-describedby]': '_ariaDescribedby || null',
'[attr.aria-required]': 'role ? required : null',
'[attr.aria-disabled]': 'disabled.toString()',
'[attr.aria-invalid]': 'errorState',
Expand Down Expand Up @@ -144,9 +143,6 @@ export class MatChipList
/** Uid of the chip list */
_uid: string = `mat-chip-list-${nextUniqueId++}`;

/** The aria-describedby attribute on the chip list for improved a11y. */
_ariaDescribedby: string;

/** Tab index for the chip list. */
_tabIndex = 0;

Expand Down Expand Up @@ -177,6 +173,12 @@ export class MatChipList
return this.empty ? null : 'listbox';
}

/**
* Implemented as part of MatFormFieldControl.
* @docs-private
*/
@Input('aria-describedby') userAriaDescribedBy: string;

/** An object used to control when error messages are shown. */
@Input() override errorStateMatcher: ErrorStateMatcher;

Expand Down Expand Up @@ -455,7 +457,11 @@ export class MatChipList
* @docs-private
*/
setDescribedByIds(ids: string[]) {
this._ariaDescribedby = ids.join(' ');
if (ids.length) {
this._elementRef.nativeElement.setAttribute('aria-describedby', ids.join(' '));
} else {
this._elementRef.nativeElement.removeAttribute('aria-describedby');
}
}

// Implemented as part of ControlValueAccessor.
Expand Down
4 changes: 2 additions & 2 deletions tools/public_api_guard/material/chips.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,6 @@ export interface MatChipInputEvent {
export class MatChipList extends _MatChipListBase implements MatFormFieldControl<any>, ControlValueAccessor, AfterContentInit, DoCheck, OnInit, OnDestroy, CanUpdateErrorState {
constructor(_elementRef: ElementRef<HTMLElement>, _changeDetectorRef: ChangeDetectorRef, _dir: Directionality, _parentForm: NgForm, _parentFormGroup: FormGroupDirective, _defaultErrorStateMatcher: ErrorStateMatcher, ngControl: NgControl);
_allowFocusEscape(): void;
_ariaDescribedby: string;
ariaOrientation: 'horizontal' | 'vertical';
_blur(): void;
readonly change: EventEmitter<MatChipListChange>;
Expand Down Expand Up @@ -255,6 +254,7 @@ export class MatChipList extends _MatChipListBase implements MatFormFieldControl
_uid: string;
protected _updateFocusForDestroyedChips(): void;
protected _updateTabIndex(): void;
userAriaDescribedBy: string;
_userTabIndex: number | null;
get value(): any;
set value(value: any);
Expand All @@ -264,7 +264,7 @@ export class MatChipList extends _MatChipListBase implements MatFormFieldControl
// (undocumented)
writeValue(value: any): void;
// (undocumented)
static ɵcmp: i0.ɵɵComponentDeclaration<MatChipList, "mat-chip-list", ["matChipList"], { "errorStateMatcher": "errorStateMatcher"; "multiple": "multiple"; "compareWith": "compareWith"; "value": "value"; "required": "required"; "placeholder": "placeholder"; "disabled": "disabled"; "ariaOrientation": "aria-orientation"; "selectable": "selectable"; "tabIndex": "tabIndex"; }, { "change": "change"; "valueChange": "valueChange"; }, ["chips"], ["*"]>;
static ɵcmp: i0.ɵɵComponentDeclaration<MatChipList, "mat-chip-list", ["matChipList"], { "userAriaDescribedBy": "aria-describedby"; "errorStateMatcher": "errorStateMatcher"; "multiple": "multiple"; "compareWith": "compareWith"; "value": "value"; "required": "required"; "placeholder": "placeholder"; "disabled": "disabled"; "ariaOrientation": "aria-orientation"; "selectable": "selectable"; "tabIndex": "tabIndex"; }, { "change": "change"; "valueChange": "valueChange"; }, ["chips"], ["*"]>;
// (undocumented)
static ɵfac: i0.ɵɵFactoryDeclaration<MatChipList, [null, null, { optional: true; }, { optional: true; }, { optional: true; }, null, { optional: true; self: true; }]>;
}
Expand Down

0 comments on commit 06e65c6

Please sign in to comment.