Created
March 20, 2017 06:29
-
-
Save dok/69716aa81b6938fa20d9fcbbf963964d to your computer and use it in GitHub Desktop.
simple react-input
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, { Component } from 'react'; | |
export default class Input extends Component { | |
props: { | |
value: React.PropTypes.string.isRequired | |
}; | |
constructor(props) { | |
super(props); | |
this.state = { | |
value: props.value | |
}; | |
} | |
onKeyPress(event) { | |
if (event.charCode === 13 || event.keyCode === 13) { | |
this.props.onEnter(event); | |
} | |
} | |
onChange(event) { | |
this.setState({ | |
value: event.target.value | |
}); | |
} | |
render() { | |
const { className, placeholder, type } = this.props; | |
const { value } = this.state; | |
return ( | |
<input className={className} | |
placeholder={placeholder} | |
value={value} | |
type={type} | |
onChange={this.onChange.bind(this)} | |
onKeyPress={this.onKeyPress.bind(this)}/> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment