Skip to content

fix(datepicker): calendar controls not being inverted in rtl #9219

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
Jan 24, 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/calendar.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
SEP,
} from '@angular/material/core';
import {By} from '@angular/platform-browser';
import {Direction, Directionality} from '@angular/cdk/bidi';
import {MatButtonModule} from '../button/index';
import {MatCalendar} from './calendar';
import {MatCalendarBody} from './calendar-body';
Expand All @@ -36,6 +37,8 @@ import {MatYearView} from './year-view';


describe('MatCalendar', () => {
let dir: {value: Direction};

beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
Expand All @@ -55,6 +58,7 @@ describe('MatCalendar', () => {
],
providers: [
MatDatepickerIntl,
{provide: Directionality, useFactory: () => dir = {value: 'ltr'}}
],
});

Expand Down Expand Up @@ -204,6 +208,20 @@ describe('MatCalendar', () => {
expect(calendarInstance._activeDate).toEqual(new Date(2016, DEC, 31));
});

it('should increment date on left arrow press in rtl', () => {
dir.value = 'rtl';

dispatchKeyboardEvent(calendarBodyEl, 'keydown', LEFT_ARROW);
fixture.detectChanges();

expect(calendarInstance._activeDate).toEqual(new Date(2017, FEB, 1));

dispatchKeyboardEvent(calendarBodyEl, 'keydown', LEFT_ARROW);
fixture.detectChanges();

expect(calendarInstance._activeDate).toEqual(new Date(2017, FEB, 2));
});

it('should increment date on right arrow press', () => {
dispatchKeyboardEvent(calendarBodyEl, 'keydown', RIGHT_ARROW);
fixture.detectChanges();
Expand All @@ -216,6 +234,23 @@ describe('MatCalendar', () => {
expect(calendarInstance._activeDate).toEqual(new Date(2017, FEB, 2));
});

it('should decrement date on right arrow press in rtl', () => {
dir.value = 'rtl';

dispatchKeyboardEvent(calendarBodyEl, 'keydown', RIGHT_ARROW);
fixture.detectChanges();

expect(calendarInstance._activeDate).toEqual(new Date(2017, JAN, 30));

calendarInstance._activeDate = new Date(2017, JAN, 1);
fixture.detectChanges();

dispatchKeyboardEvent(calendarBodyEl, 'keydown', RIGHT_ARROW);
fixture.detectChanges();

expect(calendarInstance._activeDate).toEqual(new Date(2016, DEC, 31));
});

it('should go up a row on up arrow press', () => {
dispatchKeyboardEvent(calendarBodyEl, 'keydown', UP_ARROW);
fixture.detectChanges();
Expand Down Expand Up @@ -326,6 +361,20 @@ describe('MatCalendar', () => {
expect(calendarInstance._activeDate).toEqual(new Date(2016, NOV, 30));
});

it('should increment month on left arrow press in rtl', () => {
dir.value = 'rtl';

dispatchKeyboardEvent(calendarBodyEl, 'keydown', LEFT_ARROW);
fixture.detectChanges();

expect(calendarInstance._activeDate).toEqual(new Date(2017, FEB, 28));

dispatchKeyboardEvent(calendarBodyEl, 'keydown', LEFT_ARROW);
fixture.detectChanges();

expect(calendarInstance._activeDate).toEqual(new Date(2017, MAR, 28));
});

it('should increment month on right arrow press', () => {
dispatchKeyboardEvent(calendarBodyEl, 'keydown', RIGHT_ARROW);
fixture.detectChanges();
Expand All @@ -338,6 +387,20 @@ describe('MatCalendar', () => {
expect(calendarInstance._activeDate).toEqual(new Date(2017, MAR, 28));
});

it('should decrement month on right arrow press in rtl', () => {
dir.value = 'rtl';

dispatchKeyboardEvent(calendarBodyEl, 'keydown', RIGHT_ARROW);
fixture.detectChanges();

expect(calendarInstance._activeDate).toEqual(new Date(2016, DEC, 31));

dispatchKeyboardEvent(calendarBodyEl, 'keydown', RIGHT_ARROW);
fixture.detectChanges();

expect(calendarInstance._activeDate).toEqual(new Date(2016, NOV, 30));
});

it('should go up a row on up arrow press', () => {
dispatchKeyboardEvent(calendarBodyEl, 'keydown', UP_ARROW);
fixture.detectChanges();
Expand Down
21 changes: 16 additions & 5 deletions src/lib/datepicker/calendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import {createMissingDateImplError} from './datepicker-errors';
import {MatDatepickerIntl} from './datepicker-intl';
import {MatMonthView} from './month-view';
import {MatYearView} from './year-view';
import {Directionality} from '@angular/cdk/bidi';


/**
Expand Down Expand Up @@ -162,7 +163,8 @@ export class MatCalendar<D> implements AfterContentInit, OnDestroy, OnChanges {
private _ngZone: NgZone,
@Optional() private _dateAdapter: DateAdapter<D>,
@Optional() @Inject(MAT_DATE_FORMATS) private _dateFormats: MatDateFormats,
changeDetectorRef: ChangeDetectorRef) {
changeDetectorRef: ChangeDetectorRef,
@Optional() private _dir?: Directionality) {

if (!this._dateAdapter) {
throw createMissingDateImplError('DateAdapter');
Expand Down Expand Up @@ -277,12 +279,14 @@ export class MatCalendar<D> implements AfterContentInit, OnDestroy, OnChanges {

/** Handles keydown events on the calendar body when calendar is in month view. */
private _handleCalendarBodyKeydownInMonthView(event: KeyboardEvent): void {
const isRtl = this._isRtl();

switch (event.keyCode) {
case LEFT_ARROW:
this._activeDate = this._dateAdapter.addCalendarDays(this._activeDate, -1);
this._activeDate = this._dateAdapter.addCalendarDays(this._activeDate, isRtl ? 1 : -1);
break;
case RIGHT_ARROW:
this._activeDate = this._dateAdapter.addCalendarDays(this._activeDate, 1);
this._activeDate = this._dateAdapter.addCalendarDays(this._activeDate, isRtl ? -1 : 1);
break;
case UP_ARROW:
this._activeDate = this._dateAdapter.addCalendarDays(this._activeDate, -7);
Expand Down Expand Up @@ -329,12 +333,14 @@ export class MatCalendar<D> implements AfterContentInit, OnDestroy, OnChanges {

/** Handles keydown events on the calendar body when calendar is in year view. */
private _handleCalendarBodyKeydownInYearView(event: KeyboardEvent): void {
const isRtl = this._isRtl();

switch (event.keyCode) {
case LEFT_ARROW:
this._activeDate = this._dateAdapter.addCalendarMonths(this._activeDate, -1);
this._activeDate = this._dateAdapter.addCalendarMonths(this._activeDate, isRtl ? 1 : -1);
break;
case RIGHT_ARROW:
this._activeDate = this._dateAdapter.addCalendarMonths(this._activeDate, 1);
this._activeDate = this._dateAdapter.addCalendarMonths(this._activeDate, isRtl ? -1 : 1);
break;
case UP_ARROW:
this._activeDate = this._prevMonthInSameCol(this._activeDate);
Expand Down Expand Up @@ -396,4 +402,9 @@ export class MatCalendar<D> implements AfterContentInit, OnDestroy, OnChanges {
private _getValidDateOrNull(obj: any): D | null {
return (this._dateAdapter.isDateInstance(obj) && this._dateAdapter.isValid(obj)) ? obj : null;
}

/** Determines whether the user has the RTL layout direction. */
private _isRtl() {
return this._dir && this._dir.value === 'rtl';
}
}