Skip to content

Commit

Permalink
Backspace implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
cagataycivici committed Apr 14, 2020
1 parent 86c6e04 commit 343c661
Showing 1 changed file with 30 additions and 14 deletions.
44 changes: 30 additions & 14 deletions src/components/inputnumber/InputNumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,19 +252,19 @@ export class InputNumber extends Component {
}
}

onDownButtonMouseUp(event) {
onDownButtonMouseUp() {
if (!this.props.disabled) {
this.clearTimer();
}
}

onDownButtonMouseLeave(event) {
onDownButtonMouseLeave() {
if (!this.props.disabled) {
this.clearTimer();
}
}

onDownButtonKeyUp(event) {
onDownButtonKeyUp() {
if (!this.props.disabled) {
this.clearTimer();
}
Expand All @@ -278,6 +278,7 @@ export class InputNumber extends Component {

onInputKeyDown(event) {
let selectionStart = event.target.selectionStart;
let selectionEnd = event.target.selectionEnd;
let inputValue = event.target.value;

switch (event.which) {
Expand Down Expand Up @@ -309,22 +310,37 @@ export class InputNumber extends Component {

//backspace
case 8:
let removeCharIndex = selectionStart - 1;
let deleteChar = inputValue.charAt(removeCharIndex);
event.preventDefault();
let newValueStr;

if (selectionStart === selectionEnd) {
let removeCharIndex = selectionStart - 1;
let deleteChar = inputValue.charAt(removeCharIndex);

if (this.isNumeralChar(deleteChar)) {
if (this._group.test(deleteChar)) {
this._group.lastIndex = 0;
removeCharIndex = selectionStart - 2;
}

if (this.isNumeralChar(deleteChar)) {
if (this._group.test(deleteChar)) {
this._group.lastIndex = 0;
removeCharIndex = selectionStart - 2;
newValueStr = inputValue.slice(0, removeCharIndex) + inputValue.slice(removeCharIndex + 1);
}
}
else {
if ((selectionEnd - selectionStart) === inputValue.length)
newValueStr = '';
else if (selectionStart === 0)
newValueStr = inputValue.slice(selectionEnd)
else if (selectionEnd === inputValue.length)
newValueStr = inputValue.slice(0, selectionStart)
else
newValueStr = inputValue.slice(0, selectionStart) + inputValue.slice(selectionEnd);
}

let newValue = this.parseValue(inputValue.slice(0, removeCharIndex) + inputValue.slice(removeCharIndex + 1));
if (newValueStr != null) {
let newValue = this.parseValue(newValueStr);
this.inputEl.value = this.formatValue(newValue);
this.updateModel(event, newValue);
event.preventDefault();
}
else {
event.preventDefault();
}
break;

Expand Down

0 comments on commit 343c661

Please sign in to comment.