Skip to content

Commit 0d4f80d

Browse files
crisbetojelbourn
authored andcommitted
refactor(autocomplete): use overlay api to set conditional panel class
Takes advantage of the API introduced in #12631 to add a class to the panel automatically, based on its position.
1 parent 3723191 commit 0d4f80d

File tree

3 files changed

+24
-19
lines changed

3 files changed

+24
-19
lines changed

src/lib/autocomplete/autocomplete-trigger.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -618,20 +618,25 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
618618
.withFlexibleDimensions(false)
619619
.withPush(false)
620620
.withPositions([
621-
{originX: 'start', originY: 'bottom', overlayX: 'start', overlayY: 'top'},
622-
{originX: 'start', originY: 'top', overlayX: 'start', overlayY: 'bottom'}
621+
{
622+
originX: 'start',
623+
originY: 'bottom',
624+
overlayX: 'start',
625+
overlayY: 'top'
626+
},
627+
{
628+
originX: 'start',
629+
originY: 'top',
630+
overlayX: 'start',
631+
overlayY: 'bottom',
632+
633+
// The overlay edge connected to the trigger should have squared corners, while
634+
// the opposite end has rounded corners. We apply a CSS class to swap the
635+
// border-radius based on the overlay position.
636+
panelClass: 'mat-autocomplete-panel-above'
637+
}
623638
]);
624639

625-
// The overlay edge connected to the trigger should have squared corners, while
626-
// the opposite end has rounded corners. We apply a CSS class to swap the
627-
// border-radius based on the overlay position.
628-
this._positionStrategy.positionChanges.subscribe(({connectionPair}) => {
629-
if (this.autocomplete) {
630-
this.autocomplete._classList['mat-autocomplete-panel-above'] =
631-
connectionPair.originY === 'top';
632-
}
633-
});
634-
635640
return this._positionStrategy;
636641
}
637642

src/lib/autocomplete/autocomplete.scss

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,14 @@ $mat-autocomplete-panel-border-radius: 4px !default;
2626
visibility: hidden;
2727
}
2828

29+
.mat-autocomplete-panel-above & {
30+
border-radius: 0;
31+
border-top-left-radius: $mat-autocomplete-panel-border-radius;
32+
border-top-right-radius: $mat-autocomplete-panel-border-radius;
33+
}
34+
2935
@include cdk-high-contrast {
3036
outline: solid 1px;
3137
}
3238
}
3339

34-
.mat-autocomplete-panel-above {
35-
border-radius: 0;
36-
border-top-left-radius: $mat-autocomplete-panel-border-radius;
37-
border-top-right-radius: $mat-autocomplete-panel-border-radius;
38-
}

src/lib/autocomplete/autocomplete.spec.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1441,8 +1441,7 @@ describe('MatAutocomplete', () => {
14411441
expect(Math.floor(inputTop))
14421442
.toEqual(Math.floor(panelBottom), `Expected panel to fall back to above position.`);
14431443

1444-
expect(panel.querySelector('.mat-autocomplete-panel')!.classList)
1445-
.toContain('mat-autocomplete-panel-above');
1444+
expect(panel.classList).toContain('mat-autocomplete-panel-above');
14461445
}));
14471446

14481447
it('should allow the panel to expand when the number of results increases', fakeAsync(() => {

0 commit comments

Comments
 (0)