Skip to content

Commit ce2487e

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

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

3421+
it('should update the active item index on click', fakeAsync(() => {
3422+
trigger.click();
3423+
fixture.detectChanges();
3424+
flush();
3425+
3426+
expect(fixture.componentInstance.select._keyManager.activeItemIndex).toBe(0);
3427+
3428+
const options = overlayContainerElement.querySelectorAll('mat-option') as
3429+
NodeListOf<HTMLElement>;
3430+
3431+
options[2].click();
3432+
fixture.detectChanges();
3433+
3434+
expect(fixture.componentInstance.select._keyManager.activeItemIndex).toBe(2);
3435+
}));
3436+
34213437
});
34223438
});
34233439

src/lib/select/select.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -858,6 +858,7 @@ export class MatSelect extends _MatSelectMixinBase implements AfterContentInit,
858858
this._selectionModel.toggle(option);
859859
this.stateChanges.next();
860860
wasSelected ? option.deselect() : option.select();
861+
this._keyManager.setActiveItem(this._getOptionIndex(option)!);
861862
this._sortValues();
862863
} else {
863864
this._clearSelection(option.value == null ? undefined : option);

0 commit comments

Comments
 (0)