From a17666d58cff74360963ed2eed768a4754f05722 Mon Sep 17 00:00:00 2001 From: Vanessa Schmitt Date: Mon, 19 Oct 2020 10:30:01 -0700 Subject: [PATCH] feat(material-experimental/mdc-chips): Allow custom role for ChipSet The default mat-chip-set role is 'presentation' if chips are present, and null if there are no chips. Some users want to specify custom roles, in particular 'list' (they would use 'listitem' for the chips, which have no default role). Currently this isn't possible, because mat-chip-set overwrites their custom role with 'presentation'. Add an Input to allow a custom role. --- .../mdc-chips/chip-set.spec.ts | 8 +++++++- src/material-experimental/mdc-chips/chip-set.ts | 14 +++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/material-experimental/mdc-chips/chip-set.spec.ts b/src/material-experimental/mdc-chips/chip-set.spec.ts index d01f8d08f293..eb294ad68170 100644 --- a/src/material-experimental/mdc-chips/chip-set.spec.ts +++ b/src/material-experimental/mdc-chips/chip-set.spec.ts @@ -67,9 +67,15 @@ describe('MDC-based MatChipSet', () => { expect(chips.toArray().every(chip => chip.disabled)).toBe(true); })); - it('should have role presentation', () => { + it('should have role presentation by default', () => { expect(chipSetNativeElement.getAttribute('role')).toBe('presentation'); }); + + it('should allow a custom role to be specified', () => { + chipSetInstance.role = 'list'; + fixture.detectChanges(); + expect(chipSetNativeElement.getAttribute('role')).toBe('list'); + }); }); }); diff --git a/src/material-experimental/mdc-chips/chip-set.ts b/src/material-experimental/mdc-chips/chip-set.ts index f82e307d0792..d88a1b349911 100644 --- a/src/material-experimental/mdc-chips/chip-set.ts +++ b/src/material-experimental/mdc-chips/chip-set.ts @@ -132,7 +132,19 @@ export class MatChipSet extends _MatChipSetMixinBase implements AfterContentInit get empty(): boolean { return this._chips.length === 0; } /** The ARIA role applied to the chip set. */ - get role(): string | null { return this.empty ? null : 'presentation'; } + @Input() + get role(): string | null { + if (this._role) { + return this._role; + } else { + return this.empty ? null : 'presentation'; + } + } + + set role(value: string | null) { + this._role = value; + } + private _role: string|null = null; /** Whether any of the chips inside of this chip-set has focus. */ get focused(): boolean { return this._hasFocusedChip(); }