Created
June 18, 2021 06:32
-
-
Save chrisryana/51c75f9c40bace48b20ce8baca66a151 to your computer and use it in GitHub Desktop.
Автофокус на contentEditable ноду
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
// Ставит курсор в конец contentEditable ноды | |
const setCaretToEnd = () => { | |
const selection = window.getSelection(); | |
const range = document.createRange(); | |
const walletNameLength = inputRef.current.innerText.length; | |
range.setStart(window.getSelection().focusNode, walletNameLength); | |
range.collapse(true); | |
selection.removeAllRanges(); | |
selection.addRange(range); | |
}; | |
useEffect(() => { | |
if (editable && inputRef) { | |
inputRef.current.focus(); | |
} | |
}, [editable]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment