1 | # react-typeahead
|
2 |
|
3 | > A typeahead/autocomplete component for React
|
4 |
|
5 | react-typeahead is a javascript library that provides a react-based
|
6 | typeahead, or autocomplete text entry, as well as a "typeahead tokenizer",
|
7 | a typeahead that allows you to select multiple results.
|
8 |
|
9 | ## Usage
|
10 |
|
11 | For a typeahead input:
|
12 |
|
13 | ```javascript
|
14 | var Typeahead = require('react-typeahead').Typeahead;
|
15 | React.render(
|
16 | <Typeahead
|
17 | options={['John', 'Paul', 'George', 'Ringo']}
|
18 | maxVisible= 2
|
19 | />
|
20 | );
|
21 | ```
|
22 |
|
23 | For a tokenizer typeahead input:
|
24 |
|
25 | ```javascript
|
26 | var Tokenizer = require('react-typeahead').Tokenizer;
|
27 | React.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 |
|
55 | Type: React Component
|
56 |
|
57 | Basic typeahead input and results list.
|
58 |
|
59 | #### props.options
|
60 |
|
61 | Type: `Array`
|
62 | Default: []
|
63 |
|
64 | An 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 |
|
68 | Type: `Number`
|
69 |
|
70 | Limit the number of options rendered in the results list.
|
71 |
|
72 | #### props.customClasses
|
73 |
|
74 | Type: `Object`
|
75 | Allowed Keys: `input`, `results`, `listItem`, `listAnchor`, `hover`
|
76 |
|
77 | An object containing custom class names for child elements. Useful for
|
78 | integrating with 3rd party UI kits.
|
79 |
|
80 | #### props.placeholder
|
81 |
|
82 | Type: `String`
|
83 |
|
84 | Placeholder text for the typeahead input.
|
85 |
|
86 | #### props.inputProps
|
87 |
|
88 | Type: `Object`
|
89 |
|
90 | Props to pass directly to the `<input>` element.
|
91 |
|
92 | #### props.onKeyDown
|
93 |
|
94 | Type: `Function`
|
95 |
|
96 | Event handler for the `keyDown` event on the typeahead input.
|
97 |
|
98 | #### props.onKeyUp
|
99 |
|
100 | Type: `Function`
|
101 |
|
102 | Event handler for the `keyUp` event on the typeahead input.
|
103 |
|
104 | #### props.onOptionSelected
|
105 |
|
106 | Type: `Function`
|
107 |
|
108 | Event handler triggered whenever a user picks an option.
|
109 |
|
110 | #### props.filterOption
|
111 |
|
112 | Type: `String` or `Function`
|
113 |
|
114 | A 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 |
|
116 | If 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 |
|
120 | Type: `String` or `Function`
|
121 |
|
122 | A 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 |
|
124 | If 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 |
|
128 | Type: `String` or `Function`
|
129 |
|
130 | A 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 |
|
132 | If specified as a string, it will interpret it as a field name and use that field from each option object.
|
133 |
|
134 | If not specified, it will fall back onto the semantics described in `props.displayOption`.
|
135 |
|
136 | This 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 |
|
142 | Focuses the typeahead input.
|
143 |
|
144 | ---
|
145 |
|
146 | ### Tokenizer(props)
|
147 |
|
148 | Type: React Component
|
149 |
|
150 | Typeahead component that allows for multiple options to be selected.
|
151 |
|
152 | #### props.options
|
153 |
|
154 | Type: `Array`
|
155 | Default: []
|
156 |
|
157 | An array supplied to the filter function.
|
158 |
|
159 | #### props.maxVisible
|
160 |
|
161 | Type: `Number`
|
162 |
|
163 | Limit the number of options rendered in the results list.
|
164 |
|
165 | #### props.name
|
166 |
|
167 | Type: `String`
|
168 |
|
169 | The name for HTML forms to be used for submitting the tokens' values array.
|
170 |
|
171 | #### props.customClasses
|
172 |
|
173 | Type: `Object`
|
174 | Allowed Keys: `input`, `results`, `listItem`, `listAnchor`, `typeahead`
|
175 |
|
176 | An object containing custom class names for child elements. Useful for
|
177 | integrating with 3rd party UI kits.
|
178 |
|
179 | #### props.placeholder
|
180 |
|
181 | Type: `String`
|
182 |
|
183 | Placeholder text for the typeahead input.
|
184 |
|
185 | #### props.inputProps
|
186 |
|
187 | Type: `Object`
|
188 |
|
189 | Props to pass directly to the `<input>` element.
|
190 |
|
191 | #### props.onKeyDown
|
192 |
|
193 | Type: `Function`
|
194 |
|
195 | Event handler for the `keyDown` event on the typeahead input.
|
196 |
|
197 | #### props.onKeyUp
|
198 |
|
199 | Type: `Function`
|
200 |
|
201 | Event handler for the `keyUp` event on the typeahead input.
|
202 |
|
203 |
|
204 | #### props.defaultSelected
|
205 |
|
206 | Type: `Array`
|
207 |
|
208 | A set of values of tokens to be loaded on first render.
|
209 |
|
210 | #### props.onTokenRemove
|
211 |
|
212 | Type: `Function`
|
213 | Params: `(selectedTokens, removedToken)`
|
214 |
|
215 | Event handler triggered whenever a token is removed.
|
216 |
|
217 | #### props.onTokenAdd
|
218 |
|
219 | Type: `Function`
|
220 | Params: `(selectedTokens, addedToken)`
|
221 |
|
222 | Event handler triggered whenever a token is removed.
|
223 |
|
224 | #### props.filterOption
|
225 |
|
226 | Type: `Function`
|
227 |
|
228 | A 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 |
|
234 | Focuses the tokenizer input.
|
235 |
|
236 | ## Developing
|
237 |
|
238 | ### Setting Up
|
239 |
|
240 | You will need `npm` to develop on react-typeahead. [Installing npm][4].
|
241 |
|
242 | Once that's done, to get started, run `npm install` in your checkout directory.
|
243 | This will install all the local development dependences, such as `gulp` and `mocha`
|
244 |
|
245 | ### Testing
|
246 |
|
247 | react-typeahead uses mocha for unit tests and gulp for running them. Large changes should
|
248 | include unittests.
|
249 |
|
250 | After updating or creating new tests, run `npm run-script build-test` to regenerate the
|
251 | test package.
|
252 |
|
253 | Once 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 |
|
272 | Basically, fork the repository and send a pull request. It can be difficult to review these, so
|
273 | here 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.
|