Skip to content

Commit 311d786

Browse files
crisbetovivian-hu-zz
authored andcommitted
fix(chips): set aria-invalid on chip input (#13249)
Currently we set `aria-invalid` automatically on inputs inside a form field, but not on one inside a chip list. These changes add `aria-invalid` to the chip input, when the related chip list is invalid. Fixes #13205.
1 parent 119fafd commit 311d786

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/lib/chips/chip-input.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ let nextUniqueId = 0;
4040
'[id]': 'id',
4141
'[attr.disabled]': 'disabled || null',
4242
'[attr.placeholder]': 'placeholder || null',
43+
'[attr.aria-invalid]': '_chipList && _chipList.ngControl ? _chipList.ngControl.invalid : null',
4344
}
4445
})
4546
export class MatChipInput implements OnChanges {

src/lib/chips/chip-list.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,6 +1040,20 @@ describe('MatChipList', () => {
10401040
expect(document.activeElement).toBe(nativeInput, 'Expected input to remain focused.');
10411041
}));
10421042

1043+
it('should set aria-invalid if the form field is invalid', () => {
1044+
fixture.componentInstance.control = new FormControl(undefined, [Validators.required]);
1045+
fixture.detectChanges();
1046+
1047+
const input: HTMLInputElement = fixture.nativeElement.querySelector('input');
1048+
1049+
expect(input.getAttribute('aria-invalid')).toBe('true');
1050+
1051+
fixture.componentInstance.chips.first.selectViaInteraction();
1052+
fixture.detectChanges();
1053+
1054+
expect(input.getAttribute('aria-invalid')).toBe('false');
1055+
});
1056+
10431057
describe('keyboard behavior', () => {
10441058
beforeEach(() => {
10451059
chipListDebugElement = fixture.debugElement.query(By.directive(MatChipList));

0 commit comments

Comments
 (0)