Skip to content

Commit

Permalink
feat(datepicker): add aria-* attrs and keyboard bindings to datepicke…
Browse files Browse the repository at this point in the history
…r input (#3542)

* add aria-* attrs and keyboard bindings to datepicker input

* fix rebase errors

* added comment
  • Loading branch information
mmalerba authored Mar 10, 2017
1 parent a489c03 commit d196c36
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/lib/datepicker/datepicker-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {SimpleDate} from '../core/datetime/simple-date';
import {CalendarLocale} from '../core/datetime/calendar-locale';
import {Subscription} from 'rxjs';
import {MdInputContainer} from '../input/input-container';
import {DOWN_ARROW} from '../core/keyboard/keycodes';


export const MD_DATEPICKER_VALUE_ACCESSOR: any = {
Expand All @@ -28,8 +29,12 @@ export const MD_DATEPICKER_VALUE_ACCESSOR: any = {
selector: 'input[mdDatepicker], input[matDatepicker]',
providers: [MD_DATEPICKER_VALUE_ACCESSOR],
host: {
'[attr.aria-expanded]': '_datepicker?.opened || "false"',
'[attr.aria-haspopup]': 'true',
'[attr.aria-owns]': '_datepicker?.id',
'(input)': '_onChange($event.target.value)',
'(blur)': '_onTouched()',
'(keydown)': '_onKeydown($event)',
}
})
export class MdDatepickerInput implements AfterContentInit, ControlValueAccessor, OnDestroy {
Expand All @@ -40,7 +45,7 @@ export class MdDatepickerInput implements AfterContentInit, ControlValueAccessor
this._datepicker._registerInput(this);
}
}
private _datepicker: MdDatepicker;
_datepicker: MdDatepicker;

@Input()
get value(): SimpleDate {
Expand Down Expand Up @@ -107,4 +112,11 @@ export class MdDatepickerInput implements AfterContentInit, ControlValueAccessor
setDisabledState(disabled: boolean): void {
this._renderer.setElementProperty(this._elementRef.nativeElement, 'disabled', disabled);
}

_onKeydown(event: KeyboardEvent) {
if (event.altKey && event.keyCode === DOWN_ARROW) {
this._datepicker.open();
event.preventDefault();
}
}
}
1 change: 1 addition & 0 deletions src/lib/datepicker/datepicker.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<template>
<md-calendar
[id]="id"
[class.mat-datepicker-touch]="touchUi"
[class.mat-datepicker-non-touch]="!touchUi"
[startAt]="startAt"
Expand Down
16 changes: 16 additions & 0 deletions src/lib/datepicker/datepicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ import {MdDatepickerInput} from './datepicker-input';
import {CalendarLocale} from '../core/datetime/calendar-locale';


/** Used to generate a unique ID for each datepicker instance. */
let datepickerUid = 0;


/** Component responsible for managing the datepicker popup/dialog. */
@Component({
moduleId: module.id,
Expand Down Expand Up @@ -62,6 +66,10 @@ export class MdDatepicker implements OnDestroy {

@Output() selectedChanged = new EventEmitter<SimpleDate>();

opened = false;

id = `md-datepicker-${datepickerUid++}`;

get _selected(): SimpleDate {
return this._datepickerInput ? this._datepickerInput.value : null;
}
Expand Down Expand Up @@ -112,6 +120,9 @@ export class MdDatepicker implements OnDestroy {
* @param touchUi Whether to use the touch UI.
*/
open(): void {
if (this.opened) {
return;
}
if (!this._datepickerInput) {
throw new MdError('Attempted to open an MdDatepicker with no associated input.');
}
Expand All @@ -121,10 +132,14 @@ export class MdDatepicker implements OnDestroy {
}

this.touchUi ? this._openAsDialog() : this._openAsPopup();
this.opened = true;
}

/** Close the calendar. */
close(): void {
if (!this.opened) {
return;
}
if (this._popupRef && this._popupRef.hasAttached()) {
this._popupRef.detach();
}
Expand All @@ -135,6 +150,7 @@ export class MdDatepicker implements OnDestroy {
if (this._calendarPortal && this._calendarPortal.isAttached) {
this._calendarPortal.detach();
}
this.opened = false;
}

/** Open the calendar as a dialog. */
Expand Down

0 comments on commit d196c36

Please sign in to comment.