From 82a12a77ef50d2dbff475668668d4b1cc5dcc0fc Mon Sep 17 00:00:00 2001 From: Bill Erickson Date: Sun, 1 Jul 2018 17:01:34 -0400 Subject: [PATCH] LP#1775466 Typeahead has allowFreeText option Signed-off-by: Bill Erickson --- .../app/share/typeahead/typeahead.component.html | 3 +- .../src/app/share/typeahead/typeahead.component.ts | 33 ++++++++++++++++++++-- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/Open-ILS/src/eg2/src/app/share/typeahead/typeahead.component.html b/Open-ILS/src/eg2/src/app/share/typeahead/typeahead.component.html index c47430a12f..879a02340c 100644 --- a/Open-ILS/src/eg2/src/app/share/typeahead/typeahead.component.html +++ b/Open-ILS/src/eg2/src/app/share/typeahead/typeahead.component.html @@ -13,8 +13,9 @@ [resultTemplate]="displayTemplate" [inputFormatter]="formatDisplayString" (click)="click$.next($event.target.value)" + (blur)="onBlur()" (selectItem)="selectorChanged($event)" - #input #instance="ngbTypeahead"/> + #instance="ngbTypeahead"/>
keyboard_arrow_up keyboard_arrow_down diff --git a/Open-ILS/src/eg2/src/app/share/typeahead/typeahead.component.ts b/Open-ILS/src/eg2/src/app/share/typeahead/typeahead.component.ts index 98e9047ef4..1c5afac2c7 100644 --- a/Open-ILS/src/eg2/src/app/share/typeahead/typeahead.component.ts +++ b/Open-ILS/src/eg2/src/app/share/typeahead/typeahead.component.ts @@ -13,7 +13,7 @@ import {StoreService} from '@eg/core/store.service'; export interface TypeaheadEntry { id: any; label: string; - disabled?: boolean; + freetext?: boolean; } @Component({ @@ -29,6 +29,7 @@ export class TypeaheadComponent implements OnInit { selected: TypeaheadEntry; click$: Subject; entrylist: TypeaheadEntry[]; + freeTextId: number; @ViewChild('instance') instance: NgbTypeahead; @@ -41,6 +42,8 @@ export class TypeaheadComponent implements OnInit { // box regardless of any text that already exists there. @Input() clickShowsAll = true; + @Input() allowFreeText = false; + // Entry ID of the default entry to select (optional) // onChange() is NOT fired when applying the default value @Input() startId: any; @@ -63,6 +66,7 @@ export class TypeaheadComponent implements OnInit { this.entrylist = []; this.click$ = new Subject(); this.onChange = new EventEmitter(); + this.freeTextId = -1; this.formatDisplayString = (result: TypeaheadEntry) => { return result.label.trim(); @@ -83,11 +87,34 @@ export class TypeaheadComponent implements OnInit { setTimeout(() => this.click$.next('')); } + onBlur() { + + if (typeof this.selected === 'string' && this.selected !== '') { + // Free text entered which does not match a known entry + + if (this.allowFreeText) { + // translate it into a dummy TypeaheadEntry + // and manually fire the onchange handler. + this.selected = { + id: this.freeTextId--, + label: this.selected, + freetext: true + } + this.selectorChanged( + {item: this.selected, preventDefault: () => true}); + } else { + // If free text is now allowed, clear the value when + // the user navigates away to avoid confusion. + this.selected = null; + } + } + } // Fired by the typeahead to inform us of a change. - // TODO: this does not fire when the value is cleared :( -- implement - // change detection on this.selected to look specifically for NULL. + // This only fires when an item in the list is selected, not when + // the value is cleared or free-text is used. selectorChanged(selEvent: NgbTypeaheadSelectItemEvent) { + console.log('selector changed'); this.onChange.emit(selEvent.item.id); } -- 2.11.0