This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
/
InputCell.jsx
105 lines (90 loc) · 2.71 KB
/
InputCell.jsx
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import React from 'react';
import {PureRenderMixin} from 'react/addons';
import {Input} from 'zanata-ui';
import StringUtils from '../../utils/StringUtils';
import Actions from '../../actions/GlossaryActions';
import GlossaryStore from '../../stores/GlossaryStore';
import _ from 'lodash';
var InputCell = React.createClass({
propTypes: {
contentHash: React.PropTypes.string.isRequired,
field: React.PropTypes.string.isRequired,
rowIndex: React.PropTypes.number.isRequired,
placeholder: React.PropTypes.string,
onFocusCallback: React.PropTypes.func,
onBlurCallback: React.PropTypes.func
},
TIMEOUT: 150,
updateTimeout: null,
getInitialState: function() {
return this._getState();
},
_getState: function() {
var entry = GlossaryStore.getEntry(this.props.contentHash),
value = _.get(entry, this.props.field),
focusedRow = GlossaryStore.getFocusedRow(),
isFocused = focusedRow && (focusedRow.rowIndex === this.props.rowIndex);
return {
value: value,
isFocused : isFocused
}
},
componentDidMount: function() {
GlossaryStore.addChangeListener(this._onChange);
},
componentWillUnmount: function() {
GlossaryStore.removeChangeListener(this._onChange);
},
_onChange: function() {
if (this.isMounted()) {
this.setState(this._getState());
}
},
_onValueChange : function(event) {
var value = event.target.value;
this.setState({value: value});
if(this.updateTimeout !== null) {
clearTimeout(this.updateTimeout);
}
this.updateTimeout = setTimeout(() => {
console.info('update');
Actions.updateEntryField(this.props.contentHash, this.props.field, value);
}, this.TIMEOUT);
},
_onFocus: function(event) {
if(this.props.onFocusCallback) {
this.props.onFocusCallback(event, this.props.rowIndex);
}
},
_onBlur: function(event) {
if(this.props.onBlurCallback) {
this.props.onBlurCallback(event, this.props.rowIndex);
}
},
//handle reset from the input
_onReset: function () {
this.setState({value: this.props.value});
Actions.updateEntryField(this.props.contentHash, this.props.field, this.props.value);
},
render: function() {
return (
<div>
<span className='cdtargetn mh1/2'>{this.state.value}</span>
<Input
className='cdtargetib'
margin='mh1/8'
border='outline'
label={this.props.field}
hideLabel
value={this.state.value}
placeholder={this.props.placeholder}
onChange={this._onValueChange}
onFocus={this._onFocus}
onReset={this._onReset}
onBlur={this._onBlur}
/>
</div>
);
}
});
export default InputCell;