Skip to content
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

fix(module:date-picker): make keyboard navigation possible #3146

Closed
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
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());
}));

jimmytheneutrino marked this conversation as resolved.
Show resolved Hide resolved
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