Skip to content

Commit 4cc72b8

Browse files
crisbetommalerba
authored andcommitted
fix(datepicker): update popup direction if datepicker direction changes (#10871)
Fixes the popup direction going out of sync with the datepicker, if the datepicker's direction changes after the popup has been initialized.
1 parent 5554506 commit 4cc72b8

File tree

2 files changed

+74
-6
lines changed

2 files changed

+74
-6
lines changed

src/lib/datepicker/datepicker.spec.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {MatDatepickerInput} from './datepicker-input';
2929
import {MatDatepickerToggle} from './datepicker-toggle';
3030
import {MAT_DATEPICKER_SCROLL_STRATEGY, MatDatepickerIntl, MatDatepickerModule} from './index';
3131
import {Subject} from 'rxjs';
32+
import {Directionality} from '@angular/cdk/bidi';
3233

3334
describe('MatDatepicker', () => {
3435
const SUPPORTS_INTL = typeof Intl != 'undefined';
@@ -1268,6 +1269,68 @@ describe('MatDatepicker', () => {
12681269
}));
12691270
});
12701271

1272+
describe('datepicker directionality', () => {
1273+
it('should pass along the directionality to the popup', () => {
1274+
const fixture = createComponent(StandardDatepicker, [MatNativeDateModule], [{
1275+
provide: Directionality,
1276+
useValue: ({value: 'rtl'})
1277+
}]);
1278+
1279+
fixture.detectChanges();
1280+
fixture.componentInstance.datepicker.open();
1281+
fixture.detectChanges();
1282+
1283+
const overlay = document.querySelector('.cdk-overlay-pane')!;
1284+
1285+
expect(overlay.getAttribute('dir')).toBe('rtl');
1286+
});
1287+
1288+
it('should update the popup direction if the directionality value changes', fakeAsync(() => {
1289+
const dirProvider = {value: 'ltr'};
1290+
const fixture = createComponent(StandardDatepicker, [MatNativeDateModule], [{
1291+
provide: Directionality,
1292+
useFactory: () => dirProvider
1293+
}]);
1294+
1295+
fixture.detectChanges();
1296+
fixture.componentInstance.datepicker.open();
1297+
fixture.detectChanges();
1298+
1299+
let overlay = document.querySelector('.cdk-overlay-pane')!;
1300+
1301+
expect(overlay.getAttribute('dir')).toBe('ltr');
1302+
1303+
fixture.componentInstance.datepicker.close();
1304+
fixture.detectChanges();
1305+
flush();
1306+
1307+
dirProvider.value = 'rtl';
1308+
fixture.componentInstance.datepicker.open();
1309+
fixture.detectChanges();
1310+
1311+
overlay = document.querySelector('.cdk-overlay-pane')!;
1312+
1313+
expect(overlay.getAttribute('dir')).toBe('rtl');
1314+
}));
1315+
1316+
it('should pass along the directionality to the dialog in touch mode', () => {
1317+
const fixture = createComponent(StandardDatepicker, [MatNativeDateModule], [{
1318+
provide: Directionality,
1319+
useValue: ({value: 'rtl'})
1320+
}]);
1321+
1322+
fixture.componentInstance.touch = true;
1323+
fixture.detectChanges();
1324+
fixture.componentInstance.datepicker.open();
1325+
fixture.detectChanges();
1326+
1327+
const overlay = document.querySelector('.cdk-overlay-pane')!;
1328+
1329+
expect(overlay.getAttribute('dir')).toBe('rtl');
1330+
});
1331+
1332+
});
1333+
12711334
});
12721335

12731336
describe('with missing DateAdapter and MAT_DATE_FORMATS', () => {

src/lib/datepicker/datepicker.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -422,14 +422,13 @@ export class MatDatepicker<D> implements OnDestroy, CanColor {
422422
/** Open the calendar as a dialog. */
423423
private _openAsDialog(): void {
424424
this._dialogRef = this._dialog.open<MatDatepickerContent<D>>(MatDatepickerContent, {
425-
direction: this._dir ? this._dir.value : 'ltr',
425+
direction: this._getDirection(),
426426
viewContainerRef: this._viewContainerRef,
427427
panelClass: 'mat-datepicker-dialog',
428428
});
429-
if (this._dialogRef) {
430-
this._dialogRef.afterClosed().subscribe(() => this.close());
431-
this._dialogRef.componentInstance.datepicker = this;
432-
}
429+
430+
this._dialogRef.afterClosed().subscribe(() => this.close());
431+
this._dialogRef.componentInstance.datepicker = this;
433432
this._setColor();
434433
}
435434

@@ -445,6 +444,7 @@ export class MatDatepicker<D> implements OnDestroy, CanColor {
445444
}
446445

447446
if (!this._popupRef.hasAttached()) {
447+
this._popupRef.setDirection(this._getDirection());
448448
this._popupComponentRef = this._popupRef.attach(this._calendarPortal);
449449
this._popupComponentRef.instance.datepicker = this;
450450
this._setColor();
@@ -462,7 +462,7 @@ export class MatDatepicker<D> implements OnDestroy, CanColor {
462462
positionStrategy: this._createPopupPositionStrategy(),
463463
hasBackdrop: true,
464464
backdropClass: 'mat-overlay-transparent-backdrop',
465-
direction: this._dir ? this._dir.value : 'ltr',
465+
direction: this._getDirection(),
466466
scrollStrategy: this._scrollStrategy(),
467467
panelClass: 'mat-datepicker-popup',
468468
});
@@ -533,4 +533,9 @@ export class MatDatepicker<D> implements OnDestroy, CanColor {
533533
this._dialogRef.componentInstance.color = color;
534534
}
535535
}
536+
537+
/** Returns the layout direction of the datepicker. */
538+
private _getDirection() {
539+
return this._dir ? this._dir.value : 'ltr';
540+
}
536541
}

0 commit comments

Comments
 (0)