Documentation Upgrade
Start here
CwSearchInput is for async suggestion search. It handles debouncing, cancellation, keyboard navigation, and selected-value binding so you can focus on fetching and mapping results.
Copy-paste examples
These snippets intentionally show the full public API surface the live demo relies on.
Search over a simple string list
The default mapping helpers are enough when your suggestions are already strings.
<script lang="ts">
const crops = ['Tomato', 'Pepper', 'Lettuce', 'Basil'];
let value = $state('');
async function fetchSuggestions(query: string, signal: AbortSignal) {
await new Promise((resolve) => setTimeout(resolve, 250));
if (signal.aborted) throw new DOMException('Aborted', 'AbortError');
return crops.filter((crop) => crop.toLowerCase().includes(query.toLowerCase()));
}
</script>
<CwSearchInput
label="Search crops"
minChars={1}
fetchSuggestions={fetchSuggestions}
bind:value={value}
/>
Search object results with custom mapping
Use this pattern when the API returns structured objects.
<script lang="ts">
let selectedId = $state('');
async function fetchDevices(query: string, signal: AbortSignal) {
const response = await fetch('/api/devices?q=' + encodeURIComponent(query), { signal });
return await response.json();
}
</script>
<CwSearchInput
label="Search devices"
name="device"
placeholder="Type device name or serial..."
fetchSuggestions={fetchDevices}
mapSuggestionToLabel={(item) => `${item.name} (${item.serial})`}
mapSuggestionToValue={(item) => item.id}
oninput={(value) => console.log('Typing:', value)}
onselect={(value, item) => {
selectedId = value;
console.log('Selected device:', item);
}}
/>