Skip to content

Instantly share code, notes, and snippets.

@alexng353
Created June 16, 2024 18:16
Show Gist options
  • Save alexng353/44171ec0b9101ea582002c0818b28ec5 to your computer and use it in GitHub Desktop.
Save alexng353/44171ec0b9101ea582002c0818b28ec5 to your computer and use it in GitHub Desktop.
Capture TAB in a react input
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;
}
}}
/>);
}
@alexng353
Copy link
Author

image
Here's an image of this component in action

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment