From 21d1a6724eadcf91a616beb06ad763e7e5fafcdc Mon Sep 17 00:00:00 2001 From: Florrie Date: Sat, 9 Dec 2017 17:24:19 -0400 Subject: Add setValue and moveToEnd methods to TextInput --- ui/form/TextInput.js | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'ui/form') diff --git a/ui/form/TextInput.js b/ui/form/TextInput.js index a10a26f..11e4060 100644 --- a/ui/form/TextInput.js +++ b/ui/form/TextInput.js @@ -82,6 +82,16 @@ module.exports = class TextInput extends FocusElement { this.keepCursorInRange() } + setValue(value) { + this.value = value + this.moveToEnd() + } + + moveToEnd() { + this.cursorIndex = this.value.length + this.keepCursorInRange() + } + keepCursorInRange() { // Keep the cursor inside or at the end of the input value. @@ -95,18 +105,18 @@ module.exports = class TextInput extends FocusElement { // Scroll right, if the cursor is past the right edge of where text is // displayed. - if (this.cursorIndex - this.scrollChars > this.w - 3) { + while (this.cursorIndex - this.scrollChars > this.w - 3) { this.scrollChars++ } // Scroll left, if the cursor is behind the left edge of where text is // displayed. - if (this.cursorIndex - this.scrollChars < 0) { + while (this.cursorIndex - this.scrollChars < 0) { this.scrollChars-- } // Scroll left, if we can see past the end of the text. - if (this.scrollChars > 0 && ( + while (this.scrollChars > 0 && ( this.scrollChars + this.w - 3 > this.value.length) ) { this.scrollChars-- -- cgit 1.3.0-6-gf8a5