Created
May 20, 2020 08:25
-
-
Save alx8437/4f0717da31fc9c5fc6b5a4e2eb3900ff to your computer and use it in GitHub Desktop.
This file contains hidden or 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 from 'react'; | |
class AddNewItemForm extends React.Component { | |
constructor() { | |
super(); | |
} | |
state = { | |
error: false, | |
inputValue: '' | |
} | |
newInputValue = (e) => { | |
this.setState({inputValue: e.currentTarget.value}); | |
this.setState({ | |
error: false | |
}) | |
} | |
onAddItemClick = () => { | |
let newText = this.state.inputValue.trim(); | |
if (newText === "") { | |
this.setState({ | |
error: true | |
}) | |
} else { | |
this.props.addItem(newText); | |
this.state.inputValue = ''; | |
} | |
} | |
onKeyPress = (e) => { | |
if (e.key === "Enter") { | |
this.onAddItemList(); | |
} | |
} | |
render = () => { | |
let classToError = this.state.error === true ? "error" : ""; | |
return ( | |
<div className="todoList-header"> | |
<div className="todoList-newTaskForm"> | |
<input | |
value={this.state.inputValue} | |
onChange={this.newInputValue} | |
className={classToError} | |
type="text" | |
placeholder="New item name" | |
onKeyPress={this.onKeyPress} | |
/> | |
<button | |
onClick={this.onAddItemClick} | |
>Add | |
</button> | |
</div> | |
</div> | |
) | |
} | |
} | |
export default AddNewItemForm; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment