-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTodoTextInput.js
More file actions
executable file
·58 lines (51 loc) · 1.24 KB
/
TodoTextInput.js
File metadata and controls
executable file
·58 lines (51 loc) · 1.24 KB
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
import React, { Component, PropTypes } from 'react'
import classnames from 'classnames'
class TodoTextInput extends Component {
constructor(props, context) {
super(props, context)
this.state = {
text: this.props.text || ''
}
}
handleSubmit(e) {
const text = e.target.value.trim()
if (e.which === 13) {
this.props.onSave(text)
if (this.props.newTodo) {
this.setState({ text: '' })
}
}
}
handleChange(e) {
this.setState({ text: e.target.value })
}
handleBlur(e) {
if (!this.props.newTodo) {
this.props.onSave(e.target.value)
}
}
render() {
return (
<input className={
classnames({
edit: this.props.editing,
'new-todo': this.props.newTodo
})}
type="text"
placeholder={this.props.placeholder}
autoFocus="true"
value={this.state.text}
onBlur={this.handleBlur.bind(this)}
onChange={this.handleChange.bind(this)}
onKeyDown={this.handleSubmit.bind(this)} />
)
}
}
TodoTextInput.propTypes = {
onSave: PropTypes.func.isRequired,
text: PropTypes.string,
placeholder: PropTypes.string,
editing: PropTypes.bool,
newTodo: PropTypes.bool
}
export default TodoTextInput