Skip to content

Instantly share code, notes, and snippets.

@everdimension
Created February 5, 2018 11:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save everdimension/a8f759da91d5073a6d9edc5a55eeea01 to your computer and use it in GitHub Desktop.
Save everdimension/a8f759da91d5073a6d9edc5a55eeea01 to your computer and use it in GitHub Desktop.
import * as React from 'react';
interface Props {
value: string | number;
name?: string;
onChange(name: string, value: number): void;
}
interface State {
value: string;
}
class NumericInput extends React.Component<
Partial<React.HTMLProps<HTMLInputElement>> & Props,
State,
> {
constructor(props: Props & Partial<React.HTMLProps<HTMLInputElement>>) {
super(props);
this.state = {
value: String(props.value),
};
this.handleChange = this.handleChange.bind(this);
}
handleChange(event: React.FormEvent<HTMLInputElement>) {
this.props.onChange(
event.currentTarget.name,
Number(event.currentTarget.value),
);
}
render() {
const { value, onChange /* just to exclude */, ...htmlProps } = this.props;
return (
<input
type="text"
value={value}
onChange={this.handleChange}
{...htmlProps}
/>
);
}
}
export default NumericInput;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment