@@ -32,7 +32,7 @@ import {
32
32
import { checkPropType , inputPropsType , sizeType } from '../../propTypes' ;
33
33
34
34
import {
35
- Option ,
35
+ OptionType ,
36
36
RefElement ,
37
37
RenderToken ,
38
38
RenderTokenProps ,
@@ -42,23 +42,23 @@ import {
42
42
TypeaheadManagerChildProps ,
43
43
} from '../../types' ;
44
44
45
- export interface RenderMenuProps
45
+ export interface RenderMenuProps < Option extends OptionType >
46
46
extends Omit <
47
- TypeaheadMenuProps ,
47
+ TypeaheadMenuProps < Option > ,
48
48
'labelKey' | 'options' | 'renderMenuItemChildren' | 'text'
49
49
> {
50
- renderMenuItemChildren ?: RenderMenuItemChildren ;
50
+ renderMenuItemChildren ?: RenderMenuItemChildren < Option > ;
51
51
}
52
52
53
- export interface TypeaheadComponentProps extends Partial < TypeaheadProps > {
53
+ export interface TypeaheadComponentProps < Option extends OptionType > extends Partial < TypeaheadProps < Option > > {
54
54
align ?: Align ;
55
55
className ?: string ;
56
56
clearButton ?: boolean ;
57
57
disabled ?: boolean ;
58
58
dropup ?: boolean ;
59
59
emptyLabel ?: ReactNode ;
60
60
flip ?: boolean ;
61
- instanceRef ?: Ref < Typeahead > ;
61
+ instanceRef ?: Ref < Typeahead < Option > > ;
62
62
isInvalid ?: boolean ;
63
63
isLoading ?: boolean ;
64
64
isValid ?: boolean ;
@@ -70,15 +70,15 @@ export interface TypeaheadComponentProps extends Partial<TypeaheadProps> {
70
70
positionFixed ?: boolean ;
71
71
renderInput ?: (
72
72
inputProps : TypeaheadInputProps ,
73
- props : TypeaheadManagerChildProps
73
+ props : TypeaheadManagerChildProps < Option >
74
74
) => JSX . Element ;
75
75
renderMenu ?: (
76
76
results : Option [ ] ,
77
- menuProps : RenderMenuProps ,
78
- state : TypeaheadManagerChildProps
77
+ menuProps : RenderMenuProps < Option > ,
78
+ state : TypeaheadManagerChildProps < Option >
79
79
) => JSX . Element ;
80
- renderMenuItemChildren ?: RenderMenuItemChildren ;
81
- renderToken ?: RenderToken ;
80
+ renderMenuItemChildren ?: RenderMenuItemChildren < Option > ;
81
+ renderToken ?: RenderToken < Option > ;
82
82
size ?: Size ;
83
83
style ?: CSSProperties ;
84
84
}
@@ -127,10 +127,10 @@ const defaultProps = {
127
127
isLoading : false ,
128
128
} ;
129
129
130
- const defaultRenderMenu = (
130
+ const defaultRenderMenu = < Option extends OptionType > (
131
131
results : Option [ ] ,
132
- menuProps : RenderMenuProps ,
133
- props : TypeaheadManagerChildProps
132
+ menuProps : RenderMenuProps < Option > ,
133
+ props : TypeaheadManagerChildProps < Option >
134
134
) => (
135
135
< TypeaheadMenu
136
136
{ ...menuProps }
@@ -140,9 +140,9 @@ const defaultRenderMenu = (
140
140
/>
141
141
) ;
142
142
143
- const defaultRenderToken = (
143
+ const defaultRenderToken = < Option extends OptionType > (
144
144
option : Option ,
145
- props : RenderTokenProps ,
145
+ props : RenderTokenProps < Option > ,
146
146
idx : number
147
147
) => (
148
148
< Token
@@ -160,9 +160,9 @@ const overlayPropKeys = [
160
160
'dropup' ,
161
161
'flip' ,
162
162
'positionFixed' ,
163
- ] as ( keyof TypeaheadComponentProps ) [ ] ;
163
+ ] as ( keyof TypeaheadComponentProps < OptionType > ) [ ] ;
164
164
165
- function getOverlayProps ( props : TypeaheadComponentProps ) {
165
+ function getOverlayProps < Option extends OptionType > ( props : TypeaheadComponentProps < Option > ) {
166
166
return pick ( props , overlayPropKeys ) ;
167
167
}
168
168
@@ -178,7 +178,7 @@ const RootClose = ({ children, onRootClose, ...props }: RootCloseProps) => {
178
178
return children ( attachRef ) ;
179
179
} ;
180
180
181
- class TypeaheadComponent extends React . Component < TypeaheadComponentProps > {
181
+ class TypeaheadComponent < Option extends OptionType > extends React . Component < TypeaheadComponentProps < Option > > {
182
182
static propTypes = propTypes ;
183
183
static defaultProps = defaultProps ;
184
184
@@ -190,7 +190,7 @@ class TypeaheadComponent extends React.Component<TypeaheadComponentProps> {
190
190
191
191
return (
192
192
< Typeahead { ...this . props } options = { options } ref = { instanceRef } >
193
- { ( props : TypeaheadManagerChildProps ) => {
193
+ { ( props : TypeaheadManagerChildProps < Option > ) => {
194
194
const { hideMenu, isMenuShown, results } = props ;
195
195
const auxContent = this . _renderAux ( props ) ;
196
196
@@ -238,7 +238,7 @@ class TypeaheadComponent extends React.Component<TypeaheadComponentProps> {
238
238
239
239
_renderInput = (
240
240
inputProps : TypeaheadInputProps ,
241
- props : TypeaheadManagerChildProps
241
+ props : TypeaheadManagerChildProps < Option >
242
242
) => {
243
243
const { isInvalid, isValid, multiple, renderInput, renderToken, size } =
244
244
this . props ;
@@ -279,7 +279,7 @@ class TypeaheadComponent extends React.Component<TypeaheadComponentProps> {
279
279
_renderMenu = (
280
280
results : Option [ ] ,
281
281
menuProps : OverlayRenderProps ,
282
- props : TypeaheadManagerChildProps
282
+ props : TypeaheadManagerChildProps < Option >
283
283
) => {
284
284
const {
285
285
emptyLabel,
@@ -306,7 +306,7 @@ class TypeaheadComponent extends React.Component<TypeaheadComponentProps> {
306
306
) ;
307
307
} ;
308
308
309
- _renderAux = ( { onClear, selected } : TypeaheadManagerChildProps ) => {
309
+ _renderAux = ( { onClear, selected } : TypeaheadManagerChildProps < Option > ) => {
310
310
const { clearButton, disabled, isLoading, size } = this . props ;
311
311
312
312
let content ;
@@ -331,6 +331,18 @@ class TypeaheadComponent extends React.Component<TypeaheadComponentProps> {
331
331
} ;
332
332
}
333
333
334
- export default forwardRef < Typeahead , TypeaheadComponentProps > ( ( props , ref ) => (
335
- < TypeaheadComponent { ...props } instanceRef = { ref } />
336
- ) ) ;
334
+ const TypeaheadComponentInner = < Option extends OptionType > ( props : TypeaheadComponentProps < Option > , ref : React . ForwardedRef < Typeahead < Option > > ) => < TypeaheadComponent { ...props } instanceRef = { ref } />
335
+
336
+ const TypeaheadComponentWithRef = forwardRef ( TypeaheadComponentInner ) ;
337
+
338
+ type TypeaheadComponentWithRefProps < Option extends OptionType > = TypeaheadComponentProps < Option > & {
339
+ ref ?: React . Ref < Typeahead < Option > > ;
340
+ } ;
341
+
342
+ export default function TypeaheadComp < Option extends OptionType > ( {
343
+ ref,
344
+ ...props
345
+ } : TypeaheadComponentWithRefProps < Option > ) {
346
+ // @ts -ignore
347
+ return < TypeaheadComponentWithRef ref = { ref } { ...props } /> ;
348
+ }
0 commit comments