LP1831390: Fixing implementation of registerOnTouch user/berick/lp1831390-ang-form-control-value-accessor-2
authorJane Sandberg <sandbej@linnbenton.edu>
Mon, 8 Jul 2019 13:44:53 +0000 (06:44 -0700)
committerBill Erickson <berickxx@gmail.com>
Wed, 17 Jul 2019 19:33:34 +0000 (15:33 -0400)
This commit ensures that the onTouch callback is called on the blur
event, per the official Angular documentation.

Also improves the display of default values in the datepicker

Signed-off-by: Jane Sandberg <sandbej@linnbenton.edu>
Signed-off-by: Bill Erickson <berickxx@gmail.com>
Open-ILS/src/eg2/src/app/share/combobox/combobox.component.ts
Open-ILS/src/eg2/src/app/share/date-select/date-select.component.html
Open-ILS/src/eg2/src/app/share/date-select/date-select.component.ts

index 79042c6..d391244 100644 (file)
@@ -97,8 +97,9 @@ export class ComboboxComponent implements ControlValueAccessor, OnInit {
     // and display.  Default version trims leading/trailing spaces.
     formatDisplayString: (e: ComboboxEntry) => string;
 
-    // Stub function required by ControlValueAccessor
+    // Stub functions required by ControlValueAccessor
     propagateChange = (_: any) => {};
+    propagateTouch = () => {};
 
     constructor(
       private elm: ElementRef,
@@ -120,14 +121,12 @@ export class ComboboxComponent implements ControlValueAccessor, OnInit {
     }
 
     onClick($event) {
-        this.registerOnTouched();
         this.click$.next($event.target.value);
     }
 
     openMe($event) {
         // Give the input a chance to focus then fire the click
         // handler to force open the typeahead
-        this.registerOnTouched();
         this.elm.nativeElement.getElementsByTagName('input')[0].focus();
         setTimeout(() => this.click$.next(''));
     }
@@ -197,6 +196,7 @@ export class ComboboxComponent implements ControlValueAccessor, OnInit {
             this.selectorChanged(
                 {item: this.selected, preventDefault: () => true});
         }
+        this.propagateTouch();
     }
 
     // Fired by the typeahead to inform us of a change.
@@ -277,7 +277,9 @@ export class ComboboxComponent implements ControlValueAccessor, OnInit {
         this.propagateChange = fn;
     }
 
-    registerOnTouched() { }
+    registerOnTouched(fn) {
+        this.propagateTouch = fn;
+    }
 
 }
 
index 7e65f76..06b31ef 100644 (file)
@@ -10,6 +10,7 @@
     name="{{fieldName}}"
     [disabled]="_disabled"
     [required]="required"
+    (blur)="propagateTouch()"
     [(ngModel)]="current"
     (dateSelect)="onDateSelect($event)"/>
   <div class="input-group-append">
index d4802e1..4aa1780 100644 (file)
@@ -37,8 +37,9 @@ export class DateSelectComponent implements OnInit, ControlValueAccessor {
     @Output() onChangeAsIso: EventEmitter<string>;
     @Output() onChangeAsYmd: EventEmitter<string>;
 
-    // Stub function required by ControlValueAccessor
+    // Stub functions required by ControlValueAccessor
     propagateChange = (_: any) => {};
+    propagateTouch = () => {};
 
     constructor() {
         this.onChangeAsDate = new EventEmitter<Date>();
@@ -56,11 +57,7 @@ export class DateSelectComponent implements OnInit, ControlValueAccessor {
         }
 
         if (this.initialDate) {
-            this.current = {
-                year: this.initialDate.getFullYear(),
-                month: this.initialDate.getMonth() + 1,
-                day: this.initialDate.getDate()
-            };
+            this.writeValue(this.initialDate);
         }
     }
 
@@ -83,8 +80,12 @@ export class DateSelectComponent implements OnInit, ControlValueAccessor {
     }
 
     writeValue(value: Date) {
-        if (value !== undefined) {
-            this.initialDate = value;
+        if (value) {
+            this.current = {
+                year: value.getFullYear(),
+                month: value.getMonth() + 1,
+                day: value.getDate()
+            };
         }
     }
 
@@ -92,7 +93,9 @@ export class DateSelectComponent implements OnInit, ControlValueAccessor {
         this.propagateChange = fn;
     }
 
-    registerOnTouched() { }
+    registerOnTouched(fn) {
+        this.propagateTouch = fn;
+    }
 }