diff --git a/src/components/App.js b/src/components/App.js index ab10f31..26a941c 100644 --- a/src/components/App.js +++ b/src/components/App.js @@ -26,11 +26,28 @@ class App extends Component { ] }; + /** + * Add a new todo to state items. + * + * @param {String} text + */ + addNewTodo = (text) => { + this.setState(state => { + const todo = { + text, + completed: false, + id: state.length + 1 + }; + + return {items: [...state.items, todo]}; + }) + } + render() { return (
- +
); diff --git a/src/components/InputBox.js b/src/components/InputBox.js index be378fe..9189943 100644 --- a/src/components/InputBox.js +++ b/src/components/InputBox.js @@ -1,12 +1,50 @@ -import React from 'react'; +import React, { Component } from 'react'; +import PropTypes from 'prop-types'; /** * Input component to add a new todo. */ -const InputBox = () => { - return ( - - ) +class InputBox extends Component { + propTypes = { + items: PropTypes.array, + addNewTodo: PropTypes.func.isRequired + } + + state = { + value: '' + } + + /** + * Set input text value on change event. + */ + handleChange = (event) => { + this.setState({value: event.target.value}) + } + + /** + * Add a note when 'Enter' key is pressed. + */ + handleKeyUp = (event) => { + const value = event.target.value; + + if (event.keyCode === 13 && value) { + this.props.addNewTodo(value); + + this.setState({value: ''}); + } + } + + render() { + return ( + + ) + } } export default InputBox; diff --git a/src/components/TodoList.js b/src/components/TodoList.js index 71bbc86..39cd56c 100644 --- a/src/components/TodoList.js +++ b/src/components/TodoList.js @@ -17,7 +17,7 @@ const TodoList = (props) => { return (

Todos

- +
    { items && items.map(item =>