Created
June 16, 2024 18:16
-
-
Save alexng353/44171ec0b9101ea582002c0818b28ec5 to your computer and use it in GitHub Desktop.
Capture TAB in a react input
This file contains hidden or 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
function InputCaptureTabs() { | |
const [input, set_input] = useState(""); | |
return (<Input | |
multiline | |
placeholder="Type some code in" | |
value={users} | |
onChange={(event) => set_input(event.target.value)} | |
onKeyDown={(event) => { | |
if (event.key === "Tab") { | |
event.preventDefault(); | |
const start = event.currentTarget.selectionStart; | |
const end = event.currentTarget.selectionEnd; | |
event.currentTarget.value = | |
event.currentTarget.value.slice(0, Math.max(0, start)) + | |
" " + | |
event.currentTarget.value.slice(Math.max(0, end)); | |
event.currentTarget.selectionStart = start + 4; | |
event.currentTarget.selectionEnd = start + 4; | |
} | |
}} | |
/>); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's an image of this component in action