Skip to content

Commit e3571f5

Browse files
committed
fix: type mismatch in example
1 parent 52f7258 commit e3571f5

File tree

2 files changed

+62
-50
lines changed

2 files changed

+62
-50
lines changed

examples/App.tsx

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,23 @@ const EXAMPLES: Array<ExampleProps> = [
1414
{
1515
title: 'Multiple Select',
1616
description: 'Multiple select example',
17-
multiple: true,
17+
dropdownProps: { multiple: true },
1818
},
1919
{
2020
title: 'Multiple Select Badge Mode',
2121
description: 'Multiple select example - with badges',
22-
multiple: true,
23-
dropdownProps: { mode: 'BADGE', showBadgeDot: false },
22+
dropdownProps: { multiple: true, mode: 'BADGE', showBadgeDot: false },
2423
},
2524
{
2625
title: 'Multiple Select Badge Mode with Dots',
2726
description: 'Multiple select example - with badges and dots',
28-
multiple: true,
29-
dropdownProps: { mode: 'BADGE', showBadgeDot: true },
27+
dropdownProps: { multiple: true, mode: 'BADGE', showBadgeDot: true },
3028
},
3129
{
3230
title: 'Customized Multiple Select Badge Mode',
3331
description: 'Multiple select example - with badges',
34-
multiple: true,
3532
dropdownProps: {
33+
multiple: true,
3634
mode: 'BADGE',
3735
showBadgeDot: false,
3836
badgeDotStyle: {},
@@ -74,14 +72,14 @@ const EXAMPLES: Array<ExampleProps> = [
7472
{
7573
title: 'Multiple Search Example',
7674
description: 'This is the default dropdown picker - with search',
77-
multiple: true,
78-
dropdownProps: { searchable: true },
75+
76+
dropdownProps: { multiple: true, searchable: true },
7977
},
8078
{
8179
title: 'Multiple Search Clear on Select Example',
8280
description: 'This is the default dropdown picker - with search',
83-
multiple: true,
8481
dropdownProps: {
82+
multiple: true,
8583
searchable: true,
8684
clearSearchFieldOnSelect: true,
8785
mode: 'BADGE',
@@ -90,8 +88,7 @@ const EXAMPLES: Array<ExampleProps> = [
9088
{
9189
title: 'Modal Example',
9290
description: 'This is the default dropdown picker - with search',
93-
multiple: true,
94-
dropdownProps: { listMode: 'MODAL' },
91+
dropdownProps: { multiple: true, listMode: 'MODAL' },
9592
},
9693
];
9794

examples/example-src-files/example.tsx

Lines changed: 54 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,32 @@
1-
import React, { JSX, useState } from 'react';
1+
import React, { JSX, useMemo, useState } from 'react';
22
import { Button, StyleSheet, Text, useColorScheme, View } from 'react-native';
33
import 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+
1023
export 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

2132
const DEFAULT_ITEMS = [
@@ -64,7 +75,6 @@ const styles = StyleSheet.create({
6475
* @param props.multiple
6576
*/
6677
export 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

Comments
 (0)