-
Notifications
You must be signed in to change notification settings - Fork 222
Expand file tree
/
Copy pathindex.js
More file actions
207 lines (188 loc) · 6.1 KB
/
Copy pathindex.js
File metadata and controls
207 lines (188 loc) · 6.1 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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/**
* @jsx React.DOM
*/
var React = require('react');
var Token = require('./token');
var KeyEvent = require('../keyevent');
var Typeahead = require('../typeahead');
var classNames = require('classnames');
function _arraysAreDifferent(array1, array2) {
if (array1.length != array2.length){
return true;
}
for (var i = array2.length - 1; i >= 0; i--) {
if (array2[i] !== array1[i]){
return true;
}
}
}
/**
* A typeahead that, when an option is selected, instead of simply filling
* the text entry widget, prepends a renderable "token", that may be deleted
* by pressing backspace on the beginning of the line with the keyboard.
*/
var TypeaheadTokenizer = React.createClass({
propTypes: {
name: React.PropTypes.string,
options: React.PropTypes.array,
customClasses: React.PropTypes.object,
allowCustomValues: React.PropTypes.number,
defaultSelected: React.PropTypes.array,
defaultValue: React.PropTypes.string,
placeholder: React.PropTypes.string,
inputProps: React.PropTypes.object,
onTokenRemove: React.PropTypes.func,
onKeyDown: React.PropTypes.func,
onKeyUp: React.PropTypes.func,
onTokenAdd: React.PropTypes.func,
onFocus: React.PropTypes.func,
onBlur: React.PropTypes.func,
onClick: React.PropTypes.func,
filterOption: React.PropTypes.oneOfType([
React.PropTypes.string,
React.PropTypes.func
]),
displayOption: React.PropTypes.oneOfType([
React.PropTypes.string,
React.PropTypes.func
]),
maxVisible: React.PropTypes.number,
defaultClassNames: React.PropTypes.bool
},
getInitialState: function() {
return {
// We need to copy this to avoid incorrect sharing
// of state across instances (e.g., via getDefaultProps())
selected: this.props.defaultSelected.slice(0)
};
},
getDefaultProps: function() {
return {
options: [],
defaultSelected: [],
customClasses: {},
allowCustomValues: 0,
defaultValue: "",
placeholder: "",
inputProps: {},
defaultClassNames: true,
filterOption: null,
displayOption: function(token){return token },
onKeyDown: function(event) {},
onKeyUp: function(event) {},
onFocus: function(event) {},
onBlur: function(event) {},
onClick: function(event) {},
onTokenAdd: function() {},
onTokenRemove: function() {}
};
},
componentWillReceiveProps: function(nextProps){
// if we get new defaultProps, update selected
if (_arraysAreDifferent(this.props.defaultSelected, nextProps.defaultSelected)){
this.setState({selected: nextProps.defaultSelected.slice(0)})
}
},
focus: function(){
this.refs.typeahead.focus();
},
getSelectedTokens: function(){
return this.state.selected;
},
// TODO: Support initialized tokens
//
_renderTokens: function() {
var tokenClasses = {};
tokenClasses[this.props.customClasses.token] = !!this.props.customClasses.token;
var classList = classNames(tokenClasses);
var result = this.state.selected.map(function(selected) {
var displayString = this.props.displayOption(selected);
return (
<Token key={ displayString } className={classList}
onRemove={ this._removeTokenForValue }
object={selected}
name={ this.props.name }>
{ displayString }
</Token>
);
}, this);
return result;
},
_getOptionsForTypeahead: function() {
// return this.props.options without this.selected
return this.props.options;
},
_onKeyDown: function(event) {
// We only care about intercepting backspaces
if (event.keyCode === KeyEvent.DOM_VK_BACK_SPACE) {
return this._handleBackspace(event);
}
this.props.onKeyDown(event);
},
_handleBackspace: function(event){
// No tokens
if (!this.state.selected.length) {
return;
}
// Remove token ONLY when bksp pressed at beginning of line
// without a selection
var entry = this.refs.typeahead.refs.entry.getDOMNode();
if (entry.selectionStart == entry.selectionEnd &&
entry.selectionStart == 0) {
this._removeTokenForValue(
this.state.selected[this.state.selected.length - 1]);
event.preventDefault();
}
},
_removeTokenForValue: function(value) {
var index = this.state.selected.indexOf(value);
if (index == -1) {
return;
}
this.state.selected.splice(index, 1);
this.setState({selected: this.state.selected});
this.props.onTokenRemove(value);
return;
},
_addTokenForValue: function(value) {
if (this.state.selected.indexOf(value) != -1) {
return;
}
this.state.selected.push(value);
this.setState({selected: this.state.selected});
this.refs.typeahead.setEntryText("");
this.props.onTokenAdd(value);
},
render: function() {
var classes = {};
classes[this.props.customClasses.typeahead] = !!this.props.customClasses.typeahead;
var classList = classNames(classes);
var tokenizerClasses = [this.props.defaultClassNames && "typeahead-tokenizer"];
tokenizerClasses[this.props.className] = !!this.props.className;
var tokenizerClassList = classNames(tokenizerClasses)
return (
<div className={tokenizerClassList}>
{ this._renderTokens() }
<Typeahead ref="typeahead"
className={classList}
placeholder={this.props.placeholder}
inputProps={this.props.inputProps}
allowCustomValues={this.props.allowCustomValues}
customClasses={this.props.customClasses}
options={this._getOptionsForTypeahead()}
defaultValue={this.props.defaultValue}
maxVisible={this.props.maxVisible}
onOptionSelected={this._addTokenForValue}
onKeyDown={this._onKeyDown}
onKeyUp={this.props.onKeyUp}
onFocus={this.props.onFocus}
onBlur={this.props.onBlur}
onClick={this.props.onClick}
displayOption={this.props.displayOption}
defaultClassNames={this.props.defaultClassNames}
filterOption={this.props.filterOption} />
</div>
);
}
});
module.exports = TypeaheadTokenizer;