Skip to content

Commit bc27fea

Browse files
crisbetojelbourn
authored andcommitted
fix(select): active item not being updated on click in multiple mode (#7808)
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 1a735bc commit bc27fea

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
@@ -3540,6 +3540,22 @@ describe('MatSelect', () => {
35403540
'Expected `multiple` to have been set on dynamically-added option.');
35413541
}));
35423542

3543+
it('should update the active item index on click', fakeAsync(() => {
3544+
trigger.click();
3545+
fixture.detectChanges();
3546+
flush();
3547+
3548+
expect(fixture.componentInstance.select._keyManager.activeItemIndex).toBe(0);
3549+
3550+
const options = overlayContainerElement.querySelectorAll('mat-option') as
3551+
NodeListOf<HTMLElement>;
3552+
3553+
options[2].click();
3554+
fixture.detectChanges();
3555+
3556+
expect(fixture.componentInstance.select._keyManager.activeItemIndex).toBe(2);
3557+
}));
3558+
35433559
});
35443560
});
35453561

src/lib/select/select.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,7 @@ export class MatSelect extends _MatSelectMixinBase implements AfterContentInit,
861861
this._selectionModel.toggle(option);
862862
this.stateChanges.next();
863863
wasSelected ? option.deselect() : option.select();
864+
this._keyManager.setActiveItem(this._getOptionIndex(option)!);
864865
this._sortValues();
865866
} else {
866867
this._clearSelection(option.value == null ? undefined : option);

0 commit comments

Comments
 (0)