Skip to content

Instantly share code, notes, and snippets.

@rcdexta
Created October 9, 2018 02:46
Show Gist options
  • Save rcdexta/35ec7d991d4bbd535a9e7742ad0229b3 to your computer and use it in GitHub Desktop.
Save rcdexta/35ec7d991d4bbd535a9e7742ad0229b3 to your computer and use it in GitHub Desktop.
TodoForm.js
class TodoForm extends React.Component {
static propTypes = {
onAddItem: PropTypes.func
}
state = {value: ''}
handleChange = (event) => {
this.setState({value: event.target.value});
}
addItem = () => {
const {value} = this.state
if (value && value !== '') {
this.props.onAddItem({newItemValue: value});
this.setState({value: ''})
}
}
render() {
return (
<div className="pure-form pure-form-aligned">
<input type="text" value={this.state.value} data-testid="newItemField"
className="form-control" placeholder="add a new item..." onChange={this.handleChange}/>
<button className="pure-button pure-button-primary" data-testid="addBtn" onClick={this.addItem}>Add</button>
</div>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment