Skip to content

Commit ee4872e

Browse files
authored
fix(module:date-picker): fix datePicker show multi panel (#7680)
close #7450
1 parent 1a16154 commit ee4872e

3 files changed

Lines changed: 44 additions & 0 deletions

File tree

‎components/date-picker/date-picker.component.spec.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,28 @@ describe('NzDatePickerComponent', () => {
221221
expect(getPickerContainer()).not.toBeNull();
222222
}));
223223

224+
it('should prevent default on the mousedown event when mouse down in date picker', fakeAsync(() => {
225+
fixture.detectChanges();
226+
openPickerByClickTrigger();
227+
228+
const event = new MouseEvent('mousedown');
229+
spyOn(event, 'preventDefault').and.callThrough();
230+
fixture.nativeElement.querySelector(`.${PREFIX_CLASS}`).dispatchEvent(event);
231+
232+
expect(event.preventDefault).toHaveBeenCalled();
233+
}));
234+
235+
it('should execute default on the mousedown event when mouse down in date picker input', fakeAsync(() => {
236+
fixture.detectChanges();
237+
openPickerByClickTrigger();
238+
239+
const event = new MouseEvent('mousedown');
240+
spyOn(event, 'preventDefault').and.callThrough();
241+
fixture.nativeElement.querySelector(`.${PREFIX_CLASS} input`).dispatchEvent(event);
242+
243+
expect(event.preventDefault).not.toHaveBeenCalled();
244+
}));
245+
224246
it('should support nzAllowClear and work properly', fakeAsync(() => {
225247
const clearBtnSelector = By.css(`.${PREFIX_CLASS}-clear`);
226248
const initial = (fixtureInstance.nzValue = new Date());

‎components/date-picker/date-picker.component.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,10 @@ export class NzDatePickerComponent implements OnInit, OnChanges, OnDestroy, Afte
369369
this.focus();
370370
this.updateInputWidthAndArrowLeft();
371371
});
372+
373+
// prevent mousedown event to trigger focusout event when click in date picker
374+
// see: https://github.com/NG-ZORRO/ng-zorro-antd/issues/7450
375+
this.elementRef.nativeElement.addEventListener('mousedown', this.onMouseDown);
372376
}
373377

374378
updateInputWidthAndArrowLeft(): void {
@@ -407,6 +411,12 @@ export class NzDatePickerComponent implements OnInit, OnChanges, OnDestroy, Afte
407411
}
408412
}
409413

414+
onMouseDown(event: Event): void {
415+
if ((event.target as HTMLInputElement).tagName.toLowerCase() !== 'input') {
416+
event.preventDefault();
417+
}
418+
}
419+
410420
onFocus(event: FocusEvent, partType?: RangePartType): void {
411421
event.preventDefault();
412422
if (partType) {
@@ -697,6 +707,7 @@ export class NzDatePickerComponent implements OnInit, OnChanges, OnDestroy, Afte
697707
ngOnDestroy(): void {
698708
this.destroyed$.next();
699709
this.destroyed$.complete();
710+
this.elementRef.nativeElement.removeEventListener('mousedown', this.onMouseDown);
700711
}
701712

702713
setModeAndFormat(): void {

‎components/date-picker/range-picker.component.spec.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,17 @@ describe('NzRangePickerComponent', () => {
140140
expect(getPickerContainer()).not.toBeNull();
141141
}));
142142

143+
it('should execute default on the mousedown event when mouse down in date picker input', fakeAsync(() => {
144+
fixture.detectChanges();
145+
openPickerByClickTrigger();
146+
147+
const event = new MouseEvent('mousedown');
148+
spyOn(event, 'preventDefault').and.callThrough();
149+
fixture.nativeElement.querySelector(`.${PREFIX_CLASS}-separator`).dispatchEvent(event);
150+
151+
expect(event.preventDefault).not.toHaveBeenCalled();
152+
}));
153+
143154
it('should support nzAllowClear and work properly', fakeAsync(() => {
144155
const clearBtnSelector = By.css(`.${PREFIX_CLASS}-clear`);
145156
const initial = (fixtureInstance.modelValue = [new Date(), new Date()]);

0 commit comments

Comments
 (0)