Skip to content

Commit 8c30cee

Browse files
crisbetojosephperrott
authored andcommitted
fix(autocomplete): panel direction not being updated if the trigger direction changes (#10916)
1 parent 422d102 commit 8c30cee

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

src/lib/autocomplete/autocomplete-trigger.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -498,8 +498,9 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
498498
});
499499
}
500500
} else {
501-
/** Update the panel width, in case the host width has changed */
501+
// Update the panel width and direction, in case anything has changed.
502502
this._overlayRef.updateSize({width: this._getHostWidth()});
503+
this._overlayRef.setDirection(this._getDirection());
503504
}
504505

505506
if (this._overlayRef && !this._overlayRef.hasAttached()) {
@@ -524,7 +525,7 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
524525
positionStrategy: this._getOverlayPosition(),
525526
scrollStrategy: this._scrollStrategy(),
526527
width: this._getHostWidth(),
527-
direction: this._dir ? this._dir.value : 'ltr'
528+
direction: this._getDirection()
528529
});
529530
}
530531

@@ -541,6 +542,10 @@ export class MatAutocompleteTrigger implements ControlValueAccessor, OnDestroy {
541542
return this._positionStrategy;
542543
}
543544

545+
private _getDirection() {
546+
return this._dir ? this._dir.value : 'ltr';
547+
}
548+
544549
private _getConnectedElement(): ElementRef {
545550
return this._formField ? this._formField.getConnectedOverlayOrigin() : this._element;
546551
}

src/lib/autocomplete/autocomplete.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,30 @@ describe('MatAutocomplete', () => {
470470
expect(overlayPane.getAttribute('dir')).toEqual('rtl');
471471
});
472472

473+
it('should update the panel direction if it changes for the trigger', () => {
474+
const dirProvider = {value: 'rtl'};
475+
const rtlFixture = createComponent(SimpleAutocomplete, [
476+
{provide: Directionality, useFactory: () => dirProvider},
477+
]);
478+
479+
rtlFixture.detectChanges();
480+
rtlFixture.componentInstance.trigger.openPanel();
481+
rtlFixture.detectChanges();
482+
483+
let overlayPane = overlayContainerElement.querySelector('.cdk-overlay-pane')!;
484+
expect(overlayPane.getAttribute('dir')).toEqual('rtl');
485+
486+
rtlFixture.componentInstance.trigger.closePanel();
487+
rtlFixture.detectChanges();
488+
489+
dirProvider.value = 'ltr';
490+
rtlFixture.componentInstance.trigger.openPanel();
491+
rtlFixture.detectChanges();
492+
493+
overlayPane = overlayContainerElement.querySelector('.cdk-overlay-pane')!;
494+
expect(overlayPane.getAttribute('dir')).toEqual('ltr');
495+
});
496+
473497
describe('forms integration', () => {
474498
let fixture: ComponentFixture<SimpleAutocomplete>;
475499
let input: HTMLInputElement;

0 commit comments

Comments
 (0)