Created
May 28, 2024 11:42
-
-
Save sagarpanchal/aea8c5fe06da96df4924e0ed2f107cdc to your computer and use it in GitHub Desktop.
React: useNumberInput
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
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