From 15d3252aece227dd4425af41862af99a455d3ff5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Cambon?= Date: Thu, 16 Jun 2022 11:24:01 +0000 Subject: [PATCH] fix(module:datepicker): focus input when opened programmatically --- .../date-picker/date-picker.component.spec.ts | 13 +++++++++++++ components/date-picker/date-picker.component.ts | 1 + 2 files changed, 14 insertions(+) diff --git a/components/date-picker/date-picker.component.spec.ts b/components/date-picker/date-picker.component.spec.ts index 9c21d79160..7e638c2c1b 100644 --- a/components/date-picker/date-picker.component.spec.ts +++ b/components/date-picker/date-picker.component.spec.ts @@ -159,6 +159,12 @@ describe('NzDatePickerComponent', () => { expect(getPickerContainer()).not.toBeNull(); })); + it('should have focus when opened progammatically', fakeAsync(() => { + fixture.detectChanges(); + openPickerByCode(); + expect(document.activeElement).toEqual(getPickerInput(fixture.debugElement)); + })); + it('should open by click and close by tab', fakeAsync(() => { const nzOnChange = spyOn(fixtureInstance, 'nzOnChange'); fixtureInstance.useSuite = 5; @@ -1143,6 +1149,13 @@ describe('NzDatePickerComponent', () => { fixture.detectChanges(); } + function openPickerByCode(): void { + fixtureInstance.datePicker.open(); + fixture.detectChanges(); + tick(500); + fixture.detectChanges(); + } + function triggerInputBlur(): void { dispatchFakeEvent(getPickerInput(fixture.debugElement), 'focusout'); } diff --git a/components/date-picker/date-picker.component.ts b/components/date-picker/date-picker.component.ts index 3e4ce081b1..d9a914a90f 100644 --- a/components/date-picker/date-picker.component.ts +++ b/components/date-picker/date-picker.component.ts @@ -458,6 +458,7 @@ export class NzDatePickerComponent implements OnInit, OnChanges, OnDestroy, Afte this.updateInputWidthAndArrowLeft(); this.overlayOpen = true; this.nzOnOpenChange.emit(true); + this.focus(); this.cdr.markForCheck(); } }