Skip to content

Commit da1f7e7

Browse files
authored
fix(material/select): overlay offset calculation for disabled option centering (#21716)
Fixes a the overlay offset calculation for grouped option values if disableOptionCentering is true. The overlay should not have double offset value for any selected option to keep it under the trigger element. Fixes #21570
1 parent dbb39a1 commit da1f7e7

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

scripts/check-mdc-tests-config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,8 @@ export const config = {
172172
'should adjust for the group padding in ltr',
173173
'should adjust for the group padding in rtl',
174174
'should not adjust if all options are within a group, except the selected one',
175-
'should align the first option to the trigger, if nothing is selected'
175+
'should align the first option to the trigger, if nothing is selected',
176+
'should not adjust if option centering is disabled any option under a group is selected'
176177
],
177178
'mdc-slide-toggle': [
178179
// These tests are verifying implementation details that are not relevant for MDC.

src/material/select/select.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4225,6 +4225,25 @@ describe('MatSelect', () => {
42254225
.toBe(Math.floor(triggerTop), 'Expected trigger to align with the first option.');
42264226
}
42274227
}));
4228+
4229+
it('should not adjust if option centering is disabled any option under a group is selected',
4230+
fakeAsync(() => {
4231+
groupFixture.componentInstance.select.disableOptionCentering = true;
4232+
groupFixture.componentInstance.control.setValue('oddish-1');
4233+
groupFixture.detectChanges();
4234+
4235+
trigger.click();
4236+
groupFixture.detectChanges();
4237+
flush();
4238+
4239+
const selected = document.querySelector('.cdk-overlay-pane mat-option.mat-selected')!;
4240+
const selectedOptionLeft = selected.getBoundingClientRect().left;
4241+
const triggerLeft = trigger.getBoundingClientRect().left;
4242+
4243+
// 16px is the default option padding
4244+
expect(Math.floor(selectedOptionLeft)).toEqual(Math.floor(triggerLeft - 16));
4245+
})
4246+
);
42284247
});
42294248
});
42304249

src/material/select/select.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,6 +1282,8 @@ export class MatSelect extends _MatSelectBase<MatSelectChange> implements OnInit
12821282
// Adjust the offset, depending on the option padding.
12831283
if (this.multiple) {
12841284
offsetX = SELECT_MULTIPLE_PANEL_PADDING_X;
1285+
} else if (this.disableOptionCentering) {
1286+
offsetX = SELECT_PANEL_PADDING_X;
12851287
} else {
12861288
let selected = this._selectionModel.selected[0] || this.options.first;
12871289
offsetX = selected && selected.group ? SELECT_PANEL_INDENT_PADDING_X : SELECT_PANEL_PADDING_X;

0 commit comments

Comments
 (0)