Skip to content

Commit

Permalink
Simplify backspace implementation
Browse files Browse the repository at this point in the history
We don't need full backspace handling yet,
and it was hurting code coverage
  • Loading branch information
wachunga committed Mar 11, 2020
1 parent e73333e commit 165929d
Showing 1 changed file with 1 addition and 25 deletions.
26 changes: 1 addition & 25 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,30 +105,6 @@ function fireChangeEvent(event) {
event.target.removeEventListener("blur", fireChangeEvent);
}

function getSelectionBounds(element) {
return {
start: element.selectionStart,
end: element.selectionEnd
};
}

function deleteLeftOfCursor(element) {
const { start, end } = getSelectionBounds(element);
if (start === end) {
if (start === 0) {
// there's nothing left of cursor
return;
}
element.setSelectionRange(start - 1, end);
}

const value = element.value || "";
const updatedValue = value.substring(0, start) + value.substring(end);
element.value = updatedValue;

element.setSelectionRange(start, start);
}

const Keys = {
Backspace: { keyCode: 8, code: "Backspace", key: "Backspace" }
};
Expand All @@ -146,7 +122,7 @@ function backspace(element) {
fireEvent.input(element, {
inputType: "deleteContentBackward"
});
deleteLeftOfCursor(element);
element.value = ""; // when we add special keys to API, will need to respect selected range
}
}

Expand Down

0 comments on commit 165929d

Please sign in to comment.