This repository was archived by the owner on Jun 19, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathsingle-select.tsx
More file actions
138 lines (129 loc) · 4.83 KB
/
single-select.tsx
File metadata and controls
138 lines (129 loc) · 4.83 KB
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
import React, { useEffect, useRef, useState } from "react";
import { RiArrowDownSLine } from "react-icons/ri";
import clsx from "clsx";
import { DropdownMenu, DropdownMenuTrigger, DropdownMenuContent } from "@radix-ui/react-dropdown-menu";
import { Command, CommandGroup, CommandInput, CommandItem } from "../Cmd/command";
import Tooltip from "../Tooltip/tooltip";
interface SingleSelectProps {
value?: string;
onValueChange: (value: string) => void;
placeholder?: string;
inputPlaceholder?: string;
options: { label: string; value: string; icon?: React.ReactElement }[];
position?: "popper" | "item-aligned";
isSearchable?: boolean;
insetLabel?: string;
labelText: string;
}
const SingleSelect = ({
placeholder,
value,
onValueChange,
options,
position,
inputPlaceholder,
isSearchable = false,
insetLabel,
labelText,
}: SingleSelectProps) => {
const inputRef = useRef<HTMLInputElement>(null);
const [inputValue, setInputValue] = useState("");
const [isOpen, setIsOpen] = useState(false);
const current = options.find((option) => option.value === value);
useEffect(() => {
if (isOpen && isSearchable) {
// TODO: Revisit this. There's a timing issue where the input's ref is set when the dropdown is opened
// but it still can't receive focus. This is a workaround to focus the input after
// the dropdown is opened.
setTimeout(() => {
if (isOpen && isSearchable) {
inputRef.current?.focus();
}
});
}
}, [isOpen, isSearchable]);
return (
<DropdownMenu
open={isOpen}
modal={false}
onOpenChange={(open) => {
setIsOpen(open);
if (!open) {
setInputValue("");
}
}}
>
<DropdownMenuTrigger
data-inset-label={insetLabel}
className={clsx(
"flex w-full justify-between md:w-fit text-sm px-3 py-1.5 !border !border-slate-200 rounded-md bg-white data-[state=open]:border-orange-500 min-w-max",
insetLabel && `before:content-[attr(data-inset-label)] before:mr-1 before:font-normal before:text-slate-500`
)}
>
<span className="sr-only">{labelText}</span>
<Tooltip content={current?.label ?? placeholder}>
<div className="flex items-center w-44 ">
<p className="flex-grow text-start truncate">{current?.label ?? placeholder}</p>
</div>
</Tooltip>
<div className="flex items-center">
<RiArrowDownSLine size={20} className="w-5 text-slate-400" />
</div>
</DropdownMenuTrigger>
<DropdownMenuContent className="!p-0 z-50 relative bg-white w-[90vw] md:w-auto my-1 border shadow-lg rounded-lg">
<>
{options.length > 0 && (
<Command loop className="w-full px-0 pt-1 bg-transparent">
{isSearchable && (
<CommandInput
ref={inputRef}
placeholder={inputPlaceholder ?? "Search Items"}
value={inputValue}
onValueChange={setInputValue}
className="px-2 focus:ring-0"
/>
)}
<CommandGroup className="flex flex-col overflow-x-hidden overflow-y-scroll max-h-52">
{isOpen && options.length > 0
? options.map((option) => {
const Icon = option.icon;
return (
<CommandItem
key={option.value}
onSelect={(value) => {
setInputValue("");
setIsOpen(false);
onValueChange(option.value);
}}
onMouseDown={(e) => {
e.preventDefault();
setIsOpen(false);
onValueChange(option.value);
}}
onClick={() => {
setIsOpen(false);
onValueChange(option.value);
}}
className="!z-50 !cursor-pointer flex justify-between min-w-[7rem] items-center !px-3 rounded-md truncate break-words w-full"
>
{Icon ? (
<div className="flex items-center">
{Icon}
<span className="ml-1">{option.label}</span>
</div>
) : (
option.label
)}
</CommandItem>
);
})
: null}
</CommandGroup>
</Command>
)}
</>
</DropdownMenuContent>
</DropdownMenu>
);
};
export default SingleSelect;