diff --git a/src/lib/components/Keyboard.js b/src/lib/components/Keyboard.js index 32f2ae21..0c750725 100644 --- a/src/lib/components/Keyboard.js +++ b/src/lib/components/Keyboard.js @@ -80,9 +80,17 @@ class SimpleKeyboard { if(!this.input[this.options.inputName]) this.input[this.options.inputName] = ''; - let updatedInput = Utilities.getUpdatedInput(button, this.input[this.options.inputName], options); + let updatedInput = this.utilities.getUpdatedInput(button, this.input[this.options.inputName], this.options, this.caretPosition); if(this.input[this.options.inputName] !== updatedInput){ + + /** + * If maxLength and handleMaxLength yield true, halting + */ + if(this.options.maxLength && this.utilities.handleMaxLength(this.input, this.options, updatedInput)){ + return false; + } + this.input[this.options.inputName] = updatedInput; if(debug) diff --git a/src/lib/services/Utilities.js b/src/lib/services/Utilities.js index d3ea71c2..49d642dc 100644 --- a/src/lib/services/Utilities.js +++ b/src/lib/services/Utilities.js @@ -171,7 +171,50 @@ class Utilities { return output; } - static camelCase = (string) => { + handleMaxLength(inputObj, options, updatedInput){ + let maxLength = options.maxLength; + let currentInput = inputObj[options.inputName]; + let condition = currentInput.length === maxLength; + + + if( + /** + * If pressing this button won't add more characters + * We exit out of this limiter function + */ + updatedInput.length <= currentInput.length + ){ + return false; + } + + if(Number.isInteger(maxLength)){ + if(options.debug){ + console.log("maxLength (num) reached:", condition); + } + + if(condition){ + return true; + } else { + return false; + } + } + + if(typeof maxLength === "object"){ + let condition = currentInput.length === maxLength[options.inputName]; + + if(options.debug){ + console.log("maxLength (obj) reached:", condition); + } + + if(condition){ + return true; + } else { + return false; + } + } + } + + camelCase = (string) => { return string.toLowerCase().trim().split(/[.\-_\s]/g).reduce((string, word) => string + word[0].toUpperCase() + word.slice(1)); };