Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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