forked from mrzachnugent/react-native-reusables
-
Notifications
You must be signed in to change notification settings - Fork 0
/
combobox.tsx
229 lines (216 loc) · 6.73 KB
/
combobox.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import { Check, ChevronsUpDown, Search } from 'lucide-react-native';
import React from 'react';
import { ListRenderItemInfo, Text, View } from 'react-native';
import { useSafeAreaInsets } from 'react-native-safe-area-context';
import {
BottomSheet,
BottomSheetContent,
BottomSheetFlatList,
BottomSheetHeader,
BottomSheetOpenTrigger,
BottomSheetTextInput,
useBottomSheet,
} from '~/components/ui/bottom-sheet';
import {
Button,
buttonTextVariants,
buttonVariants,
} from '~/components/ui/button';
import { cn } from '~/lib/utils';
// TODO: Fix bottom sheet content UI - Too big/too much space
const HEADER_HEIGHT = 130;
interface ComboboxOption {
label: string;
value: string;
}
const Combobox = React.forwardRef<
React.ElementRef<typeof Button>,
Omit<React.ComponentPropsWithoutRef<typeof Button>, 'children'> & {
items: ComboboxOption[];
placeholder?: string;
inputProps?: React.ComponentPropsWithoutRef<typeof BottomSheetTextInput>;
emptyText?: string;
defaultSelectedItem?: ComboboxOption | null;
selectedItem?: ComboboxOption | null;
onSelectedItemChange?: (option: ComboboxOption | null) => void;
}
>(
(
{
className,
textClass,
variant = 'outline',
size = 'sm',
inputProps,
placeholder,
items,
emptyText = 'Nothing found...',
defaultSelectedItem = null,
selectedItem: selectedItemProp,
onSelectedItemChange,
...props
},
ref
) => {
const insets = useSafeAreaInsets();
const [search, setSearch] = React.useState('');
const [selectedItem, setSelectedItem] =
React.useState<ComboboxOption | null>(defaultSelectedItem);
const bottomSheet = useBottomSheet();
const inputRef =
React.useRef<React.ComponentRef<typeof BottomSheetTextInput>>(null);
const listItems = React.useMemo(() => {
return search
? items.filter((item) => {
return item.label
.toLocaleLowerCase()
.includes(search.toLocaleLowerCase());
})
: items;
}, [items, search]);
function onItemChange(listItem: ComboboxOption) {
if (selectedItemProp?.value === listItem.value) {
return null;
}
setSearch('');
bottomSheet.close();
return listItem;
}
const renderItem = React.useCallback(
({ item }: ListRenderItemInfo<unknown>) => {
const listItem = item as ComboboxOption;
const isSelected = onSelectedItemChange
? selectedItemProp?.value === listItem.value
: selectedItem?.value === listItem.value;
return (
<Button
variant='ghost'
className='items-center flex-row justify-between px-3 py-4'
style={{ minHeight: 70 }}
onPress={() => {
if (onSelectedItemChange) {
onSelectedItemChange(onItemChange(listItem));
return;
}
setSelectedItem(onItemChange(listItem));
}}
>
<View className='flex-row flex-1'>
<Text className={'text-foreground text-xl'}>
{listItem.label}
</Text>
</View>
{isSelected && (
<Check size={24} className={'text-foreground px-6 mt-1.5'} />
)}
</Button>
);
},
[selectedItem, selectedItemProp]
);
function onSubmitEditing() {
const firstItem = listItems[0];
if (!firstItem) return;
if (onSelectedItemChange) {
onSelectedItemChange(firstItem);
} else {
setSelectedItem(firstItem);
}
bottomSheet.close();
}
function onSearchIconPress() {
if (!inputRef.current) return;
const input = inputRef.current;
if (input && 'focus' in input && typeof input.focus === 'function') {
input.focus();
}
}
const itemSelected = onSelectedItemChange ? selectedItemProp : selectedItem;
return (
<BottomSheet>
<BottomSheetOpenTrigger
ref={ref}
className={buttonVariants({
variant,
size,
className: cn('flex-row w-full', className),
})}
role='combobox'
{...props}
>
<View className='flex-1 flex-row justify-between '>
<Text
className={buttonTextVariants({
variant,
size,
className: cn(!itemSelected && 'opacity-50', textClass),
})}
numberOfLines={1}
>
{itemSelected ? itemSelected.label : placeholder ?? ''}
</Text>
<ChevronsUpDown className='text-foreground ml-2 opacity-50' />
</View>
</BottomSheetOpenTrigger>
<BottomSheetContent
ref={bottomSheet.ref}
onDismiss={() => {
setSearch('');
}}
>
<BottomSheetHeader className='border-b-0'>
<Text className='text-foreground text-xl font-bold text-center px-0.5'>
{placeholder}
</Text>
</BottomSheetHeader>
<View className='relative px-4 border-b border-border pb-4'>
<BottomSheetTextInput
role='searchbox'
ref={inputRef}
className='pl-12'
value={search}
onChangeText={setSearch}
onSubmitEditing={onSubmitEditing}
returnKeyType='next'
clearButtonMode='while-editing'
placeholder='Search...'
{...inputProps}
/>
<Button
variant={'ghost'}
size='sm'
className='absolute left-4 top-2.5'
onPress={onSearchIconPress}
>
<Search size={18} className='text-foreground opacity-50' />
</Button>
</View>
<BottomSheetFlatList
data={listItems}
contentContainerStyle={{
paddingBottom: insets.bottom + HEADER_HEIGHT,
}}
renderItem={renderItem}
keyExtractor={(item) => (item as ComboboxOption).value}
className={'px-4'}
keyboardShouldPersistTaps='handled'
ListEmptyComponent={() => {
return (
<View
className='items-center flex-row justify-center flex-1 px-3 py-5'
style={{ minHeight: 70 }}
>
<Text className={'text-muted-foreground text-xl text-center'}>
{emptyText}
</Text>
</View>
);
}}
/>
</BottomSheetContent>
</BottomSheet>
);
}
);
Combobox.displayName = 'Combobox';
export { Combobox, type ComboboxOption };