Skip to content

Instantly share code, notes, and snippets.

@yinkakun
Created October 11, 2023 13:51
Show Gist options
  • Select an option

  • Save yinkakun/0fde7fe77123b892d1c2647a5c14aa35 to your computer and use it in GitHub Desktop.

Select an option

Save yinkakun/0fde7fe77123b892d1c2647a5c14aa35 to your computer and use it in GitHub Desktop.
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