-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTodoInput.js
67 lines (54 loc) · 1.48 KB
/
TodoInput.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import React from "react";
import { cloneDeep } from "lodash";
export default class TodoInput extends React.Component {
constructor(props) {
super(props);
this.state = {
value: ""
};
this.onKeyPress = this.onKeyPress.bind(this);
this.onChange = this.onChange.bind(this);
}
onChange(event) {
// Deep copy the state.
const nextState = cloneDeep(this.state);
// TODO(#8) Set the nextState.value to be
// the event.target.value.
// Set the next state.
this.setState(nextState);
}
/**
* Determines if a user pressed enter and validates
* the input before calling the onEnter prop.
*/
onKeyPress(event) {
// Do nothing if the enter key was not pressed.
if (event.key !== "Enter" || event.which !== 13) {
return;
}
// TODO(#9) Call this.props.onEnter with this.state.value.
// TODO(#10) Deep copy the state.
// TODO(#11) Update the state such that the user
// does not have to manually backspace.
// TODO(#12) Set the next state.
}
componentDidMount() {
if (this.props.initialValue) {
const nextState = cloneDeep(this.state);
nextState.value = this.props.initialValue;
this.setState(nextState);
}
}
render() {
return (
<input
className="form-control"
type="text"
placeholder="What do you need to do?"
value={this.state.value}
onChange={this.onChange}
onKeyPress={this.onKeyPress}
/>
);
}
}