Skip to content

Commit

Permalink
feat(typeahead): add support for the "editable" option
Browse files Browse the repository at this point in the history
Closes #788

Closes #789
  • Loading branch information
pkozlowski-opensource committed Sep 23, 2016
1 parent 0b2251d commit cb91905
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/typeahead/typeahead-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ import {Injectable} from '@angular/core';
*/
@Injectable()
export class NgbTypeaheadConfig {
editable = true;
showHint = false;
}
27 changes: 27 additions & 0 deletions src/typeahead/typeahead.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,33 @@ describe('ngb-typeahead', () => {
expect(getNativeInput(compiled).disabled).toBeTruthy();
});
}));

it('should only propagate model changes on select when the editable option is on', async(() => {
const html = `
<form>
<input type="text" [(ngModel)]="model" name="control" required [ngbTypeahead]="find" [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, 'o');
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');
});
}));
});

describe('select event', () => {
Expand Down
11 changes: 10 additions & 1 deletion src/typeahead/typeahead.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ export class NgbTypeahead implements ControlValueAccessor,
private _windowRef: ComponentRef<NgbTypeaheadWindow>;
private _zoneSubscription: any;


/**
* A flag indicating if model values should be restricted to the ones selected from the popup only.
*/
@Input() editable: boolean;

/**
* A function to convert a given value into string to display in the input field
*/
Expand Down Expand Up @@ -117,6 +123,7 @@ export class NgbTypeahead implements ControlValueAccessor,
private _elementRef: ElementRef, private _viewContainerRef: ViewContainerRef, private _renderer: Renderer,
private _injector: Injector, componentFactoryResolver: ComponentFactoryResolver, config: NgbTypeaheadConfig,
ngZone: NgZone) {
this.editable = config.editable;
this.showHint = config.showHint;

this._valueChanges = Observable.fromEvent(_elementRef.nativeElement, 'input', ($event) => $event.target.value);
Expand All @@ -136,7 +143,9 @@ export class NgbTypeahead implements ControlValueAccessor,
this._valueChanges
.do(value => {
this._userInput = value;
this._onChange(value);
if (this.editable) {
this._onChange(value);
}
})
.let (this.ngbTypeahead));
}
Expand Down

0 comments on commit cb91905

Please sign in to comment.