UNPKG

6.96 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(selectedTokens, token) {
31 console.log('token added: ', token);
32 console.log('current tokens: ', selectedTokens);
33 }}
34 />
35);
36```
37
38## Examples
39
40* [Basic Typeahead with Topcoat][1]
41* [Typeahead Tokenizer with Topcoat][2]
42* [Typeahead Tokenizer with simple styling][3]
43
44![](https://i.cloudup.com/CeLPJjWvFK.gif)
45
46[1]: http://wookiehangover.github.com/react-typeahead/examples/typeahead-topcoat.html
47[2]: http://wookiehangover.github.com/react-typeahead/examples/tokenizer-topcoat.html
48[3]: http://wookiehangover.github.com/react-typeahead/examples/TypeaheadTokenizer-simple.html
49[4]: http://blog.npmjs.org/post/85484771375/how-to-install-npm
50
51## API
52
53### Typeahead(props)
54
55Type: React Component
56
57Basic typeahead input and results list.
58
59#### props.options
60
61Type: `Array`
62Default: []
63
64An 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.
65
66#### props.maxVisible
67
68Type: `Number`
69
70Limit the number of options rendered in the results list.
71
72#### props.customClasses
73
74Type: `Object`
75Allowed Keys: `input`, `results`, `listItem`, `listAnchor`, `hover`
76
77An object containing custom class names for child elements. Useful for
78integrating with 3rd party UI kits.
79
80#### props.placeholder
81
82Type: `String`
83
84Placeholder text for the typeahead input.
85
86#### props.inputProps
87
88Type: `Object`
89
90Props to pass directly to the `<input>` element.
91
92#### props.onKeyDown
93
94Type: `Function`
95
96Event handler for the `keyDown` event on the typeahead input.
97
98#### props.onKeyUp
99
100Type: `Function`
101
102Event handler for the `keyUp` event on the typeahead input.
103
104#### props.onOptionSelected
105
106Type: `Function`
107
108Event handler triggered whenever a user picks an option.
109
110#### props.filterOption
111
112Type: `String` or `Function`
113
114A 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).
115
116If provided as a string, it will interpret it as a field name and fuzzy filter on that field of each option object.
117
118#### props.displayOption
119
120Type: `String` or `Function`
121
122A 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.
123
124If provided as a string, it will interpret it as a field name and use that field from each option object.
125
126#### props.formInputOption
127
128Type: `String` or `Function`
129
130A function to map an option onto a string to include in HTML forms (see `props.name`). Receives `(option)` as arguments. Must return a string.
131
132If specified as a string, it will interpret it as a field name and use that field from each option object.
133
134If not specified, it will fall back onto the semantics described in `props.displayOption`.
135
136This 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.
137
138### Typeahead(exposed component functions)
139
140#### typeahead.focus
141
142Focuses the typeahead input.
143
144---
145
146### Tokenizer(props)
147
148Type: React Component
149
150Typeahead component that allows for multiple options to be selected.
151
152#### props.options
153
154Type: `Array`
155Default: []
156
157An array supplied to the filter function.
158
159#### props.maxVisible
160
161Type: `Number`
162
163Limit the number of options rendered in the results list.
164
165#### props.name
166
167Type: `String`
168
169The name for HTML forms to be used for submitting the tokens' values array.
170
171#### props.customClasses
172
173Type: `Object`
174Allowed Keys: `input`, `results`, `listItem`, `listAnchor`, `typeahead`
175
176An object containing custom class names for child elements. Useful for
177integrating with 3rd party UI kits.
178
179#### props.placeholder
180
181Type: `String`
182
183Placeholder text for the typeahead input.
184
185#### props.inputProps
186
187Type: `Object`
188
189Props to pass directly to the `<input>` element.
190
191#### props.onKeyDown
192
193Type: `Function`
194
195Event handler for the `keyDown` event on the typeahead input.
196
197#### props.onKeyUp
198
199Type: `Function`
200
201Event handler for the `keyUp` event on the typeahead input.
202
203
204#### props.defaultSelected
205
206Type: `Array`
207
208A set of values of tokens to be loaded on first render.
209
210#### props.onTokenRemove
211
212Type: `Function`
213Params: `(selectedTokens, removedToken)`
214
215Event handler triggered whenever a token is removed.
216
217#### props.onTokenAdd
218
219Type: `Function`
220Params: `(selectedTokens, addedToken)`
221
222Event handler triggered whenever a token is removed.
223
224#### props.filterOption
225
226Type: `Function`
227
228A 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).
229
230### Tokenizer(exposed component functions)
231
232#### tokenizer.focus
233
234Focuses the tokenizer input.
235
236## Developing
237
238### Setting Up
239
240You will need `npm` to develop on react-typeahead. [Installing npm][4].
241
242Once that's done, to get started, run `npm install` in your checkout directory.
243This will install all the local development dependences, such as `gulp` and `mocha`
244
245### Testing
246
247react-typeahead uses mocha for unit tests and gulp for running them. Large changes should
248include unittests.
249
250After updating or creating new tests, run `npm run-script build-test` to regenerate the
251test package.
252
253Once that's done, running the tests is easy with `gulp`:
254
255```
256> gulp test
257[00:17:25] Using gulpfile ~/src/react-typeahead/gulpfile.js
258[00:17:25] Starting 'test'...
259
260
261 ․․․․․․․․․․․․․․․
262
263 15 passing (43ms)
264
265[00:17:25] Finished 'test' after 448 ms
266[00:17:25] Starting 'default'...
267[00:17:25] Finished 'default' after 6.23 μs
268```
269
270### Contributing
271
272Basically, fork the repository and send a pull request. It can be difficult to review these, so
273here are some general rules to follow for getting your PR accepted more quickly:
274
275- Break your changes into smaller, easy to understand commits.
276- Send separate PRs for each commit when possible.
277- Feel free to rebase, merge, and rewrite commits to make them more readible.
278- Add comments explaining anything that's not painfully obvious.
279- Add unittests for your change if possible.