Created
December 30, 2017 00:51
-
-
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
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 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