Skip to content

Commit

Permalink
fix(module:date-picker): do not wrap on tabbing away from date-picker
Browse files Browse the repository at this point in the history
Tabbing away from date-picker overlay was not possible: It wrapped around and usually ended up in browser's address bar.
This fixes this. Tabbing away from overlay now focuses on date-picker wrapper span.
  • Loading branch information
jimmytheneutrino committed Jul 16, 2019
1 parent 3c65697 commit 440e6b8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
20 changes: 19 additions & 1 deletion components/date-picker/date-picker.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { By } from '@angular/platform-browser';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
import isSameDay from 'date-fns/is_same_day';

import { dispatchKeyboardEvent, dispatchMouseEvent, NgStyleInterface } from 'ng-zorro-antd/core';
import { dispatchFakeEvent, dispatchKeyboardEvent, dispatchMouseEvent, NgStyleInterface } from 'ng-zorro-antd/core';
import en_US from '../i18n/languages/en_US';

import { NzI18nModule, NzI18nService } from 'ng-zorro-antd/i18n';
Expand Down Expand Up @@ -111,6 +111,24 @@ describe('NzDatePickerComponent', () => {
expect(getPickerContainer()).not.toBeNull();
}));

it('should be tabbable back to trigger wrapper', fakeAsync(() => {
fixture.detectChanges();
dispatchMouseEvent(getPickerTriggerWrapper(), 'click');
fixture.detectChanges();
tick(500);
fixture.detectChanges();
expect(getPickerContainer()).not.toBeNull();

// It is impossible to simulate actual TAB behaviour using events.
// This is the next best thing.
dispatchFakeEvent(queryFromOverlay('span.nz-tab-catching-span'), 'focus');
fixture.detectChanges();
tick(500);
fixture.detectChanges();
expect(getPickerContainer()).toBeNull();
expect(document.activeElement).toEqual(getPickerTrigger());
}));

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
3 changes: 2 additions & 1 deletion components/date-picker/picker.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,5 @@
> <!-- Compatible for overlay that not support offset dynamically and immediately -->
<ng-content></ng-content>
</div>
</ng-template>
<span tabindex=0 (focus)="onClickBackdrop()" class="nz-tab-catching-span"></span>
</ng-template>
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

0 comments on commit 440e6b8

Please sign in to comment.