Created
February 5, 2018 11:25
-
-
Save everdimension/a8f759da91d5073a6d9edc5a55eeea01 to your computer and use it in GitHub Desktop.
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
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