-
Notifications
You must be signed in to change notification settings - Fork 2
/
multi-select-combo-box.tsx
145 lines (135 loc) · 4.61 KB
/
multi-select-combo-box.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
import { cn } from '@/utils/shadcn-ui.util';
import { Check, ChevronsUpDown, Star } from 'lucide-react';
import { ReactNode, useCallback, useMemo, useState } from 'react';
import { Button } from '../shadcn-ui/button';
import {
Command,
CommandEmpty,
CommandGroup,
CommandInput,
CommandItem,
CommandList,
} from '../shadcn-ui/command';
import { Popover, PopoverContent, PopoverTrigger } from '../shadcn-ui/popover';
type MultiSelectComboBoxEntry = {
value: string;
label: string;
starred?: boolean;
};
interface MultiSelectComboBoxProps {
options: MultiSelectComboBoxEntry[];
getOptionsCount?: (option: MultiSelectComboBoxEntry) => number;
selected: string[];
onChange: (values: string[]) => void;
placeholder: string;
customSelectedText?: string;
sortSelected?: boolean;
icon?: ReactNode;
}
function MultiSelectComboBox({
options,
getOptionsCount = undefined,
selected,
onChange,
placeholder,
customSelectedText,
sortSelected = false,
icon = undefined,
}: MultiSelectComboBoxProps) {
const [open, setOpen] = useState(false);
const handleSelect = useCallback(
(value: string) => {
onChange(
selected.includes(value) ? selected.filter((item) => item !== value) : [...selected, value],
);
},
[selected, onChange],
);
const getPlaceholderText = () => {
if (selected.length === 1)
return options.find((option) => option.value === selected[0])?.label ?? placeholder;
if (customSelectedText) return customSelectedText;
return placeholder;
};
const sortedOptions = useMemo(() => {
if (!sortSelected) return options;
const starredItems = options
.filter((opt) => opt.starred)
.sort((a, b) => a.label.localeCompare(b.label));
const nonStarredItems = options
.filter((opt) => !opt.starred)
.sort((a, b) => {
const aSelected = selected.includes(a.value);
const bSelected = selected.includes(b.value);
if (aSelected && !bSelected) return -1;
if (!aSelected && bSelected) return 1;
return a.label.localeCompare(b.label);
});
return [...starredItems, ...nonStarredItems];
}, [options, selected, sortSelected]);
return (
<Popover open={open} onOpenChange={setOpen}>
<PopoverTrigger asChild>
<Button
variant="outline"
role="combobox"
aria-expanded={open}
className={cn(
'tw-w-full tw-justify-between',
selected.length > 0 && selected.length < options.length && 'tw-border-primary',
)}
>
<div className="tw-flex tw-items-center tw-gap-2">
<div className="tw-ml-2 tw-h-4 tw-w-4 tw-shrink-0 tw-opacity-50">
<span className="tw-flex tw-h-full tw-w-full tw-items-center tw-justify-center">
{icon}
</span>
</div>
{getPlaceholderText()}
</div>
<ChevronsUpDown className="tw-ml-2 tw-h-4 tw-w-4 tw-shrink-0 tw-opacity-50" />
</Button>
</PopoverTrigger>
<PopoverContent align="start" className="tw-w-full tw-p-0">
<Command>
<CommandInput placeholder={`Search ${placeholder.toLowerCase()}...`} />
<CommandList>
<CommandEmpty>No item found.</CommandEmpty>
<CommandGroup>
{sortedOptions.map((option) => {
const count: number | undefined = getOptionsCount
? getOptionsCount(option)
: undefined;
return (
<CommandItem
key={option.value}
value={option.value}
onSelect={handleSelect}
className="tw-flex tw-items-center tw-gap-2"
>
<div className="w-4">
<Check
className={cn(
'tw-h-4 tw-w-4',
selected.includes(option.value) ? 'tw-opacity-100' : 'tw-opacity-0',
)}
/>
</div>
<div className="tw-w-4">
{option.starred && <Star className="tw-h-4 tw-w-4" />}
</div>
<div className="tw-flex-grow">{option.label}</div>
{getOptionsCount && (
<div className="tw-w-10 tw-text-right tw-text-muted-foreground">{count}</div>
)}
</CommandItem>
);
})}
</CommandGroup>
</CommandList>
</Command>
</PopoverContent>
</Popover>
);
}
export default MultiSelectComboBox;