UNPKG

5.13 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.
64
65#### props.maxVisible
66
67Type: `Number`
68
69Limit the number of options rendered in the results list.
70
71#### props.customClasses
72
73Type: `Object`
74Allowed Keys: `input`, `results`, `listItem`, `listAnchor`, `hover`
75
76An object containing custom class names for child elements. Useful for
77integrating with 3rd party UI kits.
78
79#### props.placeholder
80
81Type: `String`
82
83Placeholder text for the typeahead input.
84
85#### props.inputProps
86
87Type: `Object`
88
89Props to pass directly to the `<input>` element.
90
91#### props.onKeyDown
92
93Type: `Function`
94
95Event handler for the `keyDown` event on the typeahead input.
96
97#### props.onOptionSelected
98
99Type: `Function`
100
101Event handler triggered whenever a user picks an option.
102
103#### props.filterOption
104
105Type: `Function`
106
107A 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).
108
109---
110
111### Tokenizer(props)
112
113Type: React Component
114
115Typeahead component that allows for multiple options to be selected.
116
117#### props.options
118
119Type: `Array`
120Default: []
121
122An array supplied to the filter function.
123
124#### props.maxVisible
125
126Type: `Number`
127
128Limit the number of options rendered in the results list.
129
130#### props.name
131
132Type: `String`
133
134The name for HTML forms to be used for submitting the tokens' values array.
135
136#### props.customClasses
137
138Type: `Object`
139Allowed Keys: `input`, `results`, `listItem`, `listAnchor`, `typeahead`
140
141An object containing custom class names for child elements. Useful for
142integrating with 3rd party UI kits.
143
144#### props.placeholder
145
146Type: `String`
147
148Placeholder text for the typeahead input.
149
150#### props.inputProps
151
152Type: `Object`
153
154Props to pass directly to the `<input>` element.
155
156#### props.defaultSelected
157
158Type: `Array`
159
160A set of values of tokens to be loaded on first render.
161
162#### props.onTokenRemove
163
164Type: `Function`
165
166Event handler triggered whenever a token is removed.
167
168#### props.onTokenAdd
169
170Type: `Function`
171
172Event handler triggered whenever a token is removed.
173
174#### props.filterOption
175
176Type: `Function`
177
178A 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).
179
180
181## Developing
182
183### Setting Up
184
185You will need `npm` to develop on react-typeahead. [Installing npm][4].
186
187Once that's done, to get started, run `npm install` in your checkout directory.
188This will install all the local development dependences, such as `gulp` and `mocha`
189
190### Testing
191
192react-typeahead uses mocha for unit tests and gulp for running them. Large changes should
193include unittests.
194
195After updating or creating new tests, run `npm run-script build-test` to regenerate the
196test package.
197
198Once that's done, running the tests is easy with `gulp`:
199
200```
201> gulp test
202[00:17:25] Using gulpfile ~/src/react-typeahead/gulpfile.js
203[00:17:25] Starting 'test'...
204
205
206 ․․․․․․․․․․․․․․․
207
208 15 passing (43ms)
209
210[00:17:25] Finished 'test' after 448 ms
211[00:17:25] Starting 'default'...
212[00:17:25] Finished 'default' after 6.23 μs
213```
214
215### Contributing
216
217Basically, fork the repository and send a pull request. It can be difficult to review these, so
218here are some general rules to follow for getting your PR accepted more quickly:
219
220- Break your changes into smaller, easy to understand commits.
221- Send separate PRs for each commit when possible.
222- Feel free to rebase, merge, and rewrite commits to make them more readible.
223- Add comments explaining anything that's not painfully obvious.
224- Add unittests for your change if possible.