|
| 1 | +import React, { useState } from 'react'; |
| 2 | +import { StyleSheet, Button, Text, View, useColorScheme } from 'react-native'; |
| 3 | +import DropDownPicker, { ItemType,DropDownPickerProps } from 'react-native-dropdown-picker'; |
| 4 | + |
| 5 | +type ExampleProps = { |
| 6 | + multiple?: boolean; |
| 7 | + title: string; |
| 8 | + description?: string; |
| 9 | + dropdownProps?: Partial<DropDownPickerProps<string>>; |
| 10 | +}; |
| 11 | + |
| 12 | +const styles = StyleSheet.create({ |
| 13 | + title: { |
| 14 | + fontSize: 24, |
| 15 | + marginBottom: 4, |
| 16 | + fontWeight: 'bold', |
| 17 | + }, |
| 18 | + description: { |
| 19 | + fontSize: 12, |
| 20 | + }, |
| 21 | + exampleContainer: { |
| 22 | + display: 'flex', |
| 23 | + flexDirection: 'column', |
| 24 | + gap: 16 |
| 25 | + } |
| 26 | +}); |
| 27 | + |
| 28 | +/** |
| 29 | + * |
| 30 | + * @param props |
| 31 | + * @param props.multiple |
| 32 | + */ |
| 33 | +export default function DropDownPickerExample({ |
| 34 | + multiple = false, |
| 35 | + title, |
| 36 | + description, |
| 37 | + dropdownProps, |
| 38 | +}: ExampleProps): JSX.Element { |
| 39 | + const [open, setOpen] = useState<boolean>(false); |
| 40 | + const [singleValue, setSingleValue] = useState<string | null>(null); |
| 41 | + const [multiValue, setMultiValue] = useState<Array<string> | null>(null); |
| 42 | + const colorScheme = useColorScheme(); |
| 43 | + const color = colorScheme === 'dark' ? '#fff' : '#222'; |
| 44 | + const theme = colorScheme === 'dark' ? 'DARK' : 'LIGHT'; |
| 45 | + |
| 46 | + const [items, setItems] = useState<Array<ItemType<string>>>([ |
| 47 | + { label: 'Apple', value: 'apple' }, |
| 48 | + { label: 'Banana', value: 'banana' }, |
| 49 | + { label: 'Nectarines', value: 'nectarines' }, |
| 50 | + { label: 'Kiwis', value: 'kiwis' }, |
| 51 | + { label: 'Raspberries', value: 'raspberries' }, |
| 52 | + { label: 'Pears', value: 'pears' }, |
| 53 | + ]); |
| 54 | + |
| 55 | + return ( |
| 56 | + <View style={styles.exampleContainer}> |
| 57 | + <View> |
| 58 | + <Text style={{...styles.title, color}}>{title}</Text> |
| 59 | + {description && ( |
| 60 | + <Text style={{...styles.description, color}}>{description}</Text> |
| 61 | + )} |
| 62 | + </View> |
| 63 | + |
| 64 | + <View> |
| 65 | + {multiple ? ( |
| 66 | + <DropDownPicker |
| 67 | + open={open} |
| 68 | + value={multiValue} |
| 69 | + items={items} |
| 70 | + setOpen={setOpen} |
| 71 | + setValue={setMultiValue} |
| 72 | + setItems={setItems} |
| 73 | + theme={theme} |
| 74 | + placeholder='Choose a fruit' |
| 75 | + multiple |
| 76 | + multipleText='You have chosen {count} fruits.' |
| 77 | + {...dropdownProps} |
| 78 | + /> |
| 79 | + ) : ( |
| 80 | + <DropDownPicker |
| 81 | + open={open} |
| 82 | + value={singleValue} |
| 83 | + items={items} |
| 84 | + setOpen={setOpen} |
| 85 | + setValue={setSingleValue} |
| 86 | + setItems={setItems} |
| 87 | + theme={theme} |
| 88 | + placeholder='Choose a fruit' |
| 89 | + multiple={false} |
| 90 | + {...dropdownProps} |
| 91 | + /> |
| 92 | + )} |
| 93 | + </View> |
| 94 | + |
| 95 | + <View> |
| 96 | + <Text style={{...styles.description, color}}> |
| 97 | + {multiple ? 'Fruits currently are: ' : 'Fruit currently is: '} |
| 98 | + {multiple |
| 99 | + ? JSON.stringify(multiValue) |
| 100 | + : JSON.stringify(singleValue)} |
| 101 | + </Text> |
| 102 | + </View> |
| 103 | + |
| 104 | + <View> |
| 105 | + <Button |
| 106 | + title={multiple ? 'Clear fruits' : 'Clear fruit'} |
| 107 | + onPress={(): void => { |
| 108 | + if (multiple) setMultiValue(null); |
| 109 | + else setSingleValue(null); |
| 110 | + }} |
| 111 | + /> |
| 112 | + </View> |
| 113 | + </View> |
| 114 | + ); |
| 115 | +} |
0 commit comments