forked from mrzachnugent/react-native-reusables
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselect.tsx
216 lines (198 loc) · 5.62 KB
/
select.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
import { FlashList, ListRenderItem } from '@shopify/flash-list';
import { Check, ChevronDown } from 'lucide-react-native';
import React from 'react';
import { GestureResponderEvent, Text, View, ViewStyle } from 'react-native';
import {
Popover,
PopoverClose,
PopoverContent,
PopoverTrigger,
} from '~/components/ui/popover';
import { cn } from '~/lib/utils';
import { Button, buttonTextVariants } from './button';
const SELECT_ITEM_HEIGHT = 50;
interface SelectOption {
value: string;
label: string;
}
interface SelectProps {
items: SelectOption[];
value: SelectOption | null;
onValueChange?: (value: SelectOption | null) => void;
}
interface SelectContext {
items: SelectOption[];
selected: SelectOption | null;
onValueChange?: (value: SelectOption | null) => void;
}
const SelectContext = React.createContext<SelectContext>({} as SelectContext);
const Select = React.forwardRef<
React.ElementRef<typeof Popover>,
React.ComponentPropsWithoutRef<typeof Popover> & SelectProps
>(({ onValueChange, value, items, ...props }, ref) => {
return (
<SelectContext.Provider
value={{
items,
selected: value,
onValueChange,
}}
>
<Popover ref={ref} {...props} />
</SelectContext.Provider>
);
});
Select.displayName = 'Select';
function useSelectContext() {
const context = React.useContext(SelectContext);
if (!context) {
throw new Error(
'Select compound components cannot be rendered outside the Select component'
);
}
return context;
}
const SelectTrigger = React.forwardRef<
React.ElementRef<typeof PopoverTrigger>,
Omit<React.ComponentPropsWithoutRef<typeof PopoverTrigger>, 'children'> & {
placeholder: string;
}
>(({ variant = 'outline', placeholder = 'Select...', ...props }, ref) => {
const { selected } = useSelectContext();
return (
<PopoverTrigger ref={ref} size='sm' variant='outline' {...props}>
{({ pressed }) => (
<View className='flex-1 flex-row justify-between items-center'>
<Text
className={buttonTextVariants({
variant: 'outline',
size: 'sm',
className: cn(
!selected?.value && 'opacity-50',
!selected?.value && pressed && 'opacity-30',
selected?.value && pressed && 'opacity-70'
),
})}
>
{selected?.value ?? placeholder}
</Text>
<ChevronDown
className={buttonTextVariants({
variant: 'outline',
className: 'opacity-50',
})}
/>
</View>
)}
</PopoverTrigger>
);
});
SelectTrigger.displayName = 'SelectTrigger';
const SelectList = React.forwardRef<
React.ElementRef<typeof FlashList<SelectOption>>,
Omit<
React.ComponentPropsWithoutRef<typeof FlashList<SelectOption>>,
'data' | 'estimatedItemSize' | 'initialScrollIndex'
> & {
containerProps?: React.ComponentPropsWithoutRef<typeof PopoverContent>;
}
>(({ containerProps, extraData, ...props }, ref) => {
const { selected, items } = useSelectContext();
const initialScrollIndex = React.useMemo(() => {
return selected
? items.findIndex((item) => item.value === selected.value)
: undefined;
}, [selected, items]);
return (
<PopoverContent
style={{ height: items.length * SELECT_ITEM_HEIGHT }}
className='p-0 max-h-[30%]'
{...containerProps}
>
<FlashList<SelectOption>
ref={ref}
data={items}
estimatedItemSize={SELECT_ITEM_HEIGHT - 1}
initialScrollIndex={initialScrollIndex}
extraData={[selected, extraData]}
{...props}
/>
</PopoverContent>
);
});
SelectList.displayName = 'SelectList';
type RenderSelectItem = ListRenderItem<SelectOption>;
const SelectItem = React.forwardRef<
React.ElementRef<typeof PopoverClose>,
Omit<
React.ComponentPropsWithoutRef<typeof PopoverClose>,
'children' | 'style' | 'asChild'
> & {
index: number;
item: SelectOption;
style?: ViewStyle;
}
>(
(
{ variant = 'outline', index, item, onPress, style, className, ...props },
ref
) => {
const { selected, onValueChange } = useSelectContext();
function handleOnPress(ev: GestureResponderEvent) {
onPress?.(ev);
if (selected?.value === item.value) {
onValueChange?.(null);
}
onValueChange?.(item);
}
return (
<PopoverClose asChild style={[{ height: SELECT_ITEM_HEIGHT }, style]}>
<Button
ref={ref}
variant={'ghost'}
className={cn(
index === 0 ? '' : 'border-t border-border',
'justify-start gap-3 pl-3',
className
)}
onPress={handleOnPress}
{...props}
>
{({ pressed }) => (
<>
<View>
<Check
className={cn(
'text-primary',
buttonTextVariants({
variant: 'ghost',
className:
selected?.value === item.value ? '' : 'opacity-0',
})
)}
/>
</View>
<Text
className={buttonTextVariants({
variant: 'ghost',
className: pressed ? 'opacity-70' : '',
})}
>
{item.label}
</Text>
</>
)}
</Button>
</PopoverClose>
);
}
);
SelectItem.displayName = 'SelectItem';
export {
Select,
SelectTrigger,
SelectList,
SelectItem,
type RenderSelectItem,
type SelectOption,
};