Skip to content

Instantly share code, notes, and snippets.

@sagarpanchal
Created May 28, 2024 11:42
Show Gist options
  • Save sagarpanchal/aea8c5fe06da96df4924e0ed2f107cdc to your computer and use it in GitHub Desktop.
Save sagarpanchal/aea8c5fe06da96df4924e0ed2f107cdc to your computer and use it in GitHub Desktop.
React: useNumberInput
const useNumberInput = (ref: React.RefObject<HTMLInputElement>) => {
const stepUp = React.useCallback(() => {
if (!ref.current) return
ref.current.stepUp()
ref.current.dispatchEvent(new Event("change", { bubbles: true }))
}, [ref])
const stepDown = React.useCallback(() => {
if (!ref.current) return
ref.current.stepDown()
ref.current.dispatchEvent(new Event("change", { bubbles: true }))
}, [ref])
const getValue = React.useCallback(() => {
if (!ref.current) return
return ref.current.valueAsNumber
}, [ref])
return { stepUp, stepDown, getValue }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment