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): fix datePicker show multi panel #7680

Merged
merged 1 commit into from Oct 17, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions components/date-picker/date-picker.component.spec.ts
Expand Up @@ -221,6 +221,28 @@ describe('NzDatePickerComponent', () => {
expect(getPickerContainer()).not.toBeNull();
}));

it('should prevent default on the mousedown event when mouse down in date picker', fakeAsync(() => {
fixture.detectChanges();
openPickerByClickTrigger();

const event = new MouseEvent('mousedown');
spyOn(event, 'preventDefault').and.callThrough();
fixture.nativeElement.querySelector(`.${PREFIX_CLASS}`).dispatchEvent(event);

expect(event.preventDefault).toHaveBeenCalled();
}));

it('should execute default on the mousedown event when mouse down in date picker input', fakeAsync(() => {
fixture.detectChanges();
openPickerByClickTrigger();

const event = new MouseEvent('mousedown');
spyOn(event, 'preventDefault').and.callThrough();
fixture.nativeElement.querySelector(`.${PREFIX_CLASS} input`).dispatchEvent(event);

expect(event.preventDefault).not.toHaveBeenCalled();
}));

it('should support nzAllowClear and work properly', fakeAsync(() => {
const clearBtnSelector = By.css(`.${PREFIX_CLASS}-clear`);
const initial = (fixtureInstance.nzValue = new Date());
Expand Down
11 changes: 11 additions & 0 deletions components/date-picker/date-picker.component.ts
Expand Up @@ -369,6 +369,10 @@ export class NzDatePickerComponent implements OnInit, OnChanges, OnDestroy, Afte
this.focus();
this.updateInputWidthAndArrowLeft();
});

// prevent mousedown event to trigger focusout event when click in date picker
// see: https://github.com/NG-ZORRO/ng-zorro-antd/issues/7450
this.elementRef.nativeElement.addEventListener('mousedown', this.onMouseDown);
wenqi73 marked this conversation as resolved.
Show resolved Hide resolved
}

updateInputWidthAndArrowLeft(): void {
Expand Down Expand Up @@ -407,6 +411,12 @@ export class NzDatePickerComponent implements OnInit, OnChanges, OnDestroy, Afte
}
}

onMouseDown(event: Event): void {
if ((event.target as HTMLInputElement).tagName.toLowerCase() !== 'input') {
event.preventDefault();
}
}

onFocus(event: FocusEvent, partType?: RangePartType): void {
event.preventDefault();
if (partType) {
Expand Down Expand Up @@ -697,6 +707,7 @@ export class NzDatePickerComponent implements OnInit, OnChanges, OnDestroy, Afte
ngOnDestroy(): void {
this.destroyed$.next();
this.destroyed$.complete();
this.elementRef.nativeElement.removeEventListener('mousedown', this.onMouseDown);
}

setModeAndFormat(): void {
Expand Down
11 changes: 11 additions & 0 deletions components/date-picker/range-picker.component.spec.ts
Expand Up @@ -140,6 +140,17 @@ describe('NzRangePickerComponent', () => {
expect(getPickerContainer()).not.toBeNull();
}));

it('should execute default on the mousedown event when mouse down in date picker input', fakeAsync(() => {
fixture.detectChanges();
openPickerByClickTrigger();

const event = new MouseEvent('mousedown');
spyOn(event, 'preventDefault').and.callThrough();
fixture.nativeElement.querySelector(`.${PREFIX_CLASS}-separator`).dispatchEvent(event);

expect(event.preventDefault).not.toHaveBeenCalled();
}));

it('should support nzAllowClear and work properly', fakeAsync(() => {
const clearBtnSelector = By.css(`.${PREFIX_CLASS}-clear`);
const initial = (fixtureInstance.modelValue = [new Date(), new Date()]);
Expand Down