Skip to content

Instantly share code, notes, and snippets.

@gossi
Created May 3, 2019 12:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gossi/1bf5b71d474c413bad115b8bd1c7ef8c to your computer and use it in GitHub Desktop.
Save gossi/1bf5b71d474c413bad115b8bd1c7ef8c to your computer and use it in GitHub Desktop.
Event Support: normalize-input-event
export const normalizeInputEvent = function(event: KeyboardEvent | InputEvent): CompatibleInputEvent {
const e: CompatibleInputEvent = {
originalEvent: event
};
if (event instanceof KeyboardEvent) {
if (event.key === 'Backspace') {
e.inputType = 'deleteContentBackward';
e.navigationType = 'cursorLeft';
} else if (event.key === 'Delete') {
e.inputType = 'deleteContentForward';
} else if (event.key.startsWith('Arrow')) {
e.navigationType = event.key.replace('Arrow', 'cursor');
} else {
e.data = event.key;
e.inputType = 'insertText';
}
} else {
// @ts-ignore event.inputType is there on android - actually what we need here!
const { inputType } = event;
e.inputType = inputType;
e.data = event.data;
if (inputType === 'insertText') {
e.navigationType = 'cursorRight';
}
}
return e;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment