Created
May 3, 2019 12:35
-
-
Save gossi/1bf5b71d474c413bad115b8bd1c7ef8c to your computer and use it in GitHub Desktop.
Event Support: normalize-input-event
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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