« get me outta code hell

Add removeInput method and captureTab property to Form - 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
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2017-12-09 17:22:56 -0400
committerFlorrie <towerofnix@gmail.com>2017-12-09 17:22:56 -0400
commita800730b9d8d098214f76860422e437514adf35f (patch)
treed7e5d79b2506738afc0019a6357f620430c6fb13 /ui/form
parent39b4468c2757d6d7a9416099b21030b09f850280 (diff)
Add removeInput method and captureTab property to Form
Diffstat (limited to 'ui/form')
-rw-r--r--ui/form/Form.js36
1 files changed, 33 insertions, 3 deletions
diff --git a/ui/form/Form.js b/ui/form/Form.js
index cf94978..708de1f 100644
--- a/ui/form/Form.js
+++ b/ui/form/Form.js
@@ -8,6 +8,7 @@ module.exports = class Form extends FocusElement {
 
     this.inputs = []
     this.curIndex = 0
+    this.captureTab = true
   }
 
   addInput(input, asChild = true) {
@@ -22,7 +23,26 @@ module.exports = class Form extends FocusElement {
     }
   }
 
+  removeInput(input, asChild = true) {
+    // Removes the given input from the form's input list. If the optional
+    // argument asChild is false, it won't try to removeChild the input.
+
+    if (this.inputs.includes(input)) {
+      this.inputs.splice(this.inputs.indexOf(input), 1)
+
+      if (asChild) {
+        this.removeChild(input)
+      }
+    }
+  }
+
   keyPressed(keyBuf) {
+    // Don't do anything if captureTab is set to false. This is handy for
+    // nested forms.
+    if (!this.captureTab) {
+      return
+    }
+
     if (telc.isTab(keyBuf) || telc.isBackTab(keyBuf)) {
       // No inputs to tab through, so do nothing.
       if (this.inputs.length < 2) {
@@ -40,7 +60,9 @@ module.exports = class Form extends FocusElement {
   }
 
   updateSelectedElement() {
-    this.root.select(this.inputs[this.curIndex])
+    if (this.root.select) {
+      this.root.select(this.inputs[this.curIndex])
+    }
   }
 
   previousInput() {
@@ -57,8 +79,16 @@ module.exports = class Form extends FocusElement {
 
     this.updateSelectedElement()
   }
-  
+
+  firstInput() {
+    this.curIndex = 0
+
+    this.updateSelectedElement()
+  }
+
   focused() {
-    this.root.select(this.inputs[this.curIndex])
+    if (this.root.select) {
+      this.root.select(this.inputs[this.curIndex])
+    }
   }
 }