Documentation menu
Getting Started
Components
Primitives
- Agent Thinking
- Alert
- Alert Dialog
- Avatar
- Badge
- Breadcrumb
- Button
- Button Group
- Calendar
- Card
- Checkbox
- Command
- Command Palette
- Context Menu
- Data Table
- Dialog
- Dropdown Menu
- Empty
- Field
- Glow Cloud
- Input
- Label
- Medication Schedule
- Period Tabs
- Popover
- Rainbow Button
- Scroll Area
- Select
- Selectable Card
- Separator
- Sheet
- Sidebar
- Skeleton
- Slider
- Sonner
- Spinner
- Switch
- Table
- Tabs
- Tag
- Textarea
- Timeline
- Tooltip
- Use Count Up
Charts and metrics
Patterns
- Confirm Button
- Date Picker
- Embedded
- Empty State
- Entity Drawer
- Error Boundary
- Field Row
- File Dropzone
- Form Dialog
- Kv
- List Editor
- List Page
- Live Time
- Mini Table
- Money
- Note Field
- Note Text
- Page Card
- Page Header
- Page Tabs
- Page Toolbar
- Person Cell
- Picture Field
- Print Card
- Range Switcher
- Row Actions
- Search Input
- Search Select
- Selected Box
- Signature Pad
- Status Badge
- Sub Nav Layout
- Suggest Input
- Tab Icons
- Table Footer
- Theme Accent Picker
- Time Picker
- View All Link
Dashboard cards
Advanced
Device frames
Website widgets
Website screens
Website panels
Patterns
Suggest Input
A text input that suggests earlier values.
import { useState } from "react"
import { SuggestInput } from "@/registry/vcare/ui/suggest-input"
export default function SuggestInputDemo() {
const [v, setV] = useState("")
return (
<div className="w-72">
<SuggestInput
value={v}
onChange={setV}
placeholder="Referred by"
options={["Dr Anil Kumar", "Dr Beena Joseph", "Dr Chitra Menon", "Self"].map((x) => ({ value: x, label: x }))}
/>
</div>
)
}Installation
npx shadcn@latest add @vcare/suggest-inputOr install directly from the URL: https://ui.vcarehms.com/r/suggest-input.json
- Install the dependencies:
npm i lucide-react - Add the registry items it uses: input, utils.
- Copy this file into your project and change the
@/registry/vcare/…imports to your own paths.
components/ui/suggest-input.tsx
import { useEffect, useMemo, useRef, useState } from 'react';
import { ChevronDown, X } from 'lucide-react';
import { Input } from '@/registry/vcare/ui/input';
import { cn } from '@/registry/vcare/lib/utils';
/**
* A text box with suggestions: pick one from the list or type anything else (e.g. a referring doctor who is not on the
* list yet). The list filters as you type; arrow keys and Enter pick.
*/
export function SuggestInput({
value,
onChange,
options,
placeholder,
id,
}: {
value: string;
onChange: (v: string) => void;
options: { value: string; label: string }[];
placeholder?: string;
id?: string;
}) {
const [open, setOpen] = useState(false);
const [hi, setHi] = useState(0);
const box = useRef<HTMLDivElement>(null);
const shown = useMemo(() => {
const q = value.trim().toLowerCase();
const exact = options.some((o) => o.value === value);
return q && !exact ? options.filter((o) => o.label.toLowerCase().includes(q)) : options;
}, [options, value]);
// While the list is open, Escape closes the list only (not the dialog around it).
useEffect(() => {
if (!open) return;
const onKey = (e: KeyboardEvent) => {
if (e.key !== 'Escape') return;
e.stopPropagation();
e.preventDefault();
setOpen(false);
};
window.addEventListener('keydown', onKey, true);
return () => window.removeEventListener('keydown', onKey, true);
}, [open]);
const pick = (v: string) => {
onChange(v);
setOpen(false);
};
return (
<div
ref={box}
className="relative"
onBlur={(e) => {
if (!box.current?.contains(e.relatedTarget as Node)) setOpen(false);
}}
>
<Input
id={id}
value={value}
placeholder={placeholder}
className="pe-14"
role="combobox"
aria-expanded={open}
aria-autocomplete="list"
onFocus={() => setOpen(true)}
onChange={(e) => {
onChange(e.target.value);
setOpen(true);
setHi(0);
}}
onKeyDown={(e) => {
if (e.key === 'ArrowDown') {
e.preventDefault();
setOpen(true);
setHi((h) => Math.min(h + 1, shown.length - 1));
} else if (e.key === 'ArrowUp') {
e.preventDefault();
setHi((h) => Math.max(h - 1, 0));
} else if (e.key === 'Enter' && open && shown[hi]) {
e.preventDefault();
pick(shown[hi]!.value);
}
}}
/>
<span className="absolute end-2 top-1/2 flex -translate-y-1/2 items-center gap-0.5 text-muted-foreground">
{value && (
<button type="button" tabIndex={-1} aria-label="Clear" className="grid size-6 place-items-center rounded-md hover:bg-accent" onClick={() => onChange('')}>
<X className="size-3.5" />
</button>
)}
<button type="button" tabIndex={-1} aria-label="Show list" className="grid size-6 place-items-center rounded-md hover:bg-accent" onClick={() => setOpen((o) => !o)}>
<ChevronDown className="size-4" />
</button>
</span>
{open && shown.length > 0 && (
<ul role="listbox" className="absolute inset-x-0 top-full z-50 mt-1 max-h-64 overflow-auto rounded-xl border bg-popover p-1 shadow-overlay">
{shown.map((o, i) => (
<li key={o.value}>
<button
type="button"
role="option"
aria-selected={o.value === value}
onMouseDown={(e) => e.preventDefault()}
onClick={() => pick(o.value)}
className={cn('block w-full truncate rounded-lg px-2.5 py-1.5 text-start text-sm hover:bg-accent', i === hi && 'bg-accent', o.value === value && 'font-semibold')}
>
{o.label}
</button>
</li>
))}
</ul>
)}
</div>
);
}Usage
import { SuggestInput } from "@/components/ui/suggest-input"Exports
SuggestInput
Dependencies
lucide-react@^1.80.0(npm)@vcare/input(registry)@vcare/utils(registry)
Raw item: /r/suggest-input.json