UNPKG

9.03 kBMarkdownView Raw
1# react-typeahead
2
3> A typeahead/autocomplete component for React
4
5react-typeahead is a javascript library that provides a react-based
6typeahead, or autocomplete text entry, as well as a "typeahead tokenizer",
7a typeahead that allows you to select multiple results.
8
9## Usage
10
11For a typeahead input:
12
13```javascript
14var Typeahead = require('react-typeahead').Typeahead;
15React.render(
16 <Typeahead
17 options={['John', 'Paul', 'George', 'Ringo']}
18 maxVisible={2}
19 />
20);
21```
22
23For a tokenizer typeahead input:
24
25```javascript
26var Tokenizer = require('react-typeahead').Tokenizer;
27React.render(
28 <Tokenizer
29 options={['John', 'Paul', 'George', 'Ringo']}
30 onTokenAdd={function(token) {
31 console.log('token added: ', token);
32 }}
33 />
34);
35```
36
37## Examples
38
39* [Basic Typeahead with Topcoat][1]
40* [Typeahead Tokenizer with Topcoat][2]
41* [Typeahead Tokenizer with simple styling][3]
42
43![](https://i.cloudup.com/CeLPJjWvFK.gif)
44
45[1]: http://wookiehangover.github.com/react-typeahead/examples/typeahead-topcoat.html
46[2]: http://wookiehangover.github.com/react-typeahead/examples/tokenizer-topcoat.html
47[3]: http://wookiehangover.github.com/react-typeahead/examples/TypeaheadTokenizer-simple.html
48[4]: http://blog.npmjs.org/post/85484771375/how-to-install-npm
49
50## API
51
52### Typeahead(props)
53
54Type: React Component
55
56Basic typeahead input and results list.
57
58#### props.options
59
60Type: `Array`
61Default: []
62
63An array supplied to the filtering function. Can be a list of strings or a list of arbitrary objects. In the latter case, `filterOption` and `displayOption` should be provided.
64
65#### props.defaultValue
66
67Type: `String`
68
69A default value used when the component has no value. If it matches any options a option list will show.
70
71#### props.value
72
73Type: `String`
74
75Specify a value for the text input.
76
77#### props.maxVisible
78
79Type: `Number`
80
81Limit the number of options rendered in the results list.
82
83#### props.customClasses
84
85Type: `Object`
86Allowed Keys: `input`, `results`, `listItem`, `listAnchor`, `hover`
87
88An object containing custom class names for child elements. Useful for
89integrating with 3rd party UI kits.
90
91#### props.placeholder
92
93Type: `String`
94
95Placeholder text for the typeahead input.
96
97#### props.textarea
98
99Type: `Boolean`
100
101Set to `true` to use a `<textarea>` element rather than an `<input>` element
102
103#### props.inputProps
104
105Type: `Object`
106
107Props to pass directly to the `<input>` element.
108
109#### props.onKeyDown
110
111Type: `Function`
112
113Event handler for the `keyDown` event on the typeahead input.
114
115#### props.onKeyUp
116
117Type: `Function`
118
119Event handler for the `keyUp` event on the typeahead input.
120
121#### props.onBlur
122
123Type: `Function`
124
125Event handler for the `blur` event on the typeahead input.
126
127#### props.onFocus
128
129Type: `Function`
130
131Event handler for the `focus` event on the typeahead input.
132
133#### props.onOptionSelected
134
135Type: `Function`
136
137Event handler triggered whenever a user picks an option.
138
139#### props.filterOption
140
141Type: `String` or `Function`
142
143A function to filter the provided `options` based on the current input value. For each option, receives `(inputValue, option)`. If not supplied, defaults to [fuzzy string matching](https://github.com/mattyork/fuzzy).
144
145If provided as a string, it will interpret it as a field name and fuzzy filter on that field of each option object.
146
147#### props.displayOption
148
149Type: `String` or `Function`
150
151A function to map an option onto a string for display in the list. Receives `(option, index)` where index is relative to the results list, not all the options. Must return a string.
152
153If provided as a string, it will interpret it as a field name and use that field from each option object.
154
155#### props.formInputOption
156
157Type: `String` or `Function`
158
159A function to map an option onto a string to include in HTML forms (see `props.name`). Receives `(option)` as arguments. Must return a string.
160
161If specified as a string, it will interpret it as a field name and use that field from each option object.
162
163If not specified, it will fall back onto the semantics described in `props.displayOption`.
164
165This option is ignored if you don't specify the `name` prop. It is required if you both specify the `name` prop and are using non-string options. It is optional otherwise.
166
167#### props.defaultClassNames
168
169Type: `boolean`
170Default: true
171
172If false, the default classNames are removed from the typeahead.
173
174#### props.customListComponent
175
176Type: `React Component`
177
178A React Component that renders the list of typeahead results. This replaces the default list of results.
179
180This component receives the following props :
181
182##### Passed through
183
184- `props.displayOptions`
185- `props.customClasses`
186- `props.onOptionSelected`
187
188##### Created or modified
189- `props.options`
190 - This is the Typeahead's `props.options` filtered and limited to `Typeahead.props.maxVisible`.
191- `props.selectedIndex`
192 - The index of the highlighted option for rendering
193
194
195### Typeahead ([Exposed Component Functions][reactecf])
196
197#### typeahead.focus
198
199Focuses the typeahead input.
200
201---
202
203### Tokenizer(props)
204
205Type: React Component
206
207Typeahead component that allows for multiple options to be selected.
208
209#### props.options
210
211Type: `Array`
212Default: []
213
214An array supplied to the filter function.
215
216#### props.maxVisible
217
218Type: `Number`
219
220Limit the number of options rendered in the results list.
221
222#### props.name
223
224Type: `String`
225
226The name for HTML forms to be used for submitting the tokens' values array.
227
228#### props.customClasses
229
230Type: `Object`
231Allowed Keys: `input`, `results`, `listItem`, `listAnchor`, `typeahead`
232
233An object containing custom class names for child elements. Useful for
234integrating with 3rd party UI kits.
235
236#### props.placeholder
237
238Type: `String`
239
240Placeholder text for the typeahead input.
241
242#### props.inputProps
243
244Type: `Object`
245
246Props to pass directly to the `<input>` element.
247
248#### props.onKeyDown
249
250Type: `Function`
251
252Event handler for the `keyDown` event on the typeahead input.
253
254#### props.onKeyUp
255
256Type: `Function`
257
258Event handler for the `keyUp` event on the typeahead input.
259
260#### props.onBlur
261
262Type: `Function`
263
264Event handler for the `blur` event on the typeahead input.
265
266#### props.onFocus
267
268Type: `Function`
269
270Event handler for the `focus` event on the typeahead input.
271
272#### props.defaultSelected
273
274Type: `Array`
275
276A set of values of tokens to be loaded on first render.
277
278#### props.onTokenRemove
279
280Type: `Function`
281Params: `(removedToken)`
282
283Event handler triggered whenever a token is removed.
284
285#### props.onTokenAdd
286
287Type: `Function`
288Params: `(addedToken)`
289
290Event handler triggered whenever a token is removed.
291
292#### props.displayOption
293
294Type: `String` or `Function`
295
296A function to map an option onto a string for display in the list. Receives `(option, index)` where index is relative to the results list, not all the options. Must return a string.
297
298If provided as a string, it will interpret it as a field name and use that field from each option object.
299
300#### props.filterOption
301
302Type: `Function`
303
304A function to filter the provided `options` based on the current input value. For each option, receives `(inputValue, option)`. If not supplied, defaults to [fuzzy string matching](https://github.com/mattyork/fuzzy).
305
306#### props.defaultClassNames
307
308Type: `boolean`
309Default: true
310
311If false, the default classNames are removed from the tokenizer and the typeahead.
312
313### Tokenizer ([Exposed Component Functions][reactecf])
314
315#### tokenizer.focus
316
317Focuses the tokenizer input.
318
319#### tokenizer.getSelectedTokens
320
321Type: `Function`
322
323A function to return the currently selected tokens.
324
325## Developing
326
327### Setting Up
328
329You will need `npm` to develop on react-typeahead. [Installing npm][4].
330
331Once that's done, to get started, run `npm install` in your checkout directory.
332This will install all the local development dependences, such as `gulp` and `mocha`
333
334### Testing
335
336react-typeahead uses mocha for unit tests and gulp for running them. Large changes should
337include unittests.
338
339After updating or creating new tests, run `npm run-script build-test` to regenerate the
340test package.
341
342Once that's done, running the tests is easy with `gulp`:
343
344```
345> gulp test
346[00:17:25] Using gulpfile ~/src/react-typeahead/gulpfile.js
347[00:17:25] Starting 'test'...
348
349
350 ․․․․․․․․․․․․․․․
351
352 15 passing (43ms)
353
354[00:17:25] Finished 'test' after 448 ms
355[00:17:25] Starting 'default'...
356[00:17:25] Finished 'default' after 6.23 μs
357```
358
359### Contributing
360
361Basically, fork the repository and send a pull request. It can be difficult to review these, so
362here are some general rules to follow for getting your PR accepted more quickly:
363
364- All new properties and exposed component function should be documented in the README.md
365- Break your changes into smaller, easy to understand commits.
366- Send separate PRs for each commit when possible.
367- Feel free to rebase, merge, and rewrite commits to make them more readible.
368- Add comments explaining anything that's not painfully obvious.
369- Add unittests for your change if possible.
370
371[reactecf]: https://facebook.github.io/react/tips/expose-component-functions.html