« get me outta code hell

Un-selectable inputs - tui-lib - Pure Node.js library for making visual command-line programs (ala vim, ncdu)
about summary refs log tree commit diff
path: root/ui/form/Form.js
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2018-05-30 23:55:45 -0300
committerFlorrie <towerofnix@gmail.com>2018-05-30 23:55:45 -0300
commit553bbf486e043f206bb8f2c92c943fd688f8fedc (patch)
tree08366f0c8f9b0624f6b19d4e932fd286a1cab0f1 /ui/form/Form.js
parentf782f7395cd287d4767c1f3ffd20a72a51007595 (diff)
Un-selectable inputs
Diffstat (limited to 'ui/form/Form.js')
-rw-r--r--ui/form/Form.js26
1 files changed, 21 insertions, 5 deletions
diff --git a/ui/form/Form.js b/ui/form/Form.js
index ce064aa..e5d756a 100644
--- a/ui/form/Form.js
+++ b/ui/form/Form.js
@@ -59,6 +59,10 @@ module.exports = class Form extends FocusElement {
     }
   }
 
+  get selectable() {
+    return this.inputs.some(inp => inp.selectable)
+  }
+
   updateSelectedElement() {
     if (this.root.select) {
       this.root.select(this.inputs[this.curIndex])
@@ -66,16 +70,23 @@ module.exports = class Form extends FocusElement {
   }
 
   previousInput() {
-    this.curIndex = (this.curIndex - 1)
-    if (this.curIndex < 0) {
-      this.curIndex = (this.inputs.length - 1)
-    }
+    // TODO: Forms currently assume there is at least one selectable input,
+    // but this isn't necessarily always the case.
+    do {
+      this.curIndex = (this.curIndex - 1)
+      if (this.curIndex < 0) {
+        this.curIndex = (this.inputs.length - 1)
+      }
+    } while (!this.inputs[this.curIndex].selectable)
 
     this.updateSelectedElement()
   }
 
   nextInput() {
-    this.curIndex = (this.curIndex + 1) % this.inputs.length
+    // TODO: See previousInput
+    do {
+      this.curIndex = (this.curIndex + 1) % this.inputs.length
+    } while (!this.inputs[this.curIndex].selectable)
 
     this.updateSelectedElement()
   }
@@ -83,6 +94,11 @@ module.exports = class Form extends FocusElement {
   firstInput(selectForm = true) {
     this.curIndex = 0
 
+    // TODO: See previousInput
+    if (!this.inputs[this.curIndex].selectable) {
+      this.nextInput()
+    }
+
     if (selectForm || (
       this.root.isChildOrSelfSelected && this.root.isChildOrSelfSelected(this)
     )) {