Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
input のchangeイベントから入力されたテキストを取得するように修正
- Loading branch information
jun68ykt
committed
Sep 13, 2019
1 parent
93ca360
commit 28e8cdc
Showing
1 changed file
with
32 additions
and
19 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,38 @@ | ||
import React from 'react' | ||
|
||
const AddTodo = ({ onAdd }) => { | ||
let input | ||
class AddTodo extends React.Component { | ||
constructor(props) { | ||
super(props) | ||
this.state = { todoText: '' } | ||
} | ||
|
||
return ( | ||
<div> | ||
<form | ||
onSubmit={e => { | ||
e.preventDefault() | ||
if (!input.value.trim()) { | ||
return | ||
} | ||
onAdd(input.value.trim()) | ||
input.value = '' | ||
}} | ||
> | ||
<input ref={node => (input = node)} /> | ||
<button type="submit">Add Todo</button> | ||
</form> | ||
</div> | ||
) | ||
onInputChange = e => { | ||
this.setState({ todoText: e.target.value }) | ||
} | ||
|
||
onSubmit = () => { | ||
const text = this.state.todoText.trim() | ||
if (!text) return | ||
this.props.onAdd(text) | ||
this.setState({ todoText: '' }) | ||
} | ||
|
||
render() { | ||
return ( | ||
<div> | ||
<input | ||
onChange={this.onInputChange} | ||
value={this.state.todoText} | ||
/> | ||
<button | ||
type="button" | ||
onClick={this.onSubmit} | ||
> | ||
Add Todo | ||
</button> | ||
</div> | ||
) | ||
} | ||
} | ||
|
||
export default AddTodo |