deno.land / x / netzo@0.5.16 / components / combobox.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
// adapted from https://github.com/shadcn-ui/ui/issues/927#issuecomment-1788084995// @deno-types="npm:@types/react@18.2.60"import * as React from "react";
import { Button } from "./button.tsx";import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem,} from "./command.tsx";import { Popover, PopoverContent, PopoverTrigger } from "./popover.tsx";import { ScrollArea } from "./scroll-area.tsx";import { cn } from "./utils.ts";
export interface ComboboxOption { value: string; label: React.ReactNode;}
type ComboboxPropsSingle = { options: ComboboxOption[]; emptyText?: string; clearable?: boolean; selectPlaceholder?: string; searchPlaceholder?: string; multiple?: false; value?: string; onChange?: (value: string) => void;};
type ComboboxPropsMultiple = { options: ComboboxOption[]; emptyText?: string; clearable?: boolean; selectPlaceholder?: string; searchPlaceholder?: string; multiple: true; value?: string[]; onChange?: (value: string[]) => void;};
export type ComboboxProps = ComboboxPropsSingle | ComboboxPropsMultiple;
export const handleSingleSelect = ( props: ComboboxPropsSingle, option: ComboboxOption,) => { if (props.clearable) { props.onChange?.(option.value === props.value ? "" : option.value); } else { props.onChange?.(option.value); }};
export const handleMultipleSelect = ( props: ComboboxPropsMultiple, option: ComboboxOption,) => { if (props.value?.includes(option.value)) { if (!props.clearable && props.value.length === 1) return false; props.onChange?.( props.value.filter((value) => value !== option.value), ); } else { props.onChange?.([...(props.value ?? []), option.value]); }};
export const Combobox = React.forwardRef( (props: ComboboxProps, ref: React.ForwardedRef<HTMLInputElement>) => { const [open, setOpen] = React.useState(false);
return ( <Popover open={open} onOpenChange={setOpen}> <PopoverTrigger asChild> <Button role="combobox" variant="outline" aria-expanded={open} disabled={props.disabled} className="w-full justify-between hover:bg-secondary/20 active:scale-100" > <span className="line-clamp-1 text-left font-normal"> {props.multiple && props.value && props.value.length === 1 && ( <span className="mr-2">{props.value}</span> )}
{props.multiple && props.value && props.value.length > 1 && ( <span className="mr-2"> {props.value.length} options selected </span> )}
{!props.multiple && props.value && props.value !== "" && props.options.find((option) => option.value === props.value) ?.label}
{(!props.value || (props.multiple && props.value.length === 0)) && (props.selectPlaceholder ?? "Select an option")} </span> <i className={cn( "mdi-chevron-down ml-2 h-4 w-4 shrink-0 rotate-0 opacity-50 transition-transform", open && "rotate-180", )} /> </Button> </PopoverTrigger> <PopoverContent align="start" className="p-0"> <Command> <CommandInput ref={ref} placeholder={props.searchPlaceholder ?? "Search for an option"} /> <CommandEmpty>{props.emptyText ?? "No results found"}</CommandEmpty> <CommandGroup> <ScrollArea orientation="vertical"> <div className="max-h-60"> {props.options.map((option) => ( <CommandItem key={option.value as string} value={(option.label as string).toLowerCase().trim()} onSelect={(selectedLabel) => { const option = props.options.find( (option) => (option.label as string).toLowerCase().trim() === selectedLabel, );
if (!option) return null;
if (props.multiple) { handleMultipleSelect(props, option); } else { handleSingleSelect(props, option); setOpen(false); } }} > <i className={cn( "mdi-check mr-2 h-4 w-4 opacity-0", !props.multiple && props.value === option.value && "opacity-100", props.multiple && props.value?.includes(option.value) && "opacity-100", )} /> {option.label} </CommandItem> ))} </div> </ScrollArea> </CommandGroup> </Command> </PopoverContent> </Popover> ); },);
netzo

Version Info

Tagged at
3 weeks ago