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): close date-picker after tabbing out #6571

Merged
merged 5 commits into from
Apr 6, 2021
Merged
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
30 changes: 29 additions & 1 deletion components/date-picker/date-picker.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,20 @@ describe('NzDatePickerComponent', () => {
expect(getPickerContainer()).not.toBeNull();
}));

it('should open by click and close by tab', fakeAsync(() => {
fixtureInstance.useSuite = 5;

fixture.detectChanges();
openPickerByClickTrigger();
expect(getPickerContainer()).not.toBeNull();

getSecondPickerInput(fixture.debugElement).focus();
fixture.detectChanges();
flush();
fixture.detectChanges();
expect(getPickerContainer()).toBeNull();
}));

it('should support changing language at runtime', fakeAsync(() => {
fixture.detectChanges();
expect(getPickerInput(fixture.debugElement).placeholder).toBe('请选择日期');
Expand Down Expand Up @@ -467,6 +481,10 @@ describe('NzDatePickerComponent', () => {
openPickerByClickTrigger();
expect(overlayContainerElement.children[0].classList).toContain('cdk-overlay-backdrop');
}));

function getSecondPickerInput(fixtureDebugElement: DebugElement): HTMLInputElement {
return fixtureDebugElement.queryAll(By.css(`.${PREFIX_CLASS}-input input`))[1].nativeElement as HTMLInputElement;
}
});

describe('panel switch and move forward/afterward', () => {
Expand Down Expand Up @@ -1141,11 +1159,17 @@ describe('date-fns testing', () => {

<!-- Suite 4 -->
<nz-date-picker *ngSwitchCase="4" [formControl]="control"></nz-date-picker>

<!-- Suite 5 -->
<ng-container *ngSwitchCase="5">
<nz-date-picker [ngModel]="firstValue"></nz-date-picker>
<nz-date-picker [ngModel]="secondValue"></nz-date-picker>
</ng-container>
</ng-container>
`
})
class NzTestDatePickerComponent {
useSuite!: 1 | 2 | 3 | 4;
useSuite!: 1 | 2 | 3 | 4 | 5;
@ViewChild('tplDateRender', { static: true }) tplDateRender!: TemplateRef<Date>;
@ViewChild('tplExtraFooter', { static: true }) tplExtraFooter!: TemplateRef<void>;
@ViewChild(NzDatePickerComponent, { static: false }) datePicker!: NzDatePickerComponent;
Expand Down Expand Up @@ -1193,4 +1217,8 @@ class NzTestDatePickerComponent {

// --- Suite 4
control!: FormControl;

// --- Suite 5
firstValue!: Date;
secondValue!: Date;
}
7 changes: 5 additions & 2 deletions components/date-picker/date-picker.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,13 +383,16 @@ export class NzDatePickerComponent implements OnInit, OnChanges, OnDestroy, Cont
this.datePickerService.initialValue = newValue;
}

onFocusChange(value: boolean): void {
this.focused = value;
onFocusChange(value: FocusEvent): void {
// When the relatedTarget is part of the elementRef, it means that it's a range-picker and you are navigating to
// the other input in that range picker. In that case we don't want to close the picker.
this.focused = (value.type === 'blur' && this.elementRef.nativeElement.contains(value.relatedTarget)) || value.type === 'focus';
// TODO: avoid autoFocus cause change after checked error
if (this.focused) {
this.renderer.addClass(this.elementRef.nativeElement, 'ant-picker-focused');
} else {
this.renderer.removeClass(this.elementRef.nativeElement, 'ant-picker-focused');
this.close();
}
}

Expand Down
6 changes: 3 additions & 3 deletions components/date-picker/picker.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ export class NzPickerComponent implements OnInit, AfterViewInit, OnChanges, OnDe
@Input() nzId: string | null = null;
@Input() hasBackdrop = false;

@Output() readonly focusChange = new EventEmitter<boolean>();
@Output() readonly focusChange = new EventEmitter<FocusEvent>();
@Output() readonly valueChange = new EventEmitter<CandyDate | CandyDate[] | null>();
@Output() readonly openChange = new EventEmitter<boolean>(); // Emitted when overlay's open state change

Expand Down Expand Up @@ -336,15 +336,15 @@ export class NzPickerComponent implements OnInit, AfterViewInit, OnChanges, OnDe

onFocus(event: FocusEvent, partType?: RangePartType): void {
event.preventDefault();
this.focusChange.emit(true);
this.focusChange.emit(event);
if (partType) {
this.datePickerService.inputPartChange$.next(partType);
}
}

onBlur(event: FocusEvent): void {
event.preventDefault();
this.focusChange.emit(false);
this.focusChange.emit(event);
}

// Show overlay content
Expand Down
38 changes: 36 additions & 2 deletions components/date-picker/range-picker.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,27 @@ describe('NzRangePickerComponent', () => {
expect(getPickerContainer()).toBeNull();
}));

it('should open by click and close by tab', fakeAsync(() => {
fixtureInstance.useSuite = 4;

fixture.detectChanges();
expect(getPickerContainer()).toBeNull();
openPickerByClickTrigger();
expect(getPickerContainer()).not.toBeNull();

getRangePickerRightInput(fixture.debugElement).focus();
fixture.detectChanges();
tick(500);
fixture.detectChanges();
expect(getPickerContainer()).not.toBeNull();

getRegularPickerInput(fixture.debugElement).focus();
fixture.detectChanges();
tick(500);
fixture.detectChanges();
expect(getPickerContainer()).toBeNull();
}));

it('should focus on the trigger after a click outside', fakeAsync(() => {
fixture.detectChanges();
openPickerByClickTrigger();
Expand Down Expand Up @@ -930,6 +951,10 @@ describe('NzRangePickerComponent', () => {
return queryFromOverlay('.ant-picker-panel:first-child td.ant-picker-cell-selected .ant-picker-cell-inner') as HTMLElement;
}

function getRegularPickerInput(fixtureDebugElement: DebugElement): HTMLInputElement {
return fixtureDebugElement.queryAll(By.css(`.${PREFIX_CLASS}-input input`))[2].nativeElement as HTMLInputElement;
}

// function getSecondSelectedDayCell(): HTMLElement {
// return queryFromOverlay('.ant-picker-panel:last-child td.ant-picker-cell-selected .ant-picker-cell-inner') as HTMLElement;
// }
Expand Down Expand Up @@ -1033,16 +1058,22 @@ describe('NzRangePickerComponent', () => {
<ng-template #tplExtraFooter>TEST_EXTRA_FOOTER</ng-template>

<!-- Suite 2 -->
<!-- use another picker to avoid nzOpen's side-effects beacuse nzOpen act as "true" if used -->
<!-- use another picker to avoid nzOpen's side-effects because nzOpen act as "true" if used -->
<nz-range-picker *ngSwitchCase="2" [nzOpen]="nzOpen"></nz-range-picker>

<!-- Suite 3 -->
<nz-range-picker *ngSwitchCase="3" nzOpen [(ngModel)]="modelValue"></nz-range-picker>

<!-- Suite 4 -->
<ng-container *ngSwitchCase="4">
<nz-range-picker [(ngModel)]="modelValue"></nz-range-picker>
<nz-date-picker [ngModel]="singleValue"></nz-date-picker>
</ng-container>
</ng-container>
`
})
class NzTestRangePickerComponent {
useSuite!: 1 | 2 | 3;
useSuite!: 1 | 2 | 3 | 4;
@ViewChild('tplDateRender', { static: true }) tplDateRender!: TemplateRef<Date>;
@ViewChild('tplExtraFooter', { static: true }) tplExtraFooter!: TemplateRef<void>;

Expand Down Expand Up @@ -1078,4 +1109,7 @@ class NzTestRangePickerComponent {

// --- Suite 2
nzOpen: boolean = false;

// --- Suite 4
singleValue!: Date;
}