Avoid cursor position change when maxLength is set

This commit is contained in:
Francisco Hodge 2018-10-06 13:35:33 -04:00
parent 9633c99dad
commit 0c773151c7
3 changed files with 16 additions and 4 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "simple-keyboard", "name": "simple-keyboard",
"version": "2.5.1", "version": "2.5.2",
"description": "On-screen Javascript Virtual Keyboard", "description": "On-screen Javascript Virtual Keyboard",
"main": "build/index.js", "main": "build/index.js",
"scripts": { "scripts": {

View File

@ -15,7 +15,8 @@ class App {
onChange: input => this.onChange(input), onChange: input => this.onChange(input),
onKeyPress: button => this.onKeyPress(button), onKeyPress: button => this.onKeyPress(button),
newLineOnEnter: true, newLineOnEnter: true,
physicalKeyboardHighlight: true physicalKeyboardHighlight: true,
maxLength: 5
}); });
/** /**

View File

@ -195,11 +195,14 @@ class Utilities {
output = [source.slice(0, position), string, source.slice(position)].join(''); output = [source.slice(0, position), string, source.slice(position)].join('');
/** /**
* Update caret position * Avoid caret position change when maxLength is set
*/ */
if(!this.isMaxLengthReached()){
this.updateCaretPos(string.length); this.updateCaretPos(string.length);
} }
}
return output; return output;
} }
@ -262,8 +265,10 @@ class Utilities {
} }
if(condition){ if(condition){
this.maxLengthReached = true;
return true; return true;
} else { } else {
this.maxLengthReached = false;
return false; return false;
} }
} }
@ -276,13 +281,19 @@ class Utilities {
} }
if(condition){ if(condition){
this.maxLengthReached = true;
return true; return true;
} else { } else {
this.maxLengthReached = false;
return false; return false;
} }
} }
} }
isMaxLengthReached = () => {
return Boolean(this.maxLengthReached);
}
camelCase = (string) => { camelCase = (string) => {
return string.toLowerCase().trim().split(/[.\-_\s]/g).reduce((string, word) => string + word[0].toUpperCase() + word.slice(1)); return string.toLowerCase().trim().split(/[.\-_\s]/g).reduce((string, word) => string + word[0].toUpperCase() + word.slice(1));
}; };