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(typeahead): for editable false clear the value on input change #3267

Merged
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
39 changes: 39 additions & 0 deletions src/typeahead/typeahead.spec.ts
Expand Up @@ -630,6 +630,39 @@ describe('ngb-typeahead', () => {
expect(fixture.componentInstance.model).toBeUndefined();
});
}));

it('should clear model on user input when the editable option is on and no search was triggered', async(() => {
const html = `
<form>
<input type="text" [(ngModel)]="model" name="control" required [ngbTypeahead]="findFilter" [editable]="false"/>
</form>`;
const fixture = createTestComponent(html);
fixture.whenStable().then(() => {
fixture.detectChanges();
const compiled = fixture.nativeElement;
expect(getNativeInput(compiled)).toHaveCssClass('ng-invalid');
expect(getNativeInput(compiled)).not.toHaveCssClass('ng-valid');

changeInput(compiled, 'one');
fixture.detectChanges();
expect(getNativeInput(compiled)).toHaveCssClass('ng-invalid');
expect(getNativeInput(compiled)).not.toHaveCssClass('ng-valid');
expect(fixture.componentInstance.model).toBeUndefined();

const event = createKeyDownEvent(Key.Enter);
getDebugInput(fixture.debugElement).triggerEventHandler('keydown', event);
fixture.detectChanges();
expect(getNativeInput(compiled)).not.toHaveCssClass('ng-invalid');
expect(getNativeInput(compiled)).toHaveCssClass('ng-valid');
expect(fixture.componentInstance.model).toBe('one');

changeInput(compiled, '');
fixture.detectChanges();
expect(getNativeInput(compiled)).toHaveCssClass('ng-invalid');
expect(getNativeInput(compiled)).not.toHaveCssClass('ng-valid');
expect(fixture.componentInstance.model).toBeUndefined();
});
}));
});

describe('select event', () => {
Expand Down Expand Up @@ -947,6 +980,12 @@ class TestComponent {
return this.findOutput$;
}

findFilter =
(text$: Observable<string>) => {
return text$.pipe(
filter(term => term.length > 1), map(text => this._strings.filter(v => v.indexOf(text) > -1)));
}

findAnywhere =
(text$: Observable<string>) => {
return text$.pipe(map(text => this._strings.filter(v => v.indexOf(text) > -1)));
Expand Down
11 changes: 2 additions & 9 deletions src/typeahead/typeahead.ts
Expand Up @@ -219,17 +219,10 @@ export class NgbTypeahead implements ControlValueAccessor,
ngOnInit(): void {
const inputValues$ = this._valueChanges.pipe(tap(value => {
this._inputValueBackup = this.showHint ? value : null;
if (this.editable) {
this._onChange(value);
}
this._onChange(this.editable ? value : undefined);
}));
const results$ = inputValues$.pipe(this.ngbTypeahead);
const processedResults$ = results$.pipe(tap(() => {
if (!this.editable) {
this._onChange(undefined);
}
}));
const userInput$ = this._resubscribeTypeahead.pipe(switchMap(() => processedResults$));
const userInput$ = this._resubscribeTypeahead.pipe(switchMap(() => results$));
this._subscription = this._subscribeToUserInput(userInput$);
}

Expand Down