Skip to content

Instantly share code, notes, and snippets.

@dok
Created March 20, 2017 06:29
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 dok/69716aa81b6938fa20d9fcbbf963964d to your computer and use it in GitHub Desktop.
Save dok/69716aa81b6938fa20d9fcbbf963964d to your computer and use it in GitHub Desktop.
simple react-input
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