Skip to content

fix(material/select): overlay offset calculation for disabled option centering #21716

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
Feb 1, 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
3 changes: 2 additions & 1 deletion scripts/check-mdc-tests-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ export const config = {
'should adjust for the group padding in ltr',
'should adjust for the group padding in rtl',
'should not adjust if all options are within a group, except the selected one',
'should align the first option to the trigger, if nothing is selected'
'should align the first option to the trigger, if nothing is selected',
'should not adjust if option centering is disabled any option under a group is selected'
],
'mdc-slide-toggle': [
// These tests are verifying implementation details that are not relevant for MDC.
Expand Down
19 changes: 19 additions & 0 deletions src/material/select/select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4225,6 +4225,25 @@ describe('MatSelect', () => {
.toBe(Math.floor(triggerTop), 'Expected trigger to align with the first option.');
}
}));

it('should not adjust if option centering is disabled any option under a group is selected',
fakeAsync(() => {
groupFixture.componentInstance.select.disableOptionCentering = true;
groupFixture.componentInstance.control.setValue('oddish-1');
groupFixture.detectChanges();

trigger.click();
groupFixture.detectChanges();
flush();

const selected = document.querySelector('.cdk-overlay-pane mat-option.mat-selected')!;
const selectedOptionLeft = selected.getBoundingClientRect().left;
const triggerLeft = trigger.getBoundingClientRect().left;

// 16px is the default option padding
expect(Math.floor(selectedOptionLeft)).toEqual(Math.floor(triggerLeft - 16));
})
);
});
});

Expand Down
2 changes: 2 additions & 0 deletions src/material/select/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1282,6 +1282,8 @@ export class MatSelect extends _MatSelectBase<MatSelectChange> implements OnInit
// Adjust the offset, depending on the option padding.
if (this.multiple) {
offsetX = SELECT_MULTIPLE_PANEL_PADDING_X;
} else if (this.disableOptionCentering) {
offsetX = SELECT_PANEL_PADDING_X;
} else {
let selected = this._selectionModel.selected[0] || this.options.first;
offsetX = selected && selected.group ? SELECT_PANEL_INDENT_PADDING_X : SELECT_PANEL_PADDING_X;
Expand Down