forked from Mobilecn-UI/nativecn-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDropDown.tsx
101 lines (89 loc) · 2.28 KB
/
DropDown.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
/* eslint-disable prettier/prettier */
import React, {
cloneElement,
createContext,
useContext,
useState,
} from 'react';
import { Text, View } from 'react-native';
import { cn } from '../lib/utils';
interface DropDownContextType {
open: boolean;
setOpen: (open: boolean) => void;
}
const DropDownContext = createContext<DropDownContextType | undefined>(
undefined
);
const DropDown = ({ children }: { children: React.ReactNode }) => {
const [open, setOpen] = useState<boolean>(false);
return (
<DropDownContext.Provider value={{ open, setOpen }}>
<View className="relative">{children}</View>
</DropDownContext.Provider>
);
};
const DropDownTrigger = ({ children }: any) => {
const { setOpen } = useDropdown();
return cloneElement(children, {
onPress: () => setOpen((prev: any) => !prev),
});
};
type DropDownContentTypes = {
className?: string;
children: React.ReactNode;
};
const DropDownContent = ({ className, children }: DropDownContentTypes) => {
const { open } = useDropdown();
return (
<>
{open && (
<View
className={cn(
'min-w-[8rem] w-full absolute flex gap-3 overflow-hidden rounded-md border border-border bg-background text-popover-foreground shadow-md mt-3 p-3 top-12 mx-auto justify-center z-50',
className
)}
>
{children}
</View>
)}
</>
);
};
type DropDownLabelProps = {
labelTitle: string;
};
const DropDownLabel = ({ labelTitle }: DropDownLabelProps) => {
return (
<Text className="text-xl font-semibold text-primary">{labelTitle}</Text>
);
};
type DropDownItemProps = {
children: React.ReactNode;
className?: string;
};
const DropDownItem = ({ children, className }: DropDownItemProps) => {
return (
<View className={cn('p-2 border border-border rounded-md', className)}>
{children}
</View>
);
};
const DropDownItemSeparator = () => {
return <View className="h-[1px] bg-border flex-1" />;
};
const useDropdown = () => {
const context = useContext(DropDownContext);
if (!context) {
throw new Error('useDropdown must be used within a DropdownProvider');
}
return context;
};
export {
DropDown,
DropDownTrigger,
DropDownContent,
DropDownLabel,
DropDownItemSeparator,
DropDownItem,
useDropdown,
};