Skip to content

Commit 5c4a334

Browse files
crisbetojelbourn
authored andcommitted
fix(datepicker): allow calendar cell selection via the space key (#13098)
Fixes not being able to select items in the calendar using the spacebar. This consistent both with the native buttons that are in the calendar header, as well as the [datepicker by cms.gov](https://assets.cms.gov/resources/framework/2.0/Pages/datepicker.html).
1 parent 82163c4 commit 5c4a334

File tree

5 files changed

+49
-1
lines changed

5 files changed

+49
-1
lines changed

src/lib/datepicker/calendar.spec.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {Direction, Directionality} from '@angular/cdk/bidi';
2-
import {ENTER, RIGHT_ARROW} from '@angular/cdk/keycodes';
2+
import {ENTER, RIGHT_ARROW, SPACE} from '@angular/cdk/keycodes';
33
import {
44
dispatchFakeEvent,
55
dispatchKeyboardEvent,
@@ -230,6 +230,20 @@ describe('MatCalendar', () => {
230230
expect(calendarInstance.activeDate).toEqual(new Date(2017, FEB, 28));
231231
expect(testComponent.selected).toBeUndefined();
232232
});
233+
234+
it('should return to month view on space', () => {
235+
const tableBodyEl = calendarBodyEl.querySelector('.mat-calendar-body') as HTMLElement;
236+
237+
dispatchKeyboardEvent(tableBodyEl, 'keydown', RIGHT_ARROW);
238+
fixture.detectChanges();
239+
240+
dispatchKeyboardEvent(tableBodyEl, 'keydown', SPACE);
241+
fixture.detectChanges();
242+
243+
expect(calendarInstance.currentView).toBe('month');
244+
expect(calendarInstance.activeDate).toEqual(new Date(2017, FEB, 28));
245+
expect(testComponent.selected).toBeUndefined();
246+
});
233247
});
234248

235249
describe('multi-year view', () => {
@@ -253,6 +267,21 @@ describe('MatCalendar', () => {
253267
expect(calendarInstance.activeDate).toEqual(new Date(2018, JAN, 31));
254268
expect(testComponent.selected).toBeUndefined();
255269
});
270+
271+
it('should go to year view on space', () => {
272+
const tableBodyEl = calendarBodyEl.querySelector('.mat-calendar-body') as HTMLElement;
273+
274+
dispatchKeyboardEvent(tableBodyEl, 'keydown', RIGHT_ARROW);
275+
fixture.detectChanges();
276+
277+
dispatchKeyboardEvent(tableBodyEl, 'keydown', SPACE);
278+
fixture.detectChanges();
279+
280+
expect(calendarInstance.currentView).toBe('year');
281+
expect(calendarInstance.activeDate).toEqual(new Date(2018, JAN, 31));
282+
expect(testComponent.selected).toBeUndefined();
283+
});
284+
256285
});
257286

258287
});

src/lib/datepicker/month-view.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
PAGE_UP,
1010
RIGHT_ARROW,
1111
UP_ARROW,
12+
SPACE,
1213
} from '@angular/cdk/keycodes';
1314
import {dispatchFakeEvent, dispatchKeyboardEvent} from '@angular/cdk/testing';
1415
import {Component} from '@angular/core';
@@ -256,6 +257,18 @@ describe('MatMonthView', () => {
256257

257258
expect(testComponent.selected).toEqual(new Date(2017, JAN, 4));
258259
});
260+
261+
it('should select active date on space', () => {
262+
dispatchKeyboardEvent(calendarBodyEl, 'keydown', LEFT_ARROW);
263+
fixture.detectChanges();
264+
265+
expect(testComponent.selected).toEqual(new Date(2017, JAN, 10));
266+
267+
dispatchKeyboardEvent(calendarBodyEl, 'keydown', SPACE);
268+
fixture.detectChanges();
269+
270+
expect(testComponent.selected).toEqual(new Date(2017, JAN, 4));
271+
});
259272
});
260273
});
261274
});

src/lib/datepicker/month-view.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
PAGE_UP,
1717
RIGHT_ARROW,
1818
UP_ARROW,
19+
SPACE,
1920
} from '@angular/cdk/keycodes';
2021
import {
2122
AfterContentInit,
@@ -212,6 +213,7 @@ export class MatMonthView<D> implements AfterContentInit {
212213
this._dateAdapter.addCalendarMonths(this._activeDate, 1);
213214
break;
214215
case ENTER:
216+
case SPACE:
215217
if (!this.dateFilter || this.dateFilter(this._activeDate)) {
216218
this._dateSelected(this._dateAdapter.getDate(this._activeDate));
217219
this._userSelection.emit();

src/lib/datepicker/multi-year-view.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
PAGE_UP,
1717
RIGHT_ARROW,
1818
UP_ARROW,
19+
SPACE,
1920
} from '@angular/cdk/keycodes';
2021
import {
2122
AfterContentInit,
@@ -197,6 +198,7 @@ export class MatMultiYearView<D> implements AfterContentInit {
197198
this._activeDate, event.altKey ? yearsPerPage * 10 : yearsPerPage);
198199
break;
199200
case ENTER:
201+
case SPACE:
200202
this._yearSelected(this._dateAdapter.getYear(this._activeDate));
201203
break;
202204
default:

src/lib/datepicker/year-view.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
PAGE_UP,
1717
RIGHT_ARROW,
1818
UP_ARROW,
19+
SPACE,
1920
} from '@angular/cdk/keycodes';
2021
import {
2122
AfterContentInit,
@@ -188,6 +189,7 @@ export class MatYearView<D> implements AfterContentInit {
188189
this._dateAdapter.addCalendarYears(this._activeDate, event.altKey ? 10 : 1);
189190
break;
190191
case ENTER:
192+
case SPACE:
191193
this._monthSelected(this._dateAdapter.getMonth(this._activeDate));
192194
break;
193195
default:

0 commit comments

Comments
 (0)