1
1
import isEqual from 'fast-deep-equal' ;
2
2
import React , {
3
+ ChangeEvent ,
4
+ FocusEventHandler ,
5
+ InputHTMLAttributes ,
6
+ KeyboardEventHandler ,
3
7
Ref ,
4
8
useCallback ,
5
9
useEffect ,
@@ -21,10 +25,12 @@ import {
21
25
import useValidateProps from './useValidateProps' ;
22
26
23
27
import {
28
+ AllowNew ,
24
29
FilterByCallback ,
25
- InternalProps ,
30
+ Id ,
31
+ LabelKey ,
26
32
Option ,
27
- TypeaheadProps ,
33
+ SelectHint ,
28
34
TypeaheadState ,
29
35
} from '../types' ;
30
36
@@ -147,6 +153,128 @@ export interface TypeaheadRef {
147
153
toggleMenu : ( ) => void ;
148
154
}
149
155
156
+ export interface TypeaheadProps {
157
+ /**
158
+ * Allows the creation of new selections on the fly. Note that any new items
159
+ * will be added to the list of selections, but not the list of original
160
+ * options unless handled as such by `Typeahead`'s parent.
161
+ *
162
+ * If a function is specified, it will be used to determine whether a custom
163
+ * option should be included. The return value should be true or false.
164
+ */
165
+ allowNew ?: AllowNew ;
166
+ /**
167
+ * Autofocus the input when the component initially mounts.
168
+ */
169
+ autoFocus ?: boolean ;
170
+ /**
171
+ * Whether or not filtering should be case-sensitive.
172
+ */
173
+ caseSensitive ?: boolean ;
174
+ /**
175
+ * The initial value displayed in the text input.
176
+ */
177
+ defaultInputValue ?: string ;
178
+ /**
179
+ * Whether or not the menu is displayed upon initial render.
180
+ */
181
+ defaultOpen ?: boolean ;
182
+ /**
183
+ * Specify any pre-selected options. Use only if you want the component to
184
+ * be uncontrolled.
185
+ */
186
+ defaultSelected ?: Option [ ] ;
187
+ /**
188
+ * Either an array of fields in `option` to search, or a custom filtering
189
+ * callback.
190
+ */
191
+ filterBy ?: string [ ] | FilterByCallback ;
192
+ /**
193
+ * Highlights the menu item if there is only one result and allows selecting
194
+ * that item by hitting enter. Does not work with `allowNew`.
195
+ */
196
+ highlightOnlyResult ?: boolean ;
197
+ /**
198
+ * An html id attribute, required for assistive technologies such as screen
199
+ * readers.
200
+ */
201
+ id ?: Id ;
202
+ /**
203
+ * Whether the filter should ignore accents and other diacritical marks.
204
+ */
205
+ ignoreDiacritics ?: boolean ;
206
+ inputProps ?: InputHTMLAttributes < HTMLInputElement > ;
207
+ /**
208
+ * Specify the option key to use for display or a function returning the
209
+ * display string. By default, the selector will use the `label` key.
210
+ */
211
+ labelKey ?: LabelKey ;
212
+ /**
213
+ * Number of input characters that must be entered before showing results.
214
+ */
215
+ minLength ?: number ;
216
+ /**
217
+ * Whether or not multiple selections are allowed.
218
+ */
219
+ multiple ?: boolean ;
220
+ /**
221
+ * Invoked when the input is blurred. Receives an event.
222
+ */
223
+ onBlur ?: FocusEventHandler < HTMLInputElement > ;
224
+ /**
225
+ * Invoked whenever items are added or removed. Receives an array of the
226
+ * selected options.
227
+ */
228
+ onChange ?: ( selected : Option [ ] ) => void ;
229
+ /**
230
+ * Invoked when the input is focused. Receives an event.
231
+ */
232
+ onFocus ?: FocusEventHandler < HTMLInputElement > ;
233
+ /**
234
+ * Invoked when the input value changes. Receives the string value of the
235
+ * input.
236
+ */
237
+ onInputChange ?: ( text : string , event : ChangeEvent < HTMLInputElement > ) => void ;
238
+ /**
239
+ * Invoked when a key is pressed. Receives an event.
240
+ */
241
+ onKeyDown ?: KeyboardEventHandler < HTMLInputElement > ;
242
+ /**
243
+ * Invoked when menu visibility changes.
244
+ */
245
+ onMenuToggle ?: ( isOpen : boolean ) => void ;
246
+
247
+ /**
248
+ * Whether or not the menu should be displayed. `undefined` allows the
249
+ * component to control visibility, while `true` and `false` show and hide
250
+ * the menu, respectively.
251
+ */
252
+ open ?: boolean ;
253
+ /**
254
+ * Full set of options, including pre-selected options. Must either be an
255
+ * array of objects (recommended) or strings.
256
+ */
257
+ options : Option [ ] ;
258
+ /**
259
+ * The selected option(s) displayed in the input. Use this prop if you want
260
+ * to control the component via its parent.
261
+ */
262
+ selected ?: Option [ ] ;
263
+ selectHint ?: SelectHint ;
264
+ }
265
+
266
+ type OptionalProps < T , K extends keyof T > = Partial < Pick < T , K > > &
267
+ Required < Omit < T , K > > ;
268
+
269
+ /**
270
+ * Most props used internally become "required" since they're given default
271
+ * values.
272
+ */
273
+ export type InternalProps = OptionalProps <
274
+ Required < Omit < TypeaheadProps , 'onChange' > > ,
275
+ 'id' | 'open' | 'selected' | 'selectHint'
276
+ > ;
277
+
150
278
function useTypeahead (
151
279
{ onChange, ...partialProps } : TypeaheadProps ,
152
280
ref ?: Ref < TypeaheadRef >
0 commit comments