-
-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathtodoEntry.js
48 lines (38 loc) · 1010 Bytes
/
todoEntry.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
import React from 'react';
import ReactDOM from 'react-dom';
import PropTypes from 'prop-types';
import {observer} from 'mobx-react';
import {makeObservable, action} from 'mobx';
const ENTER_KEY = 13;
class TodoEntry extends React.Component {
constructor(props) {
super(props);
makeObservable(this, {
handleNewTodoKeyDown: action.bound
});
}
render() {
return (<input
ref="newField"
className="new-todo"
placeholder="What needs to be done?"
onKeyDown={this.handleNewTodoKeyDown}
autoFocus={true}
/>);
}
handleNewTodoKeyDown = (event) => {
if (event.keyCode !== ENTER_KEY) {
return;
}
event.preventDefault();
var val = ReactDOM.findDOMNode(this.refs.newField).value.trim();
if (val) {
this.props.todoStore.addTodo(val);
ReactDOM.findDOMNode(this.refs.newField).value = '';
}
};
};
TodoEntry.propTypes = {
todoStore: PropTypes.object.isRequired
};
export default observer(TodoEntry);