-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathindex.tsx
128 lines (115 loc) · 3.67 KB
/
index.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
import clsx from "clsx";
import React, { ComponentProps } from "react";
import { ReactNode, useCallback, useEffect, useMemo, useState } from "react";
import { HiOutlineSelector, HiSearch } from "react-icons/hi";
import Select, { SingleValue } from "react-select";
export interface OptionItem {
readonly label: ReactNode;
readonly value: string;
}
export interface GroupedOptionItem {
label: string;
options: OptionItem[];
}
export interface SearchSelectProps {
options: OptionItem[] | GroupedOptionItem[];
name?: string;
components?: ComponentProps<typeof Select<OptionItem>>["components"];
onChange: (val: SingleValue<OptionItem>) => void;
selected: OptionItem;
className?: string;
selectContainerClassName?: string;
toggleBtn?: React.ReactNode;
menuPlacement?: "auto" | "bottom" | "top";
}
function RenderLabel(option: OptionItem) {
return option?.label || "";
}
const Blanket = (props: JSX.IntrinsicElements["div"]) => (
<div className="fixed bottom-0 left-0 top-0 right-0 z-10" {...props} />
);
export const DropdownIndicator = () => (
<HiSearch className="text-gray-400" size={24} />
);
const defaultComponents = {
RenderSelection: RenderLabel
};
export function SearchSelect({
options,
name,
onChange,
components,
selected,
className,
selectContainerClassName = "bg-white shadow-card rounded-md mt-2 absolute z-20",
toggleBtn,
menuPlacement
}: SearchSelectProps) {
const [isOpen, setOpen] = useState(false);
const [value, setValue] = useState<SingleValue<OptionItem>>(selected);
const changeCb = useCallback(
(value: SingleValue<OptionItem>) => {
setValue(value);
setOpen(!isOpen);
if (onChange) {
onChange(value);
}
},
[onChange, setValue, isOpen]
);
useEffect(() => {
setValue(selected);
}, [selected]);
const { RenderSelection, ...remainingComponents } = useMemo(
() => ({ ...defaultComponents, ...components }),
[components]
);
return (
<div className={clsx("relative h-full", className)}>
{!toggleBtn && (
<button
className="relative cursor-pointer w-full h-full items-center bg-white border border-gray-300 rounded-md shadow-sm px-2 py-1 text-left focus:outline-none focus:ring-1 focus:ring-blue-500 focus:border-blue-500 sm:text-sm flex"
onClick={() => setOpen(!isOpen)}
>
{name && (
<span className="text-gray-500 mr-1 whitespace-nowrap">{name}</span>
)}
{/* @ts-expect-error */}
<RenderSelection label={value?.label!} value={value?.value!} />
<span className="absolute inset-y-0 right-0 flex items-center text-gray-400 pointer-events-none">
<HiOutlineSelector size={24} />
</span>
</button>
)}
{toggleBtn && <div onClick={() => setOpen(!isOpen)}>{toggleBtn}</div>}
{isOpen ? (
<>
<div className={selectContainerClassName}>
<Select<OptionItem>
autoFocus
backspaceRemovesValue={false}
components={{
...remainingComponents,
DropdownIndicator,
IndicatorSeparator: null
}}
controlShouldRenderValue={false}
hideSelectedOptions={false}
isClearable={false}
menuIsOpen
onChange={(val) => {
changeCb(val);
}}
options={options}
placeholder="Search..."
tabSelectsValue={false}
value={value}
menuPlacement={menuPlacement}
/>
</div>
<Blanket onClick={() => setOpen(!isOpen)} />
</>
) : null}
</div>
);
}