Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80684 views
1
/**
2
* Copyright (c) 2014-2015, Facebook, Inc.
3
* All rights reserved.
4
*
5
* This source code is licensed under the BSD-style license found in the
6
* LICENSE file in the root directory of this source tree. An additional grant
7
* of patent rights can be found in the PATENTS file in the same directory.
8
*/
9
10
var React = require('react');
11
var ReactPropTypes = React.PropTypes;
12
13
var ENTER_KEY_CODE = 13;
14
15
var TodoTextInput = React.createClass({
16
17
propTypes: {
18
className: ReactPropTypes.string,
19
id: ReactPropTypes.string,
20
placeholder: ReactPropTypes.string,
21
onSave: ReactPropTypes.func.isRequired,
22
value: ReactPropTypes.string
23
},
24
25
getInitialState: function() {
26
return {
27
value: this.props.value || ''
28
};
29
},
30
31
/**
32
* @return {object}
33
*/
34
render: function() /*object*/ {
35
return (
36
<input
37
className={this.props.className}
38
id={this.props.id}
39
placeholder={this.props.placeholder}
40
onBlur={this._save}
41
onChange={this._onChange}
42
onKeyDown={this._onKeyDown}
43
value={this.state.value}
44
autoFocus={true}
45
/>
46
);
47
},
48
49
/**
50
* Invokes the callback passed in as onSave, allowing this component to be
51
* used in different ways.
52
*/
53
_save: function() {
54
this.props.onSave(this.state.value);
55
this.setState({
56
value: ''
57
});
58
},
59
60
/**
61
* @param {object} event
62
*/
63
_onChange: function(/*object*/ event) {
64
this.setState({
65
value: event.target.value
66
});
67
},
68
69
/**
70
* @param {object} event
71
*/
72
_onKeyDown: function(event) {
73
if (event.keyCode === ENTER_KEY_CODE) {
74
this._save();
75
}
76
}
77
78
});
79
80
module.exports = TodoTextInput;
81
82