Skip to content

fix(select):change calculateOverlayOffsetY to address offset for small lists #9885

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 6 commits into from
Feb 18, 2018
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
64 changes: 64 additions & 0 deletions src/lib/select/select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2690,6 +2690,41 @@ describe('MatSelect', () => {

});

describe('with option centering disabled', () => {
beforeEach(async(() => configureMatSelectTestingModule([
SelectWithoutOptionCentering,
])));

let fixture: ComponentFixture<SelectWithoutOptionCentering>;
let trigger: HTMLElement;
let select: HTMLElement;
let formField: HTMLElement;

beforeEach(fakeAsync(() => {
fixture = TestBed.createComponent(SelectWithoutOptionCentering);
fixture.detectChanges();
trigger = fixture.debugElement.query(By.css('.mat-select-trigger')).nativeElement;
select = fixture.debugElement.query(By.css('mat-select')).nativeElement;
formField = fixture.debugElement.query(By.css('mat-form-field')).nativeElement;
}));

it('should not align the active option with the trigger if centering is disabled',
fakeAsync(() => {
trigger.click();
fixture.detectChanges();
flush();

const scrollContainer = document.querySelector('.cdk-overlay-pane .mat-select-panel')!;

// The panel should be scrolled to 0 because centering the option disabled.
expect(scrollContainer.scrollTop).toEqual(0, `Expected panel not to be scrolled.`);
// The trigger should contain 'Pizza' because it was preselected
expect(trigger.textContent).toContain('Pizza');
// The selected index should be 1 because it was preselected
expect(fixture.componentInstance.options.toArray()[1].selected).toBe(true);
}));
});

describe('positioning', () => {
beforeEach(async(() => configureMatSelectTestingModule([
BasicSelect,
Expand Down Expand Up @@ -4404,3 +4439,32 @@ class SingleSelectWithPreselectedArrayValues {
@ViewChild(MatSelect) select: MatSelect;
@ViewChildren(MatOption) options: QueryList<MatOption>;
}

@Component({
selector: 'select-without-option-centering',
template: `
<mat-form-field>
<mat-select placeholder="Food" [formControl]="control" disableOptionCentering>
<mat-option *ngFor="let food of foods" [value]="food.value">
{{ food.viewValue }}
</mat-option>
</mat-select>
</mat-form-field>
`
})
class SelectWithoutOptionCentering {
foods: any[] = [
{ value: 'steak-0', viewValue: 'Steak' },
{ value: 'pizza-1', viewValue: 'Pizza' },
{ value: 'tacos-2', viewValue: 'Tacos'},
{ value: 'sandwich-3', viewValue: 'Sandwich' },
{ value: 'chips-4', viewValue: 'Chips' },
{ value: 'eggs-5', viewValue: 'Eggs' },
{ value: 'pasta-6', viewValue: 'Pasta' },
{ value: 'sushi-7', viewValue: 'Sushi' },
];
control = new FormControl('pizza-1');

@ViewChild(MatSelect) select: MatSelect;
@ViewChildren(MatOption) options: QueryList<MatOption>;
}
15 changes: 15 additions & 0 deletions src/lib/select/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,9 @@ export class MatSelect extends _MatSelectMixinBase implements AfterContentInit,
},
];

/** Whether the component is disabling centering of the active option over the trigger. */
private _disableOptionCentering: boolean = false;

/** Whether the select is focused. */
focused: boolean = false;

Expand Down Expand Up @@ -360,6 +363,13 @@ export class MatSelect extends _MatSelectMixinBase implements AfterContentInit,
this._multiple = coerceBooleanProperty(value);
}

/** Whether to center the active option over the trigger. */
@Input()
get disableOptionCentering(): boolean { return this._disableOptionCentering; }
set disableOptionCentering(value: boolean) {
this._disableOptionCentering = coerceBooleanProperty(value);
}

/**
* A function to compare the option values with the selected values. The first argument
* is a value from an option. The second is a value from the selection. A boolean
Expand Down Expand Up @@ -1119,6 +1129,11 @@ export class MatSelect extends _MatSelectMixinBase implements AfterContentInit,
const maxOptionsDisplayed = Math.floor(SELECT_PANEL_MAX_HEIGHT / itemHeight);
let optionOffsetFromPanelTop: number;

// Disable offset if requested by user by returning 0 as value to offset
if (this._disableOptionCentering) {
return 0;
}

if (this._scrollTop === 0) {
optionOffsetFromPanelTop = selectedIndex * itemHeight;
} else if (this._scrollTop === maxScroll) {
Expand Down