SVGHMI: Keypad is now keeping Javascript type constency. In other words, if a number was given as initial value, input value will have to convert to number in order to be valid. In case invalid value is entered, initial value is restored. svghmi
authorEdouard Tisserant
Fri, 21 Aug 2020 13:22:54 +0200
branchsvghmi
changeset 3033 52f6548982d4
parent 3032 2f6dfb99d094
child 3034 793ce2117258
SVGHMI: Keypad is now keeping Javascript type constency. In other words, if a number was given as initial value, input value will have to convert to number in order to be valid. In case invalid value is entered, initial value is restored.
svghmi/widget_keypad.ysl2
--- a/svghmi/widget_keypad.ysl2	Thu Aug 20 14:12:49 2020 +0200
+++ b/svghmi/widget_keypad.ysl2	Fri Aug 21 13:22:54 2020 +0200
@@ -88,9 +88,15 @@
          }
 
          on_Enter_click() {
-             end_modal.call(this);
-             let callback_obj = this.result_callback_obj;
-             callback_obj.edit_callback(this.editstr);
+             let coercedval = (typeof this.initial) == "number" ? Number(this.editstr) : this.editstr;
+             if(isNaN(coercedval)){
+                 this.editstr = String(this.initial);
+                 this.update();
+             } else { // revert to initial so it explicitely shows input was ignored
+                 let callback_obj = this.result_callback_obj;
+                 end_modal.call(this);
+                 callback_obj.edit_callback(coercedval);
+             }
          }
 
          on_BackSpace_click() {
@@ -137,11 +143,13 @@
          result_callback_obj = undefined;
          start_edit(info, valuetype, callback_obj, initial,size) {
              show_modal.call(this,size);
-             this.editstr = initial;
+             this.editstr = String(initial);
              this.result_callback_obj = callback_obj;
              this.Info_elt.textContent = info;
              this.shift = false;
              this.caps = false;
+             this.initial = initial;
+
              this.update();
          }