added Search component
This commit is contained in:
parent
82c98435f6
commit
abbc2de298
44
web/src/lib/Search.svelte
Normal file
44
web/src/lib/Search.svelte
Normal 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>
|
Loading…
x
Reference in New Issue
Block a user