@@ -32,7 +32,7 @@ import {
3232import { checkPropType , inputPropsType , sizeType } from '../../propTypes' ;
3333
3434import {
35- Option ,
35+ OptionType ,
3636 RefElement ,
3737 RenderToken ,
3838 RenderTokenProps ,
@@ -42,23 +42,23 @@ import {
4242 TypeaheadManagerChildProps ,
4343} from '../../types' ;
4444
45- export interface RenderMenuProps
45+ export interface RenderMenuProps < Option extends OptionType >
4646 extends Omit <
47- TypeaheadMenuProps ,
47+ TypeaheadMenuProps < Option > ,
4848 'labelKey' | 'options' | 'renderMenuItemChildren' | 'text'
4949 > {
50- renderMenuItemChildren ?: RenderMenuItemChildren ;
50+ renderMenuItemChildren ?: RenderMenuItemChildren < Option > ;
5151}
5252
53- export interface TypeaheadComponentProps extends Partial < TypeaheadProps > {
53+ export interface TypeaheadComponentProps < Option extends OptionType > extends Partial < TypeaheadProps < Option > > {
5454 align ?: Align ;
5555 className ?: string ;
5656 clearButton ?: boolean ;
5757 disabled ?: boolean ;
5858 dropup ?: boolean ;
5959 emptyLabel ?: ReactNode ;
6060 flip ?: boolean ;
61- instanceRef ?: Ref < Typeahead > ;
61+ instanceRef ?: Ref < Typeahead < Option > > ;
6262 isInvalid ?: boolean ;
6363 isLoading ?: boolean ;
6464 isValid ?: boolean ;
@@ -70,15 +70,15 @@ export interface TypeaheadComponentProps extends Partial<TypeaheadProps> {
7070 positionFixed ?: boolean ;
7171 renderInput ?: (
7272 inputProps : TypeaheadInputProps ,
73- props : TypeaheadManagerChildProps
73+ props : TypeaheadManagerChildProps < Option >
7474 ) => JSX . Element ;
7575 renderMenu ?: (
7676 results : Option [ ] ,
77- menuProps : RenderMenuProps ,
78- state : TypeaheadManagerChildProps
77+ menuProps : RenderMenuProps < Option > ,
78+ state : TypeaheadManagerChildProps < Option >
7979 ) => JSX . Element ;
80- renderMenuItemChildren ?: RenderMenuItemChildren ;
81- renderToken ?: RenderToken ;
80+ renderMenuItemChildren ?: RenderMenuItemChildren < Option > ;
81+ renderToken ?: RenderToken < Option > ;
8282 size ?: Size ;
8383 style ?: CSSProperties ;
8484}
@@ -127,10 +127,10 @@ const defaultProps = {
127127 isLoading : false ,
128128} ;
129129
130- const defaultRenderMenu = (
130+ const defaultRenderMenu = < Option extends OptionType > (
131131 results : Option [ ] ,
132- menuProps : RenderMenuProps ,
133- props : TypeaheadManagerChildProps
132+ menuProps : RenderMenuProps < Option > ,
133+ props : TypeaheadManagerChildProps < Option >
134134) => (
135135 < TypeaheadMenu
136136 { ...menuProps }
@@ -140,9 +140,9 @@ const defaultRenderMenu = (
140140 />
141141) ;
142142
143- const defaultRenderToken = (
143+ const defaultRenderToken = < Option extends OptionType > (
144144 option : Option ,
145- props : RenderTokenProps ,
145+ props : RenderTokenProps < Option > ,
146146 idx : number
147147) => (
148148 < Token
@@ -160,9 +160,9 @@ const overlayPropKeys = [
160160 'dropup' ,
161161 'flip' ,
162162 'positionFixed' ,
163- ] as ( keyof TypeaheadComponentProps ) [ ] ;
163+ ] as ( keyof TypeaheadComponentProps < OptionType > ) [ ] ;
164164
165- function getOverlayProps ( props : TypeaheadComponentProps ) {
165+ function getOverlayProps < Option extends OptionType > ( props : TypeaheadComponentProps < Option > ) {
166166 return pick ( props , overlayPropKeys ) ;
167167}
168168
@@ -178,7 +178,7 @@ const RootClose = ({ children, onRootClose, ...props }: RootCloseProps) => {
178178 return children ( attachRef ) ;
179179} ;
180180
181- class TypeaheadComponent extends React . Component < TypeaheadComponentProps > {
181+ class TypeaheadComponent < Option extends OptionType > extends React . Component < TypeaheadComponentProps < Option > > {
182182 static propTypes = propTypes ;
183183 static defaultProps = defaultProps ;
184184
@@ -190,7 +190,7 @@ class TypeaheadComponent extends React.Component<TypeaheadComponentProps> {
190190
191191 return (
192192 < Typeahead { ...this . props } options = { options } ref = { instanceRef } >
193- { ( props : TypeaheadManagerChildProps ) => {
193+ { ( props : TypeaheadManagerChildProps < Option > ) => {
194194 const { hideMenu, isMenuShown, results } = props ;
195195 const auxContent = this . _renderAux ( props ) ;
196196
@@ -238,7 +238,7 @@ class TypeaheadComponent extends React.Component<TypeaheadComponentProps> {
238238
239239 _renderInput = (
240240 inputProps : TypeaheadInputProps ,
241- props : TypeaheadManagerChildProps
241+ props : TypeaheadManagerChildProps < Option >
242242 ) => {
243243 const { isInvalid, isValid, multiple, renderInput, renderToken, size } =
244244 this . props ;
@@ -279,7 +279,7 @@ class TypeaheadComponent extends React.Component<TypeaheadComponentProps> {
279279 _renderMenu = (
280280 results : Option [ ] ,
281281 menuProps : OverlayRenderProps ,
282- props : TypeaheadManagerChildProps
282+ props : TypeaheadManagerChildProps < Option >
283283 ) => {
284284 const {
285285 emptyLabel,
@@ -306,7 +306,7 @@ class TypeaheadComponent extends React.Component<TypeaheadComponentProps> {
306306 ) ;
307307 } ;
308308
309- _renderAux = ( { onClear, selected } : TypeaheadManagerChildProps ) => {
309+ _renderAux = ( { onClear, selected } : TypeaheadManagerChildProps < Option > ) => {
310310 const { clearButton, disabled, isLoading, size } = this . props ;
311311
312312 let content ;
@@ -331,6 +331,18 @@ class TypeaheadComponent extends React.Component<TypeaheadComponentProps> {
331331 } ;
332332}
333333
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