From: Bill Erickson <berickxx@gmail.com>
Date: Fri, 16 Aug 2019 20:52:50 +0000 (-0400)
Subject: LP1840050 Combobox responds favoribly to value changes
X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=028e70f6916a872a0b7b8c0fd73cbae70ed03648;p=evergreen%2Fjoelewis.git

LP1840050 Combobox responds favoribly to value changes

Using the power of getters/setters, teach the combobox to respond to
updates of the selected entry, particulary when the selected value is
applied before the set of underlying entries is modified.

Signed-off-by: Bill Erickson <berickxx@gmail.com>
Signed-off-by: Galen Charlton <gmc@equinoxinitiative.org>
---

diff --git a/Open-ILS/src/eg2/src/app/share/combobox/combobox.component.ts b/Open-ILS/src/eg2/src/app/share/combobox/combobox.component.ts
index 85225faa41..c98da6cd93 100644
--- a/Open-ILS/src/eg2/src/app/share/combobox/combobox.component.ts
+++ b/Open-ILS/src/eg2/src/app/share/combobox/combobox.component.ts
@@ -70,6 +70,26 @@ export class ComboboxComponent implements ControlValueAccessor, OnInit {
     @Input() startId: any = null;
     @Input() startIdFiresOnChange: boolean;
 
+    // Allow the selected entry ID to be passed via the template
+    // This does NOT not emit onChange events.
+    @Input() set selectedId(id: any) {
+        if (id) {
+            if (this.entrylist.length) {
+                this.selected = this.entrylist.filter(e => e.id === id)[0];
+            }
+
+            if (!this.selected) {
+                // It's possible the selected ID lives in a set of entries
+                // that are yet to be provided.
+                this.startId = id;
+            }
+        }
+    }
+
+    get selectedId(): any {
+        return this.selected ? this.selected.id : null;
+    }
+
     @Input() idlClass: string;
     @Input() idlField: string;
     @Input() idlIncludeLibraryInLabel: string;
@@ -90,6 +110,9 @@ export class ComboboxComponent implements ControlValueAccessor, OnInit {
     @Input() set entries(el: ComboboxEntry[]) {
         if (el) {
             this.entrylist = el;
+
+            // new set of entries essentially means a new instance. reset.
+            this.defaultSelectionApplied = false;
             this.applySelection();
 
             // It's possible to provide an entrylist at load time, but
@@ -212,6 +235,7 @@ export class ComboboxComponent implements ControlValueAccessor, OnInit {
 
     // Manually set the selected value by ID.
     // This does NOT fire the onChange handler.
+    // DEPRECATED: use this.selectedId = abc or [selectedId]="abc" instead.
     applyEntryId(entryId: any) {
         this.selected = this.entrylist.filter(e => e.id === entryId)[0];
     }