Skip to content

Instantly share code, notes, and snippets.

@francisngo
Created December 30, 2017 00:51
Show Gist options
  • Save francisngo/f7c129844871b295283e7e9ec78bf576 to your computer and use it in GitHub Desktop.
Save francisngo/f7c129844871b295283e7e9ec78bf576 to your computer and use it in GitHub Desktop.
Collection for Redux todo example - This file is the presentational component for Input
import React, { Component } from 'react';
export default class Input extends Component {
constructor(props) {
super(props);
this.state = {
value: ''
};
this.handleChange = this.handleChange.bind(this);
this.handleKeyPress = this.handleKeyPress.bind(this);
}
handleChange(e) {
this.setState({ value: e.target.value });
}
handleKeyPress(e) {
const { handleSubmit } = this.props;
const { value } = this.state;
if (e.key !== 'Enter') return;
if (!value) return;
handleSubmit(value);
this.setState({ value: '' });
};
render() {
const { placeholder } = this.props;
const { value } = this.state;
return (
<input
type={"text"}
value={value}
placeholder={placeholder}
onChange={this.handleChange}
onKeyPress={this.handleKeyPress}
/>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment