Skip to content

Commit 15892ff

Browse files
committed
fix(select): active item not being updated on click in multiple mode
Fixes the active option not being updated when the user clicks inside a multi-select, causing it to be stuck on the first option.
1 parent f95f832 commit 15892ff

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/lib/select/select.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3458,6 +3458,22 @@ describe('MatSelect', () => {
34583458
'Expected `multiple` to have been set on dynamically-added option.');
34593459
}));
34603460

3461+
it('should update the active item index on click', fakeAsync(() => {
3462+
trigger.click();
3463+
fixture.detectChanges();
3464+
flush();
3465+
3466+
expect(fixture.componentInstance.select._keyManager.activeItemIndex).toBe(0);
3467+
3468+
const options = overlayContainerElement.querySelectorAll('mat-option') as
3469+
NodeListOf<HTMLElement>;
3470+
3471+
options[2].click();
3472+
fixture.detectChanges();
3473+
3474+
expect(fixture.componentInstance.select._keyManager.activeItemIndex).toBe(2);
3475+
}));
3476+
34613477
});
34623478
});
34633479

src/lib/select/select.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,7 @@ export class MatSelect extends _MatSelectMixinBase implements AfterContentInit,
852852
this._selectionModel.toggle(option);
853853
this.stateChanges.next();
854854
wasSelected ? option.deselect() : option.select();
855+
this._keyManager.setActiveItem(this._getOptionIndex(option)!);
855856
this._sortValues();
856857
} else {
857858
this._clearSelection(option.value == null ? undefined : option);

0 commit comments

Comments
 (0)