| 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275 |
120x
120x
120x
120x
2x
2x
2x
2x
2x
2x
2x
23x
9x
9x
1x
1x
1x
1x
1x
1x
1x
1x
1x
2x
2x
1x
1x
1x
1x
8x
8x
8x
1x
1x
1x
8x
8x
21x
21x
21x
21x
21x
13x
13x
13x
13x
13x
4x
13x
13x
13x
42x
| import React from 'react';
import _ from 'lodash';
import { createClass } from '../../util/component-types';
import { lucidClassNames } from '../../util/style-helpers';
import { partitionText } from '../../util/text-manipulation';
import * as reducers from './Autocomplete.reducers';
import * as KEYCODE from '../../constants/key-code';
import DropMenu from '../DropMenu/DropMenu';
const cx = lucidClassNames.bind('&-Autocomplete');
const {
arrayOf,
bool,
func,
object,
shape,
string,
} = React.PropTypes;
/**
*
* {"categories": ["controls", "text"], "madeFrom": ["DropMenu"]}
*
* A text input with suggested values displayed in an attached menu.
*/
const Autocomplete = createClass({
displayName: 'Autocomplete',
_isPrivate: true,
reducers,
propTypes: {
/**
* Appended to the component-specific class names set on the root elements.
*/
className: string,
/**
* Styles that are passed through to root element.
*/
style: object,
/**
* Disables the Autocomplete from being clicked or focused.
*/
isDisabled: bool,
/**
* Array of suggested text input values shown in drop menu.
*/
suggestions: arrayOf(string),
/**
* Text value of the input.
*/
value: string,
/**
* Object of DropMenu props which are passed thru to the underlying DropMenu component.
*/
DropMenu: shape(DropMenu.propTypes),
/**
* Called when the input value changes.
* Has the signature `(value, {props, event}) => {}` where value is a string.
*/
onChange: func,
/**
* Called when a suggestion is selected from the menu.
* Has the signature `(optionIndex, {props, event}) => {}` where optionIndex is a number.
*/
onSelect: func,
/**
* Called when menu is expected to expand.
* Has the signature `({props, event}) => {}`.
*/
onExpand: func,
},
getDefaultProps() {
return {
isDisabled: false,
suggestions: [],
value: '',
onChange: _.noop,
onSelect: _.noop,
onExpand: _.noop,
DropMenu: DropMenu.getDefaultProps(),
};
},
handleSelect(optionIndex, { event }) {
const {
suggestions,
onChange,
onSelect,
} = this.props;
onChange(suggestions[optionIndex], { event, props: this.props});
onSelect(optionIndex, { event, props: this.props});
},
handleInput(event) {
const {
onChange,
onExpand,
DropMenu: {
onCollapse,
},
} = this.props;
onChange(event.target.value, {event, props: this.props});
Eif (!_.isEmpty(event.target.value)) {
onExpand({event, props: this.props});
} else {
onCollapse();
}
},
getInputValue() {
return _.get(this.refs, 'inputNode.value', this.props.value);
},
setInputValue(value) {
Eif (this.refs.inputNode) {
this.refs.inputNode.value = value;
}
},
handleInputKeydown(event) {
const {
onExpand,
DropMenu: {
isExpanded,
focusedIndex,
onCollapse,
},
} = this.props;
const value = this.getInputValue();
Iif (event.keyCode === KEYCODE.Tab && isExpanded && focusedIndex !== null) {
this.handleSelect(focusedIndex, {event, props: this.props});
event.preventDefault();
}
Eif (event.keyCode === KEYCODE.ArrowDown && !isExpanded) {
event.stopPropagation();
Eif (_.isEmpty(value)) {
onExpand({event, props: this.props});
}
}
Iif (event.keyCode === KEYCODE.Escape) {
event.stopPropagation();
onCollapse(event);
}
Iif (event.keyCode === KEYCODE.Enter && focusedIndex === null) {
event.stopPropagation();
onCollapse(event);
}
},
handleControlClick(event) {
const {
onExpand,
DropMenu: {
isExpanded,
onCollapse,
},
} = this.props;
if (event.target === this.refs.inputNode) {
onExpand({event, props: this.props});
} else {
Iif (isExpanded) {
onCollapse(event);
} else {
onExpand({event, props: this.props});
}
this.refs.inputNode.focus();
}
},
componentDidMount() {
const { value } = this.props;
this.refs.inputNode.addEventListener('input', this.handleInput);
this.setInputValue(value);
},
componentWillReceiveProps(nextProps) {
const { value } = nextProps;
Eif (value !== this.getInputValue()) {
this.setInputValue(value);
}
},
componentWillUnmount() {
Eif (this.refs.inputNode) {
this.refs.inputNode.removeEventListener('input', this.handleInput);
}
},
render() {
const {
style,
className,
isDisabled,
DropMenu: dropMenuProps,
suggestions,
...passThroughs,
} = this.props;
const { isExpanded } = dropMenuProps;
const value = this.getInputValue();
const valuePattern = new RegExp(_.escapeRegExp(value), 'i');
return (
<DropMenu
{...dropMenuProps}
isDisabled={isDisabled}
selectedIndices={[]}
className={cx('&', className)}
onSelect={this.handleSelect}
style={style}
>
<DropMenu.Control onClick={this.handleControlClick}>
<div className={cx('&-Control', {
'&-Control-is-expanded': isExpanded,
'&-Control-is-disabled': isDisabled,
})}>
<input
{..._.omit(passThroughs, ['onChange', 'onSelect', 'onExpand', 'value', 'children'])}
type='text'
className={cx('&-Control-input')}
ref='inputNode'
onKeyDown={this.handleInputKeydown}
disabled={isDisabled}
/>
</div>
</DropMenu.Control>
{value ? _.map(suggestions, (suggestion) => (
<DropMenu.Option key={'AutocompleteOption' + suggestion} >
{(() => {
const [pre, match, post] = partitionText(suggestion, valuePattern, value.length);
const formattedSuggestion = [];
Iif (pre) {
formattedSuggestion.push(
<span key={`AutocompleteOption-suggestion-pre-${suggestion}`} className={cx('&-Option-suggestion-pre')}>{pre}</span>
);
}
if (match) {
formattedSuggestion.push(
<span key={`AutocompleteOption-suggestion-match-${suggestion}`} className={cx('&-Option-suggestion-match')}>{match}</span>
);
}
Eif (post) {
formattedSuggestion.push(
<span key={`AutocompleteOption-suggestion-post-${suggestion}`} className={cx('&-Option-suggestion-post')}>{post}</span>
);
}
return formattedSuggestion;
})()}
</DropMenu.Option>)
) : _.map(suggestions, (suggestion) => (
<DropMenu.Option key={'AutocompleteOption' + suggestion} >{suggestion}</DropMenu.Option>)
)}
</DropMenu>
);
},
});
export default Autocomplete;
|