-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselect.tsx
More file actions
282 lines (268 loc) · 11.7 KB
/
select.tsx
File metadata and controls
282 lines (268 loc) · 11.7 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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import * as PopoverPrimitive from '@radix-ui/react-popover';
import { Popover } from '@radix-ui/react-popover';
import { Check as DefaultCheckIcon, ChevronDown as DefaultChevronIcon } from 'lucide-react';
import type * as React from 'react';
import { useOverlayTriggerState } from 'react-stately';
import { PopoverTrigger } from './popover';
import { cn } from './utils';
import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList } from './command';
import {
forwardRef,
type Ref,
useEffect,
useState,
type ButtonHTMLAttributes,
type ComponentType,
type RefAttributes,
useId,
useRef,
} from 'react';
export interface SelectOption {
label: string;
value: string;
}
export interface SelectUIComponents {
Trigger?: ComponentType<ButtonHTMLAttributes<HTMLButtonElement> & RefAttributes<HTMLButtonElement>>;
Item?: ComponentType<
ButtonHTMLAttributes<HTMLButtonElement> & { selected?: boolean } & RefAttributes<HTMLButtonElement>
>;
SearchInput?: ComponentType<React.ComponentPropsWithoutRef<typeof CommandInput>>;
CheckIcon?: React.ComponentType<React.SVGProps<SVGSVGElement>>;
ChevronIcon?: React.ComponentType<React.SVGProps<SVGSVGElement>>;
}
export type SelectContentProps = Pick<
React.ComponentProps<typeof PopoverPrimitive.Content>,
'align' | 'side' | 'sideOffset'
>;
export interface SelectProps extends Omit<React.ButtonHTMLAttributes<HTMLButtonElement>, 'value' | 'onChange'> {
options: SelectOption[];
value?: string;
onValueChange?: (value: string) => void;
placeholder?: string;
disabled?: boolean;
className?: string;
contentClassName?: string;
itemClassName?: string;
components?: Partial<SelectUIComponents>;
// Content positioning props (forwarded to Radix PopoverPrimitive.Content)
contentProps?: SelectContentProps;
// Search behavior
searchable?: boolean;
searchInputProps?: React.ComponentPropsWithoutRef<typeof CommandInput>;
// Creatable behavior
creatable?: boolean;
onCreateOption?: (input: string) => SelectOption | Promise<SelectOption>;
createOptionLabel?: (input: string) => string;
}
// Default search input built on top of CommandInput. Supports cmdk props at runtime.
const DefaultSearchInput = forwardRef<HTMLInputElement, React.ComponentPropsWithoutRef<typeof CommandInput>>(
(props, _ref) => <CommandInput {...props} />,
);
DefaultSearchInput.displayName = 'SelectSearchInput';
export function Select({
options,
value,
onValueChange,
placeholder = 'Select an option',
disabled = false,
className,
contentClassName,
itemClassName,
components,
contentProps,
searchable = true,
searchInputProps,
creatable = false,
onCreateOption,
createOptionLabel,
...buttonProps
}: SelectProps) {
const popoverState = useOverlayTriggerState({});
const listboxId = useId();
const triggerRef = useRef<HTMLButtonElement>(null);
const popoverRef = useRef<HTMLDivElement>(null);
const selectedItemRef = useRef<HTMLElement>(null);
const [searchQuery, setSearchQuery] = useState('');
// No need for JavaScript width measurement - Radix provides --radix-popover-trigger-width CSS variable
// When opening, ensure the currently selected option is the active item for keyboard nav
useEffect(() => {
if (!popoverState.isOpen) return;
requestAnimationFrame(() => {
const selectedEl = selectedItemRef.current as HTMLElement | null;
if (selectedEl) selectedEl.scrollIntoView({ block: 'center' });
});
}, [popoverState.isOpen]);
const selectedOption = options.find((o) => o.value === value);
const Trigger =
components?.Trigger ||
forwardRef<HTMLButtonElement, ButtonHTMLAttributes<HTMLButtonElement>>((props, ref) => (
<button ref={ref} type="button" {...props} />
));
Trigger.displayName = Trigger.displayName || 'SelectTrigger';
const Item =
components?.Item ||
forwardRef<HTMLButtonElement, ButtonHTMLAttributes<HTMLButtonElement> & { selected?: boolean }>((props, ref) => (
<button ref={ref} type="button" {...props} />
));
Item.displayName = Item.displayName || 'SelectItem';
const CheckIcon = components?.CheckIcon || DefaultCheckIcon;
const ChevronIcon = components?.ChevronIcon || DefaultChevronIcon;
const SearchInput = components?.SearchInput || DefaultSearchInput;
return (
<Popover open={popoverState.isOpen} onOpenChange={popoverState.setOpen}>
<PopoverTrigger asChild>
<Trigger
ref={triggerRef}
disabled={disabled}
className={cn(
'flex items-center justify-between w-full sm:text-base rounded-md border border-input bg-background px-3 py-2 h-10 text-sm ring-offset-background',
'placeholder:text-muted-foreground focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50',
className,
)}
role="combobox"
aria-haspopup="listbox"
aria-expanded={popoverState.isOpen}
aria-controls={listboxId}
{...buttonProps}
aria-label={value ? (selectedOption?.label ?? value) : placeholder}
>
{value ? (selectedOption?.label ?? value) : placeholder}
<ChevronIcon className="w-4 h-4 opacity-50" />
</Trigger>
</PopoverTrigger>
<PopoverPrimitive.Portal>
<PopoverPrimitive.Content
ref={popoverRef}
align={contentProps?.align ?? 'start'}
side={contentProps?.side}
sideOffset={contentProps?.sideOffset ?? 4}
className={cn(
'z-50 rounded-md border bg-popover text-popover-foreground shadow-md outline-none',
'data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0',
'data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95',
'data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2',
'data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2',
'p-0 shadow-md border-0 min-w-2xs',
contentClassName,
)}
style={{ width: 'var(--radix-popover-trigger-width)' }}
data-slot="popover-content"
data-align={contentProps?.align ?? 'start'}
>
<Command className="bg-white rounded-md focus:outline-none sm:text-sm w-full">
{searchable && (
<div className="px-1.5 pb-1.5 pt-1.5">
<SearchInput
placeholder="Search..."
value={searchQuery}
onValueChange={(v: string) => {
setSearchQuery(v);
searchInputProps?.onValueChange?.(v);
}}
{...searchInputProps}
/>
</div>
)}
<CommandList id={listboxId} role="listbox" className="max-h-[200px] rounded-md w-full">
<CommandEmpty className="px-3 py-2 text-sm text-gray-500">No results.</CommandEmpty>
<CommandGroup>
{options.map((option, index) => {
const isSelected = option.value === value;
const commonProps = {
'data-selected': isSelected ? 'true' : 'false',
'data-value': option.value,
'data-testid': `select-option-${option.value}`,
} as const;
// When a custom Item is provided, use asChild to let it render as the actual item element
if (components?.Item) {
const CustomItem = Item;
return (
<CommandItem
key={option.value}
onSelect={() => {
onValueChange?.(option.value);
popoverState.close();
}}
value={option.label}
id={`${listboxId}-option-${index}`}
role="option"
{...commonProps}
className={cn(itemClassName)}
// Attach ref to CommandItem (even with asChild) so we can focus the selected item on open
ref={isSelected ? (selectedItemRef as unknown as Ref<HTMLDivElement>) : undefined}
asChild
>
<CustomItem selected={isSelected}>
{isSelected && <CheckIcon className="h-4 w-4 flex-shrink-0" />}
<span className={cn('block truncate', !isSelected && 'ml-6', isSelected && 'font-semibold')}>
{option.label}
</span>
</CustomItem>
</CommandItem>
);
}
return (
<CommandItem
key={option.value}
onSelect={() => {
onValueChange?.(option.value);
popoverState.close();
}}
value={option.label}
id={`${listboxId}-option-${index}`}
role="option"
{...commonProps}
className={cn(
'w-full text-left cursor-pointer select-none py-3 px-3 transition-colors duration-150 flex items-center gap-2 rounded',
'text-gray-900',
isSelected ? 'bg-gray-100' : 'hover:bg-gray-100',
itemClassName,
)}
// Ensure we can programmatically focus the selected item when opening
ref={isSelected ? (selectedItemRef as unknown as Ref<HTMLDivElement>) : undefined}
>
{isSelected && <CheckIcon className="h-4 w-4 flex-shrink-0" />}
<span className={cn('block truncate', !isSelected && 'ml-6', isSelected && 'font-semibold')}>
{option.label}
</span>
</CommandItem>
);
})}
{(() => {
const q = searchQuery.trim();
const lower = q.toLowerCase();
const hasExactMatch =
q.length > 0 &&
options.some((o) => o.label.toLowerCase() === lower || o.value.toLowerCase() === lower);
if (!creatable || !q || hasExactMatch) return null;
const label = createOptionLabel?.(q) ?? `Select "${q}"`;
return (
<CommandItem
key={`__create__-${q}`}
data-value={`__create__-${q}`}
value={q}
role="option"
onSelect={async () => {
if (!onCreateOption) return;
const created = await onCreateOption(q);
if (created?.value) onValueChange?.(created.value);
popoverState.close();
}}
className={cn(
'w-full text-left cursor-pointer select-none py-3 px-3 transition-colors duration-150 flex items-center gap-2 rounded',
'text-gray-900 hover:bg-gray-100',
itemClassName,
)}
>
<span className="block truncate font-semibold">{label}</span>
</CommandItem>
);
})()}
</CommandGroup>
</CommandList>
</Command>
</PopoverPrimitive.Content>
</PopoverPrimitive.Portal>
</Popover>
);
}