From 7b06f7e1a8aa2589771c3c9cc7528b6068d4eed4 Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Mon, 24 Oct 2022 11:59:27 +0200 Subject: [PATCH] input: add maxLength property --- CHANGELOG.md | 5 +++++ src/client/widgets/basics/input.js | 13 +++++++++++++ 2 files changed, 18 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8e5ae647..e3881532 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## 1.18.4 + +- widgets + - input: add `maxLength` property + ## 1.18.3 - bug fixes diff --git a/src/client/widgets/basics/input.js b/src/client/widgets/basics/input.js index ce82b469..9063d19a 100644 --- a/src/client/widgets/basics/input.js +++ b/src/client/widgets/basics/input.js @@ -28,6 +28,7 @@ class Input extends Canvas { '- `^[0-9]*$` accepts digits only, any number of them', '- `/^[a-z\s]{0,10}$/i` accept between 0 and 10 alphabetic characters and spaces (case insensitive)', ]}, + maxLength: {type: 'number', value: '', help: ['Maximum number of characters allowed']} }, }) @@ -66,6 +67,13 @@ class Input extends Canvas { this.input.inputMode = 'decimal' } + if (this.getProp('maxLength') !== '') { + this.maxLength = parseInt(this.getProp('maxLength')) + this.input.maxLength = this.maxLength + } else { + this.maxLength = -1 + } + var asYouType = this.getProp('asYouType') if (this.getProp('validation') !== '') { @@ -156,6 +164,11 @@ class Input extends Canvas { return } + if (this.maxLength !== -1 && this.getStringValue(v).length > this.maxLength) { + v = this.getStringValue(v).substr(0, this.maxLength) + this.input.value = v + } + try { this.value = JSON.parse(v) } catch (err) {