Skip to content

Commit

Permalink
fix(module:date-picker): open on enter and focus on inner input
Browse files Browse the repository at this point in the history
Extracted the good parts suitable for a11y from NG-ZORRO#3146. See discussion there.
  • Loading branch information
jimmytheneutrino committed Jul 16, 2019
1 parent 4dd93e6 commit 3b0ee51
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 9 deletions.
15 changes: 15 additions & 0 deletions components/date-picker/date-picker.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,21 @@ describe('NzDatePickerComponent', () => {
expect(getPickerContainer()).toBeNull();
}));

it('should open on enter', fakeAsync(() => {
fixture.detectChanges();
getPickerTriggerWrapper().dispatchEvent(new KeyboardEvent('keyup', { key: 'enter' }));
fixture.detectChanges();
tick(500);
fixture.detectChanges();
expect(getPickerContainer()).not.toBeNull();
}));

it('should open by click and focus on inner calendar input', fakeAsync(() => {
fixture.detectChanges();
openPickerByClickTrigger();
expect(document.activeElement).toEqual(queryFromOverlay('input.ant-calendar-input'));
}));

it('should support changing language at runtime', fakeAsync(() => {
fixture.detectChanges();
expect(getPickerTrigger().placeholder).toBe('请选择日期');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
placeholder="{{ placeholder || locale.dateSelect }}"
value="{{ toReadableInput(value) }}"
(keyup)="onInputKeyup($event)"
#inputElement
/>
</div>
<a class="{{ prefixCls }}-clear-btn" role="button" title="{{ locale.clear }}">
Expand Down
10 changes: 9 additions & 1 deletion components/date-picker/lib/calendar/calendar-input.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
import {
ChangeDetectionStrategy,
Component,
ElementRef,
EventEmitter,
Input,
OnInit,
Output,
ViewChild,
ViewEncapsulation
} from '@angular/core';

Expand All @@ -34,14 +36,20 @@ export class CalendarInputComponent implements OnInit {
@Input() disabledDate: (d: Date) => boolean;

@Input() value: CandyDate;
@Input() autoFocus: boolean;
@Output() readonly valueChange = new EventEmitter<CandyDate>();
@ViewChild('inputElement', { static: true }) inputRef: ElementRef;

prefixCls: string = 'ant-calendar';
invalidInputClass: string = '';

constructor(private dateHelper: DateHelperService) {}

ngOnInit(): void {}
ngOnInit(): void {
if (this.autoFocus) {
this.inputRef.nativeElement.focus();
}
}

onInputKeyup(event: Event): void {
const date = this.checkValidInputDate(event);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
[locale]="locale"
[disabledDate]="disabledDate"
[format]="format"
[autoFocus]="partType != 'right'"
[placeholder]="getPlaceholder(partType)"
></calendar-input>
</ng-template>
Expand Down
9 changes: 9 additions & 0 deletions components/date-picker/month-picker.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ describe('NzMonthPickerComponent', () => {
expect(getPickerContainer()).toBeNull();
}));

it('should open on enter', fakeAsync(() => {
fixture.detectChanges();
getPickerTriggerWrapper().dispatchEvent(new KeyboardEvent('keyup', { key: 'enter' }));
fixture.detectChanges();
tick(500);
fixture.detectChanges();
expect(getPickerContainer()).not.toBeNull();
}));

it('should support nzAllowClear and work properly', fakeAsync(() => {
const clearBtnSelector = By.css('nz-picker i.ant-calendar-picker-clear');
const initial = (fixtureInstance.nzValue = new Date());
Expand Down
1 change: 1 addition & 0 deletions components/date-picker/picker.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
[ngStyle]="style"
tabindex="0"
(click)="onClickInputBox()"
(keyup.enter)="onClickInputBox()"
>
<!-- Content of single picker -->
<ng-container *ngIf="!isRange">
Expand Down
21 changes: 13 additions & 8 deletions components/date-picker/picker.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,18 @@ export class NzPickerComponent implements OnInit, AfterViewInit {

ngAfterViewInit(): void {
if (this.autoFocus) {
if (this.isRange) {
const firstInput = (this.pickerInput.nativeElement as HTMLElement).querySelector(
'input:first-child'
) as HTMLInputElement;
firstInput.focus(); // Focus on the first input
} else {
this.pickerInput.nativeElement.focus();
}
this.focus();
}
}

focus(): void {
if (this.isRange) {
const firstInput = (this.pickerInput.nativeElement as HTMLElement).querySelector(
'input:first-child'
) as HTMLInputElement;
firstInput.focus(); // Focus on the first input
} else {
this.pickerInput.nativeElement.focus();
}
}

Expand All @@ -135,6 +139,7 @@ export class NzPickerComponent implements OnInit, AfterViewInit {
if (this.realOpenState) {
this.overlayOpen = false;
this.openChange.emit(this.overlayOpen);
this.focus();
}
}

Expand Down
15 changes: 15 additions & 0 deletions components/date-picker/range-picker.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,21 @@ describe('NzRangePickerComponent', () => {
expect(getPickerContainer()).toBeNull();
}));

it('should open on enter', fakeAsync(() => {
fixture.detectChanges();
getPickerTriggerWrapper().dispatchEvent(new KeyboardEvent('keyup', { key: 'enter' }));
fixture.detectChanges();
tick(500);
fixture.detectChanges();
expect(getPickerContainer()).not.toBeNull();
}));

it('should open by click and focus on left inner calendar input', fakeAsync(() => {
fixture.detectChanges();
openPickerByClickTrigger();
expect(document.activeElement).toEqual(queryFromOverlay('.ant-calendar-range-left input.ant-calendar-input'));
}));

it('should support nzAllowClear and work properly', fakeAsync(() => {
const clearBtnSelector = By.css('nz-picker i.ant-calendar-picker-clear');
const initial = (fixtureInstance.modelValue = [new Date(), new Date()]);
Expand Down
9 changes: 9 additions & 0 deletions components/date-picker/year-picker.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ describe('NzYearPickerComponent', () => {
expect(getPickerContainer()).toBeNull();
}));

it('should open on enter', fakeAsync(() => {
fixture.detectChanges();
getPickerTriggerWrapper().dispatchEvent(new KeyboardEvent('keyup', { key: 'enter' }));
fixture.detectChanges();
tick(500);
fixture.detectChanges();
expect(getPickerContainer()).not.toBeNull();
}));

it('should support nzAllowClear and work properly', fakeAsync(() => {
const clearBtnSelector = By.css('nz-picker i.ant-calendar-picker-clear');
const initial = (fixtureInstance.nzValue = new Date());
Expand Down

0 comments on commit 3b0ee51

Please sign in to comment.