Created
October 11, 2023 13:51
-
-
Save yinkakun/0fde7fe77123b892d1c2647a5c14aa35 to your computer and use it in GitHub Desktop.
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
| import React from 'react'; | |
| type Props = React.ComponentProps<'input'> & {}; | |
| export const NumericInput = React.forwardRef<HTMLInputElement, Props>( | |
| ({ className, onChange, ...props }, forwardedRef) => { | |
| const handleInput = (event: React.ChangeEvent<HTMLInputElement>) => { | |
| const numericValue = event.currentTarget.value.replace(/[^0-9]/g, ''); | |
| event.currentTarget.value = numericValue; | |
| if (onChange) { | |
| onChange(event); | |
| } | |
| }; | |
| return <input type="text" ref={forwardedRef} className={className} onInput={handleInput} {...props} />; | |
| }, | |
| ); | |
| NumericInput.displayName = 'NumericInput'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment