1- import React , { JSX , useState } from 'react' ;
1+ import React , { JSX , useMemo , useState } from 'react' ;
22import { Button , StyleSheet , Text , useColorScheme , View } from 'react-native' ;
33import DropDownPicker , {
4- DropDownPickerMultipleProps ,
5- DropDownPickerProps ,
6- DropDownPickerSingleProps ,
4+ DropDownPickerIsMultipleProps ,
5+ DropDownPickerIsSingleProps ,
76 ItemType ,
7+ ValueType ,
88} from 'react-native-dropdown-picker' ;
99
10+ // Omit these types from the example props
11+ type CommonOmitKeys = 'setValue' | 'value' | 'open' | 'items' | 'setOpen' ;
12+
13+ type SingleDropdownProps < T extends ValueType > = Omit <
14+ Partial < DropDownPickerIsSingleProps < T > > ,
15+ CommonOmitKeys
16+ > ;
17+
18+ type MultipleDropdownProps < T extends ValueType > = Omit <
19+ Partial < DropDownPickerIsMultipleProps < T > > ,
20+ CommonOmitKeys
21+ > ;
22+
1023export interface ExampleProps {
11- multiple ?: boolean ;
1224 title : string ;
1325 description ?: string ;
1426 placeholder ?: string ;
1527 multipleText ?: string ;
16- // For the sake of keeping the examples simple for now
17- items ?: Array < ItemType < string > > ;
18- dropdownProps ?: Partial < DropDownPickerProps < string > > ;
28+ items ?: Array < ItemType < ValueType > > ;
29+ dropdownProps ?: MultipleDropdownProps < ValueType > | SingleDropdownProps < ValueType > ;
1930}
2031
2132const DEFAULT_ITEMS = [
@@ -64,7 +75,6 @@ const styles = StyleSheet.create({
6475 * @param props.multiple
6576 */
6677export default function DropDownPickerExample ( {
67- multiple = false ,
6878 title,
6979 description,
7080 dropdownProps,
@@ -73,26 +83,21 @@ export default function DropDownPickerExample({
7383 items = DEFAULT_ITEMS ,
7484} : ExampleProps ) : JSX . Element {
7585 const [ open , setOpen ] = useState < boolean > ( false ) ;
76- const [ singleValue , setSingleValue ] = useState < string | null > ( null ) ;
77- const [ multiValue , setMultiValue ] = useState < Array < string > | null > ( null ) ;
86+ const [ singleValue , setSingleValue ] = useState < ValueType | null > ( null ) ;
87+ const [ multiValue , setMultiValue ] = useState < Array < ValueType > | null > ( null ) ;
7888 const colorScheme = useColorScheme ( ) ;
7989 const color = colorScheme === 'dark' ? '#fff' : '#222' ;
8090 const theme = colorScheme === 'dark' ? 'DARK' : 'LIGHT' ;
8191
82- const [ _items , setItems ] = useState < Array < ItemType < string > > > ( items ) ;
83-
84- return (
85- // eslint-disable-next-line react-native/no-inline-styles
86- < View style = { { ...styles . exampleContainer , zIndex : open ? 10 : 1 } } >
87- < View >
88- < Text style = { { ...styles . title , color } } > { title } </ Text >
89- { description && (
90- < Text style = { { ...styles . description , color } } > { description } </ Text >
91- ) }
92- </ View >
92+ const [ _items , setItems ] = useState < Array < ItemType < ValueType > > > ( items ) ;
93+ const isMultiple = dropdownProps ?. multiple ;
9394
94- { multiple ? (
95- < DropDownPicker < string >
95+ const RenderDropDown = ( ) => {
96+ if ( isMultiple ) {
97+ const { ..._dropdownProps } = dropdownProps ;
98+ delete _dropdownProps . multiple // remove multiple prop to hard set as true
99+ return (
100+ < DropDownPicker < ValueType >
96101 open = { open }
97102 value = { multiValue }
98103 items = { _items }
@@ -103,13 +108,12 @@ export default function DropDownPickerExample({
103108 placeholder = { placeholder }
104109 multipleText = { multipleText }
105110 multiple
106- { ...( dropdownProps as Omit <
107- DropDownPickerMultipleProps < string > ,
108- 'setValue' | 'value' | 'multiple'
109- > ) }
111+ { ..._dropdownProps as MultipleDropdownProps < ValueType > }
110112 />
111- ) : (
112- < DropDownPicker < string >
113+ )
114+ } else {
115+ return (
116+ < DropDownPicker < ValueType >
113117 open = { open }
114118 value = { singleValue }
115119 items = { _items }
@@ -119,23 +123,34 @@ export default function DropDownPickerExample({
119123 theme = { theme }
120124 placeholder = { placeholder }
121125 multiple = { false }
122- { ...( dropdownProps as Omit <
123- DropDownPickerSingleProps < string > ,
124- 'setValue' | 'value'
125- > ) }
126+ { ...dropdownProps as SingleDropdownProps < ValueType > }
126127 />
127- ) }
128+ )
129+ }
130+ }
131+
132+ return (
133+ // eslint-disable-next-line react-native/no-inline-styles
134+ < View style = { { ...styles . exampleContainer , zIndex : open ? 10 : 1 } } >
135+ < View >
136+ < Text style = { { ...styles . title , color } } > { title } </ Text >
137+ { description && (
138+ < Text style = { { ...styles . description , color } } > { description } </ Text >
139+ ) }
140+ </ View >
141+
142+ { RenderDropDown ( ) }
128143
129144 < View style = { { ...styles . body } } >
130145 < Text style = { { ...styles . description , color } } >
131- { multiple ? 'Fruits currently are: ' : 'Fruit currently is: ' }
132- { multiple ? JSON . stringify ( multiValue ) : JSON . stringify ( singleValue ) }
146+ { isMultiple ? 'Fruits currently are: ' : 'Fruit currently is: ' }
147+ { isMultiple ? JSON . stringify ( multiValue ) : JSON . stringify ( singleValue ) }
133148 </ Text >
134149
135150 < Button
136- title = { multiple ? 'Clear fruits' : 'Clear fruit' }
151+ title = { isMultiple ? 'Clear fruits' : 'Clear fruit' }
137152 onPress = { ( ) : void => {
138- if ( multiple ) setMultiValue ( null ) ;
153+ if ( isMultiple ) setMultiValue ( null ) ;
139154 else setSingleValue ( null ) ;
140155 } }
141156 />
0 commit comments