opensourceui
GitHubTwitter/X
  1. Home
  2. Components
  3. Inputs
  4. Combobox Field Input
Back to Inputs

Components / Inputs

Combobox Field Input

Free Inputs React component — ComboboxFieldInput. MIT licensed, copy-paste ready for Next.js and Tailwind CSS.

Searchable combobox with filtered listbox, keyboard navigation, click-outside close, and hidden field for form posts.

Preview

Type to filter the list.

Setup

How to use

Free for personal and commercial use. No UI attribution required. Include the MIT copyright notice when copying component source into your project. Read the MIT license.

  1. 1

    Run in your terminal:

    $npm install clsx tailwind-merge lucide-react
  2. 2

    Copy lib/cn.ts below. Skip if you already have cn().

  3. 3

    Copy the code below and create components/inputs/combobox-field-input.tsx in your project.

  4. 4

    Import and render:

    Example

    import { ComboboxFieldInput } from "@/components/inputs/combobox-field-input";

    <ComboboxFieldInput label="City" options={cities} value={city} onValueChange={setCity} />

lib/cn.ts

1
2
3
4
5
6
import { type ClassValue, clsx } from "clsx";
import { twMerge } from "tailwind-merge";
 
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}

components/inputs/combobox-field-input.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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
"use client";
 
import {
forwardRef,
useCallback,
useEffect,
useId,
useMemo,
useRef,
useState,
type ChangeEvent,
type ComponentPropsWithoutRef,
type KeyboardEvent,
type MutableRefObject,
type Ref,
} from "react";
 
import { Check, ChevronDown } from "lucide-react";
 
import { cn } from "@/lib/cn";
 
export type ComboboxFieldOption = Readonly<{
value: string;
label: string;
disabled?: boolean;
}>;
 
export type ComboboxFieldInputProps = Readonly<
{
label?: string;
hint?: string;
error?: boolean;
errorMessage?: string;
placeholder?: string;
emptyMessage?: string;
options?: readonly ComboboxFieldOption[];
containerClassName?: string;
name?: string;
onValueChange?: (value: string) => void;
} & Omit<
ComponentPropsWithoutRef<"input">,
"size" | "type" | "value" | "defaultValue" | "onChange" | "role"
>
> & {
value?: string;
defaultValue?: string;
onChange?: (event: ChangeEvent<HTMLInputElement>) => void;
};
 
const DEFAULT_OPTIONS: readonly ComboboxFieldOption[] = [
{ value: "react", label: "React" },
{ value: "vue", label: "Vue" },
{ value: "svelte", label: "Svelte" },
{ value: "angular", label: "Angular" },
{ value: "solid", label: "Solid" },
{ value: "next", label: "Next.js" },
];
 
function mergeRefs<T>(...refs: Array<Ref<T> | undefined>) {
return (node: T | null) => {
for (const ref of refs) {
if (!ref) continue;
if (typeof ref === "function") ref(node);
else (ref as MutableRefObject<T | null>).current = node;
}
};
}
 
function findNextEnabledIndex(
options: readonly ComboboxFieldOption[],
start: number,
direction: 1 | -1,
): number {
const count = options.length;
if (count === 0) return -1;
 
for (let step = 1; step <= count; step += 1) {
const index = (start + direction * step + count) % count;
if (!options[index]?.disabled) return index;
}
 
return -1;
}
 
export const ComboboxFieldInput = forwardRef<
HTMLInputElement,
ComboboxFieldInputProps
>(function ComboboxFieldInput(
{
className,
containerClassName,
id,
label = "Framework",
hint,
error = false,
errorMessage = "Select a valid option.",
placeholder = "Search frameworks…",
emptyMessage = "No results found.",
options = DEFAULT_OPTIONS,
disabled,
required,
name,
value,
defaultValue = "",
onChange,
onValueChange,
onFocus,
onBlur,
onKeyDown,
...props
},
ref,
) {
const generatedId = useId();
const fieldId = id ?? generatedId;
const listboxId = `${fieldId}-listbox`;
const hintId = `${fieldId}-hint`;
const errorId = `${fieldId}-error`;
const rootRef = useRef<HTMLDivElement | null>(null);
const inputRef = useRef<HTMLInputElement | null>(null);
const blurTimerRef = useRef<number | null>(null);
 
const isControlled = value !== undefined;
const [internalValue, setInternalValue] = useState(String(defaultValue));
const [query, setQuery] = useState("");
const [open, setOpen] = useState(false);
const [highlighted, setHighlighted] = useState(-1);
 
const currentValue = isControlled ? String(value ?? "") : internalValue;
const selectedOption = options.find((option) => option.value === currentValue);
 
const filtered = useMemo(() => {
const normalized = query.trim().toLowerCase();
if (!normalized) return options;
return options.filter((option) =>
option.label.toLowerCase().includes(normalized),
);
}, [options, query]);
 
const displayValue = open ? query : (selectedOption?.label ?? query);
 
const emitChange = useCallback(
(next: string) => {
if (!isControlled) setInternalValue(next);
onValueChange?.(next);
onChange?.({ target: { value: next } } as ChangeEvent<HTMLInputElement>);
},
[isControlled, onChange, onValueChange],
);
 
const close = useCallback(() => {
if (blurTimerRef.current !== null) {
window.clearTimeout(blurTimerRef.current);
blurTimerRef.current = null;
}
setOpen(false);
setHighlighted(-1);
setQuery(selectedOption?.label ?? "");
}, [selectedOption?.label]);
 
const openList = useCallback(() => {
if (disabled) return;
setOpen(true);
setQuery("");
const index = filtered.findIndex((option) => option.value === currentValue);
if (index >= 0 && !filtered[index]?.disabled) {
setHighlighted(index);
return;
}
const firstEnabled = filtered.findIndex((option) => !option.disabled);
setHighlighted(firstEnabled);
}, [currentValue, disabled, filtered]);
 
const selectOption = useCallback(
(option: ComboboxFieldOption) => {
if (option.disabled) return;
emitChange(option.value);
setQuery(option.label);
setOpen(false);
setHighlighted(-1);
},
[emitChange],
);
 
useEffect(() => {
if (!open) return;
 
function handlePointerDown(event: PointerEvent) {
if (!rootRef.current?.contains(event.target as Node)) close();
}
 
function handleEscape(event: globalThis.KeyboardEvent) {
if (event.key === "Escape") {
close();
inputRef.current?.focus();
}
}
 
document.addEventListener("pointerdown", handlePointerDown);
document.addEventListener("keydown", handleEscape);
return () => {
document.removeEventListener("pointerdown", handlePointerDown);
document.removeEventListener("keydown", handleEscape);
};
}, [close, open]);
 
useEffect(() => {
if (disabled && open) close();
}, [close, disabled, open]);
 
useEffect(() => {
inputRef.current?.setCustomValidity(
required && !currentValue ? errorMessage : "",
);
}, [currentValue, errorMessage, required]);
 
useEffect(() => {
return () => {
if (blurTimerRef.current !== null) window.clearTimeout(blurTimerRef.current);
};
}, []);
 
useEffect(() => {
if (!open) return;
if (highlighted >= filtered.length) {
const firstEnabled = filtered.findIndex((option) => !option.disabled);
setHighlighted(firstEnabled);
}
}, [filtered, highlighted, open]);
 
const handleInputChange = useCallback(
(event: ChangeEvent<HTMLInputElement>) => {
const nextQuery = event.target.value;
setQuery(nextQuery);
setOpen(true);
const normalized = nextQuery.trim().toLowerCase();
const nextOptions = normalized
? options.filter((option) => option.label.toLowerCase().includes(normalized))
: options;
setHighlighted(nextOptions.findIndex((option) => !option.disabled));
if (!nextQuery.trim()) emitChange("");
},
[emitChange, options],
);
 
const handleKeyDown = useCallback(
(event: KeyboardEvent<HTMLInputElement>) => {
onKeyDown?.(event);
if (event.defaultPrevented || disabled) return;
 
if (event.key === "ArrowDown") {
event.preventDefault();
if (!open) {
openList();
return;
}
const next = findNextEnabledIndex(
filtered,
highlighted < 0 ? -1 : highlighted,
1,
);
if (next >= 0) setHighlighted(next);
return;
}
 
if (event.key === "ArrowUp") {
event.preventDefault();
if (!open) {
openList();
return;
}
const next = findNextEnabledIndex(
filtered,
highlighted < 0 ? filtered.length : highlighted,
-1,
);
if (next >= 0) setHighlighted(next);
return;
}
 
if (event.key === "Enter" && open && highlighted >= 0) {
event.preventDefault();
const option = filtered[highlighted];
if (option) selectOption(option);
return;
}
 
if (event.key === "Escape" && open) {
event.preventDefault();
close();
}
},
[
close,
disabled,
filtered,
highlighted,
onKeyDown,
open,
openList,
selectOption,
],
);
 
return (
<div
ref={rootRef}
data-slot="combobox-field-input"
data-error={error || undefined}
data-open={open || undefined}
className={cn("relative w-full max-w-sm font-sans", containerClassName)}
>
{name ? (
<input type="hidden" name={name} value={currentValue} disabled={disabled} readOnly />
) : null}
 
<label
htmlFor={fieldId}
className="mb-1.5 block text-sm font-medium text-neutral-900"
>
{label}
{required ? (
<span className="ml-0.5 text-rose-500" aria-hidden>
*
</span>
) : null}
</label>
 
<div className="relative">
<input
ref={mergeRefs(ref, inputRef)}
id={fieldId}
type="text"
role="combobox"
autoComplete="off"
disabled={disabled}
required={required}
value={displayValue}
placeholder={placeholder}
aria-autocomplete="list"
aria-expanded={open}
aria-controls={listboxId}
aria-activedescendant={
open && highlighted >= 0
? `${listboxId}-option-${highlighted}`
: undefined
}
aria-invalid={error || undefined}
aria-describedby={error ? errorId : hint ? hintId : undefined}
onChange={handleInputChange}
onFocus={(event) => {
onFocus?.(event);
if (!disabled) openList();
}}
onBlur={(event) => {
onBlur?.(event);
if (event.defaultPrevented) return;
blurTimerRef.current = window.setTimeout(() => close(), 120);
}}
onInvalid={() => {
inputRef.current?.focus();
}}
onKeyDown={handleKeyDown}
className={cn(
"h-10 w-full rounded-lg border bg-white py-2 pr-10 pl-3.5 font-sans text-sm text-neutral-900 outline-none ring-0 transition-[border-color] duration-200 placeholder:text-neutral-400 focus:ring-0 disabled:cursor-not-allowed disabled:bg-neutral-50 disabled:text-neutral-400",
error
? "border-rose-300 focus:border-rose-400"
: open
? "border-neutral-900"
: "border-neutral-200 focus:border-neutral-900",
className,
)}
{...props}
/>
 
<ChevronDown
size={16}
strokeWidth={2}
aria-hidden
className={cn(
"pointer-events-none absolute top-1/2 right-3 -translate-y-1/2 text-neutral-400 transition-transform duration-200",
open && "rotate-180",
)}
/>
</div>
 
{open ? (
<ul
id={listboxId}
role="listbox"
aria-label={label}
className="absolute z-20 mt-1.5 max-h-56 w-full overflow-auto rounded-lg border border-neutral-200 bg-white py-1 shadow-sm"
>
{filtered.length === 0 ? (
<li className="px-3.5 py-2 text-sm text-neutral-400">{emptyMessage}</li>
) : (
filtered.map((option, index) => {
const isSelected = option.value === currentValue;
const isHighlighted = index === highlighted;
 
return (
<li
key={option.value}
id={`${listboxId}-option-${index}`}
role="option"
aria-selected={isSelected}
aria-disabled={option.disabled || undefined}
onMouseEnter={() => {
if (!option.disabled) setHighlighted(index);
}}
onMouseDown={(event) => event.preventDefault()}
onClick={() => selectOption(option)}
className={cn(
"flex cursor-pointer items-center justify-between gap-2 px-3.5 py-2 text-sm transition-colors duration-150",
option.disabled && "cursor-not-allowed text-neutral-300",
!option.disabled && isHighlighted && "bg-neutral-50 text-neutral-900",
!option.disabled && !isHighlighted && "text-neutral-700",
isSelected && !option.disabled && "font-medium text-neutral-900",
)}
>
<span className="truncate">{option.label}</span>
{isSelected ? (
<Check size={14} strokeWidth={2.5} className="shrink-0 text-neutral-900" />
) : null}
</li>
);
})
)}
</ul>
) : null}
 
{error ? (
<p id={errorId} role="alert" className="mt-1.5 text-xs text-rose-600">
{errorMessage}
</p>
) : hint ? (
<p id={hintId} className="mt-1.5 text-xs text-neutral-500">
{hint}
</p>
) : null}
</div>
);
});
 
ComboboxFieldInput.displayName = "ComboboxFieldInput";
PreviousNext

On this

page

Jump to a section on this page.

  • lib/cn.ts
  • components/inputs/combobox-field-input.tsx

Sponsor spot · demo preview

Logo

Your brand name

Your tagline or category

A short pitch about your product or service goes here. This is a preview of how sponsor cards will look in this sidebar.

Your call to action

This slot is available. Sponsor Opensource UI and reach developers browsing components every day.