added Search component

This commit is contained in:
Niklas Kapelle 2025-05-07 19:14:27 +02:00
parent 82c98435f6
commit abbc2de298
Signed by: niklas
GPG Key ID: 4EB651B36D841D16

44
web/src/lib/Search.svelte Normal file
View File

@ -0,0 +1,44 @@
<script lang="ts">
let {
placeholder = "Search",
onSearch = () => {},
onSuggest = () => {
return [];
},
}: {
placeholder?: string;
onSearch?: (input: string) => void;
onSuggest?: (input: string) => string[];
} = $props();
let inputText = $state("");
let showSuggestions = $derived(inputText != "");
let suggestions = $state(["One", "Two", "Three"]);
</script>
<div class="relative w-full">
<input
type="search"
{placeholder}
bind:value={inputText}
class="bg-slate-500 text-white p-2 rounded-xl w-full"
onsubmit={() => {
onSearch(inputText);
}}
oninput={() => {
suggestions = onSuggest(inputText);
}}
/>
{#if showSuggestions}
<div
class="absolute z-10 bg-white border border-gray-300 rounded-lg w-full shadow-lg"
>
{#each suggestions as suggestion}
<div class="px-4 py-2 hover:bg-gray-100 cursor-pointer">
{suggestion}
</div>
{/each}
</div>
{/if}
</div>