Last active
December 27, 2022 03:50
-
-
Save kamikat/ca34166e2af6617d9b29a397b2e51c6f to your computer and use it in GitHub Desktop.
Workaround composition event problem in controlled React input components.
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
import { useEffect, useState, useRef } from "react"; | |
export function useAsyncInputRef<T extends HTMLElement & { value: string }>( | |
value: string, | |
forwardRef?: React.RefObject<T> | |
) { | |
const [compositionFlag, setComposition] = useState(false); | |
const newRef = useRef<T>(null); | |
const ref = forwardRef || newRef; | |
useEffect(() => { | |
const el = ref.current; | |
if (el) { | |
const compositionStart = () => setComposition(true); | |
const compositionEnd = () => setComposition(false); | |
el.addEventListener("compositionstart", compositionStart); | |
el.addEventListener("compositionend", compositionEnd); | |
return () => { | |
el.removeEventListener("compositionstart", compositionStart); | |
el.removeEventListener("compositionend", compositionEnd); | |
setComposition(false); | |
}; | |
} | |
}, [ref]); | |
useEffect(() => { | |
if (ref.current && compositionFlag === false) { | |
ref.current.value = value; | |
} | |
}, [compositionFlag, value, ref]); | |
return ref; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Live demo: https://codepen.io/kamikat/pen/OJwMJLr