UNPKG

6.97 kBMarkdownView Raw
1# API Reference
2The components and higher-order components (HOCs) described below are publicly exposed in the top-level module. Other components should be considered private and subject to change without notice.
3
4- [Components](#components)
5 - [`<Typeahead>`](#typeahead)
6 - [`<AsyncTypeahead>`](#asynctypeahead)
7 - [`<Highlighter>`](#highlighter)
8 - [`<Menu>`](#menu)
9 - [`<MenuItem>`](#menuitem)
10 - [`<TypeaheadMenu>`](#typeaheadmenu)
11 - [`<Token>`](#token)
12- [Higher-Order Components](#higher-order-components)
13 - [`asyncContainer`](#asynccontainer)
14 - [`menuItemContainer`](#menuitemcontainer)
15 - [`tokenContainer`](#tokencontainer)
16
17## Components
18Only a subset of props are documented below, primarily those expecting functions. See the [props documentation](Props.md) for the full list of options.
19
20### `<Typeahead>`
21The primary component provided by the module.
22
23#### Props
24
25##### `allowNew: boolean|Function`
26If a boolean is specified, indicates whether new entry functionality should be enabled. When true, a new entry menu item will be included when the trimmed input is not falsey and there isn't an exact match against the input.
27
28If a function is specified, implicity enables new entry functionality, but allows for a user defined callback to decide whether the new entry menu item should be included in the results list. The callback should return a boolean value and the signature is as follows:
29
30```js
31allowNew(results: Array<string|Object>, props: Object): bool
32```
33
34##### `filterBy: Array<String>|Function`
35See full documentation in the [Filtering section](Filtering.md#filterby-arraystring--function).
36
37##### `labelKey: String|Function`
38See full documentation in the [Rendering section](Rendering.md#labelkey-string--function).
39
40##### `renderMenu: Function`, `renderMenuItemChildren: Function`, and `renderToken: Function`
41See full documentation in the [Rendering section](Rendering.md#rendermenuresults-arrayobject--string-menuprops-object).
42
43##### `onChange(selected: Array<Object|String>)`
44Invoked when the set of selections changes (ie: an item is added or removed). For consistency, `selectedItems` is always an array of selections, even if multi-selection is not enabled.
45
46##### `onInputChange(text: String, event: Event)`
47Invoked when the input value changes. Receives the string value of the input (`text`), as well as the original event.
48
49##### `onBlur(event: Event)`, `onFocus(event: Event)`, `onKeyDown(event: Event)`
50As with a normal text input, these are called when the typeahead input has blur, focus, or keydown events.
51
52##### `onMenuToggle(isOpen: Boolean)`
53Invoked when menu visibility changes.
54
55##### `onMenuHide()` & `onMenuShow()`
56`DEPRECATED` Invoked when the menu is hidden or shown, respectively.
57
58##### `onPaginate(event: Event, shownResults: Number)`
59Invoked when the pagination menu item is clicked. Receives an event as the first argument and the number of shown results as the second.
60
61### `<AsyncTypeahead>`
62An enhanced version of the normal `Typeahead` component for use when performing asynchronous searches. Provides debouncing of user input, optional query caching, and search prompt, empty results, and pending request behaviors.
63
64```jsx
65<AsyncTypeahead
66 isLoading={this.state.isLoading}
67 onSearch={query => {
68 this.setState({isLoading: true});
69 fetch(`https://api.github.com/search/users?q=${query}`)
70 .then(resp => resp.json())
71 .then(json => this.setState({
72 isLoading: false,
73 options: json.items,
74 }));
75 }}
76 options={this.state.options}
77/>
78```
79
80Note that this component is the same as:
81```jsx
82import {asyncContainer, Typeahead} from 'react-bootstrap-typeahead';
83
84const AsyncTypeahead = asyncContainer(Typeahead);
85```
86
87#### Props
88
89##### `isLoading: Boolean` (required)
90Whether or not an asynchronous request is in progress.
91
92##### `onSearch(query: String)` (required)
93Callback to perform when the search is executed. `query` is the text string entered by the user.
94
95### `<Highlighter>`
96Component for highlighting substring matches in the menu items.
97
98#### Props
99
100##### `search: String` (required)
101The substring to look for. This value should correspond to the input text of the typeahead and can be obtained via the `onInputChange` prop or from the `text` property of props being passed down via `renderMenu` or `renderMenuItemChildren`.
102
103```jsx
104<Typeahead
105 ...
106 renderMenuItemChildren={(option, props, idx) => (
107 <Highlighter search={props.text}>
108 {option[props.labelKey]}
109 </Highlighter>
110 )}
111/>
112```
113
114### `<Menu>`
115Provides the markup for a Bootstrap menu, along with some extra functionality for specifying a label when there are no results.
116
117### `<MenuItem>`
118Provides the markup for a Bootstrap menu item, but is wrapped with the `menuItemContainer` HOC to ensure proper behavior within the typeahead context. Provided for use if a more customized `Menu` is desired.
119
120#### Props
121
122##### `option: Object` (required)
123The data item to be displayed.
124
125##### `position: Number`
126The position of the item as rendered in the menu. Allows the top-level `Typeahead` component to be be aware of the item's position despite any custom ordering or grouping in `renderMenu`. **Note:** The value must be a unique, zero-based, sequential integer for proper behavior when keying through the menu.
127
128### `<TypeaheadMenu>`
129The default menu which is rendered by the `Typeahead` component. Can be used in a custom `renderMenu` function for wrapping or modifying the props passed to it without having to re-implement the default functionality.
130
131### `<Token>`
132Individual token component, most commonly for use within `renderToken` to customize the `Token` contents.
133
134## Higher-Order Components
135
136### `asyncContainer`
137The HOC used in [`AsyncTypeahead`](#asynctypeahead).
138
139### `menuItemContainer`
140Connects individual menu items with the main typeahead component via context and abstracts a lot of complex functionality required for behaviors like keying through the menu and input hinting. Also provides `onClick` behavior and active state.
141
142If you use your own menu item components (in `renderMenu` for example), you are strongly advised to wrap them with this HOC:
143
144```jsx
145import {MenuItem} from 'react-bootstrap';
146import {Menu, menuItemContainer, Typeahead} from 'react-bootstrap-typeahead';
147
148const TypeaheadMenuItem = menuItemContainer(MenuItem);
149
150<Typeahead
151 renderMenu={(results, menuProps) => (
152 <Menu {...menuProps}>
153 {results.map((result, props) => (
154 <TypeaheadMenuItem>
155 {result.label}
156 </TypeaheadMenuItem>
157 ))}
158 </Menu>
159 )}
160/>
161```
162
163### `tokenContainer`
164Encapsulates keystroke and outside click behaviors used in `Token`. Useful if you want completely custom markup for the token.
165
166```jsx
167const MyCustomToken = tokenContainer(props => (
168 // Your token code...
169));
170
171<Typeahead
172 multiple
173 options={options}
174 renderToken={(option, props, index) => (
175 <MyCustomToken onRemove={props.onRemove} option={option} />
176 )}
177/>
178```