Skip to content

Commit

Permalink
fix(module:auto-complete): not emit changes when retype same value wh…
Browse files Browse the repository at this point in the history
…ile open (#4215)

* fix(module:auto-complete): not emit changes when retype same value while open

* test(module:auto-complete): add test
  • Loading branch information
hsuanxyz authored and vthinkxie committed Oct 12, 2019
1 parent 63f947e commit 21e91e3
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
18 changes: 13 additions & 5 deletions components/auto-complete/nz-autocomplete-trigger.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,15 +178,20 @@ export class NzAutocompleteTriggerDirective implements ControlValueAccessor, OnD

handleInput(event: KeyboardEvent): void {
const target = event.target as HTMLInputElement;
const document = this.document as Document;
let value: number | string | null = target.value;

if (this.canOpen() && document.activeElement === target && this.previousValue !== value) {
if (target.type === 'number') {
value = value === '' ? null : parseFloat(value);
}
if (target.type === 'number') {
value = value === '' ? null : parseFloat(value);
}

if (this.previousValue !== value) {
this.previousValue = value;
this._onChange(value);
this.openPanel();

if (this.canOpen() && document.activeElement === event.target) {
this.openPanel();
}
}
}

Expand Down Expand Up @@ -345,6 +350,9 @@ export class NzAutocompleteTriggerDirective implements ControlValueAccessor, OnD

private setTriggerValue(value: string | number | null): void {
this.elementRef.nativeElement.value = value || '';
if (!this.nzAutocomplete.nzBackfill) {
this.previousValue = value;
}
}

private doBackfill(): void {
Expand Down
36 changes: 36 additions & 0 deletions components/auto-complete/nz-autocomplete.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ describe('auto-complete', () => {
expect(overlayContainerElement.textContent).toContain('Burns Bay Road');
});

it('should open the panel when type', () => {
expect(fixture.componentInstance.trigger.panelOpen).toBe(false);
typeInElement('value', input);
fixture.detectChanges();
expect(fixture.componentInstance.trigger.panelOpen).toBe(true);
});

it('should not open the panel on focus if the input is readonly', fakeAsync(() => {
const trigger = fixture.componentInstance.trigger;
input.readOnly = true;
Expand Down Expand Up @@ -411,6 +418,12 @@ describe('auto-complete', () => {
flush();

expect(fixture.componentInstance.inputControl.value).toBe(200);

typeInElement('', input);
fixture.detectChanges();
flush();

expect(fixture.componentInstance.inputControl.value).toBe(null);
}));

it('should mark the autocomplete control as touched on blur', fakeAsync(() => {
Expand All @@ -423,6 +436,29 @@ describe('auto-complete', () => {
flush();
expect(fixture.componentInstance.inputControl.touched).toBe(true);
}));

it('should be able to re-type the same value when it is reset while open', fakeAsync(() => {
fixture.componentInstance.trigger.openPanel();
fixture.detectChanges();
flush();
fixture.detectChanges();

typeInElement('Burns', input);
flush();
fixture.detectChanges();

expect(fixture.componentInstance.inputControl.value).toBe('Burns');

fixture.componentInstance.inputControl.setValue('');
fixture.detectChanges();
expect(input.value).toBe('');

typeInElement('Burns', input);
flush();
fixture.detectChanges();

expect(fixture.componentInstance.inputControl.value).toBe('Burns');
}));
});

describe('form', () => {
Expand Down

0 comments on commit 21e91e3

Please sign in to comment.