Skip to content

fix(datepicker): update popup direction if datepicker direction changes #10871

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 1 commit into from
Apr 16, 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
63 changes: 63 additions & 0 deletions src/lib/datepicker/datepicker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {MatDatepickerInput} from './datepicker-input';
import {MatDatepickerToggle} from './datepicker-toggle';
import {MAT_DATEPICKER_SCROLL_STRATEGY, MatDatepickerIntl, MatDatepickerModule} from './index';
import {Subject} from 'rxjs';
import {Directionality} from '@angular/cdk/bidi';

describe('MatDatepicker', () => {
const SUPPORTS_INTL = typeof Intl != 'undefined';
Expand Down Expand Up @@ -1268,6 +1269,68 @@ describe('MatDatepicker', () => {
}));
});

describe('datepicker directionality', () => {
it('should pass along the directionality to the popup', () => {
const fixture = createComponent(StandardDatepicker, [MatNativeDateModule], [{
provide: Directionality,
useValue: ({value: 'rtl'})
}]);

fixture.detectChanges();
fixture.componentInstance.datepicker.open();
fixture.detectChanges();

const overlay = document.querySelector('.cdk-overlay-pane')!;

expect(overlay.getAttribute('dir')).toBe('rtl');
});

it('should update the popup direction if the directionality value changes', fakeAsync(() => {
const dirProvider = {value: 'ltr'};
const fixture = createComponent(StandardDatepicker, [MatNativeDateModule], [{
provide: Directionality,
useFactory: () => dirProvider
}]);

fixture.detectChanges();
fixture.componentInstance.datepicker.open();
fixture.detectChanges();

let overlay = document.querySelector('.cdk-overlay-pane')!;

expect(overlay.getAttribute('dir')).toBe('ltr');

fixture.componentInstance.datepicker.close();
fixture.detectChanges();
flush();

dirProvider.value = 'rtl';
fixture.componentInstance.datepicker.open();
fixture.detectChanges();

overlay = document.querySelector('.cdk-overlay-pane')!;

expect(overlay.getAttribute('dir')).toBe('rtl');
}));

it('should pass along the directionality to the dialog in touch mode', () => {
const fixture = createComponent(StandardDatepicker, [MatNativeDateModule], [{
provide: Directionality,
useValue: ({value: 'rtl'})
}]);

fixture.componentInstance.touch = true;
fixture.detectChanges();
fixture.componentInstance.datepicker.open();
fixture.detectChanges();

const overlay = document.querySelector('.cdk-overlay-pane')!;

expect(overlay.getAttribute('dir')).toBe('rtl');
});

});

});

describe('with missing DateAdapter and MAT_DATE_FORMATS', () => {
Expand Down
17 changes: 11 additions & 6 deletions src/lib/datepicker/datepicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,14 +422,13 @@ export class MatDatepicker<D> implements OnDestroy, CanColor {
/** Open the calendar as a dialog. */
private _openAsDialog(): void {
this._dialogRef = this._dialog.open<MatDatepickerContent<D>>(MatDatepickerContent, {
direction: this._dir ? this._dir.value : 'ltr',
direction: this._getDirection(),
viewContainerRef: this._viewContainerRef,
panelClass: 'mat-datepicker-dialog',
});
if (this._dialogRef) {
this._dialogRef.afterClosed().subscribe(() => this.close());
this._dialogRef.componentInstance.datepicker = this;
}

this._dialogRef.afterClosed().subscribe(() => this.close());
this._dialogRef.componentInstance.datepicker = this;
this._setColor();
}

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

if (!this._popupRef.hasAttached()) {
this._popupRef.setDirection(this._getDirection());
this._popupComponentRef = this._popupRef.attach(this._calendarPortal);
this._popupComponentRef.instance.datepicker = this;
this._setColor();
Expand All @@ -462,7 +462,7 @@ export class MatDatepicker<D> implements OnDestroy, CanColor {
positionStrategy: this._createPopupPositionStrategy(),
hasBackdrop: true,
backdropClass: 'mat-overlay-transparent-backdrop',
direction: this._dir ? this._dir.value : 'ltr',
direction: this._getDirection(),
scrollStrategy: this._scrollStrategy(),
panelClass: 'mat-datepicker-popup',
});
Expand Down Expand Up @@ -533,4 +533,9 @@ export class MatDatepicker<D> implements OnDestroy, CanColor {
this._dialogRef.componentInstance.color = color;
}
}

/** Returns the layout direction of the datepicker. */
private _getDirection() {
return this._dir ? this._dir.value : 'ltr';
}
}