Skip to content

fix(material/select): scroll to top on last option before option group #23147

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/material-experimental/mdc-select/select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2134,6 +2134,30 @@ describe('MDC-based MatSelect', () => {
expect(panel.scrollTop).toBe(520, 'Expected scroll to be at the 16th option.');
}));

it('should scroll to top when going to first option in top group', fakeAsync(() => {
fixture.destroy();
const groupFixture = TestBed.createComponent(SelectWithGroups);
groupFixture.detectChanges();
groupFixture.componentInstance.select.open();
groupFixture.detectChanges();
flush();

host = groupFixture.debugElement.query(By.css('mat-select'))!.nativeElement;
panel = overlayContainerElement.querySelector('.mat-mdc-select-panel')! as HTMLElement;

for (let i = 0; i < 5; i++) {
dispatchKeyboardEvent(host, 'keydown', DOWN_ARROW);
}

expect(panel.scrollTop).toBeGreaterThan(0);

for (let i = 0; i < 5; i++) {
dispatchKeyboardEvent(host, 'keydown', UP_ARROW);
}

expect(panel.scrollTop).toBe(0);
}));

});
});

Expand Down
21 changes: 15 additions & 6 deletions src/material-experimental/mdc-select/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
MatOption,
MAT_OPTGROUP,
MAT_OPTION_PARENT_COMPONENT,
_countGroupLabelsBeforeOption,
_getOptionScrollPosition,
} from '@angular/material-experimental/mdc-core';
import {CdkOverlayOrigin, ConnectedPosition} from '@angular/cdk/overlay';
Expand Down Expand Up @@ -163,14 +164,22 @@ export class MatSelect extends _MatSelectBase<MatSelectChange> implements OnInit

if (option) {
const panel: HTMLElement = this.panel.nativeElement;
const labelCount = _countGroupLabelsBeforeOption(index, this.options, this.optionGroups);
const element = option._getHostElement();

panel.scrollTop = _getOptionScrollPosition(
element.offsetTop,
element.offsetHeight,
panel.scrollTop,
panel.offsetHeight
);
if (index === 0 && labelCount === 1) {
// If we've got one group label before the option and we're at the top option,
// scroll the list to the top. This is better UX than scrolling the list to the
// top of the option, because it allows the user to read the top group's label.
panel.scrollTop = 0;
} else {
panel.scrollTop = _getOptionScrollPosition(
element.offsetTop,
element.offsetHeight,
panel.scrollTop,
panel.offsetHeight
);
}
}
}

Expand Down
24 changes: 24 additions & 0 deletions src/material/select/select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2177,6 +2177,30 @@ describe('MatSelect', () => {
expect(panel.scrollTop).toBe(512, 'Expected scroll to be at the 16th option.');
}));

it('should scroll to top when going to first option in top group', fakeAsync(() => {
fixture.destroy();
const groupFixture = TestBed.createComponent(SelectWithGroups);
groupFixture.detectChanges();
groupFixture.componentInstance.select.open();
groupFixture.detectChanges();
flush();

host = groupFixture.debugElement.query(By.css('mat-select'))!.nativeElement;
panel = overlayContainerElement.querySelector('.mat-select-panel')! as HTMLElement;

for (let i = 0; i < 5; i++) {
dispatchKeyboardEvent(host, 'keydown', DOWN_ARROW);
}

expect(panel.scrollTop).toBeGreaterThan(0);

for (let i = 0; i < 5; i++) {
dispatchKeyboardEvent(host, 'keydown', UP_ARROW);
}

expect(panel.scrollTop).toBe(0);
}));

});
});

Expand Down
19 changes: 13 additions & 6 deletions src/material/select/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1204,12 +1204,19 @@ export class MatSelect extends _MatSelectBase<MatSelectChange> implements OnInit
const labelCount = _countGroupLabelsBeforeOption(index, this.options, this.optionGroups);
const itemHeight = this._getItemHeight();

this.panel.nativeElement.scrollTop = _getOptionScrollPosition(
(index + labelCount) * itemHeight,
itemHeight,
this.panel.nativeElement.scrollTop,
SELECT_PANEL_MAX_HEIGHT
);
if (index === 0 && labelCount === 1) {
// If we've got one group label before the option and we're at the top option,
// scroll the list to the top. This is better UX than scrolling the list to the
// top of the option, because it allows the user to read the top group's label.
this.panel.nativeElement.scrollTop = 0;
} else {
this.panel.nativeElement.scrollTop = _getOptionScrollPosition(
(index + labelCount) * itemHeight,
itemHeight,
this.panel.nativeElement.scrollTop,
SELECT_PANEL_MAX_HEIGHT
);
}
}

protected _positioningSettled() {
Expand Down