2025-12-11 18:21:21 +01:00
|
|
|
<script lang="ts">
|
2026-07-15 20:31:45 +02:00
|
|
|
import { ICON_CLASS_DEFAULT } from '$lib/constants/css-classes';
|
2025-12-11 18:21:21 +01:00
|
|
|
import { Input } from '$lib/components/ui/input';
|
|
|
|
|
import { Search, X } from '@lucide/svelte';
|
|
|
|
|
|
|
|
|
|
interface Props {
|
2026-06-24 10:21:33 +02:00
|
|
|
autofocus?: boolean;
|
2025-12-11 18:21:21 +01:00
|
|
|
value?: string;
|
|
|
|
|
placeholder?: string;
|
|
|
|
|
onInput?: (value: string) => void;
|
|
|
|
|
onClose?: () => void;
|
|
|
|
|
onKeyDown?: (event: KeyboardEvent) => void;
|
|
|
|
|
class?: string;
|
|
|
|
|
id?: string;
|
|
|
|
|
ref?: HTMLInputElement | null;
|
2026-04-28 14:35:49 +03:00
|
|
|
isCancelAlwaysVisible?: boolean;
|
2025-12-11 18:21:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let {
|
2026-06-24 10:21:33 +02:00
|
|
|
autofocus,
|
2025-12-11 18:21:21 +01:00
|
|
|
value = $bindable(''),
|
|
|
|
|
placeholder = 'Search...',
|
|
|
|
|
onInput,
|
|
|
|
|
onClose,
|
|
|
|
|
onKeyDown,
|
|
|
|
|
class: className,
|
|
|
|
|
id,
|
2026-04-28 14:35:49 +03:00
|
|
|
ref = $bindable(null),
|
|
|
|
|
isCancelAlwaysVisible = false
|
2025-12-11 18:21:21 +01:00
|
|
|
}: Props = $props();
|
|
|
|
|
|
2026-04-28 14:35:49 +03:00
|
|
|
let showClearButton = $derived(isCancelAlwaysVisible || !!value || !!onClose);
|
2025-12-11 18:21:21 +01:00
|
|
|
|
|
|
|
|
function handleInput(event: Event) {
|
|
|
|
|
const target = event.target as HTMLInputElement;
|
|
|
|
|
|
|
|
|
|
value = target.value;
|
|
|
|
|
onInput?.(target.value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleClear() {
|
|
|
|
|
if (value) {
|
|
|
|
|
value = '';
|
|
|
|
|
onInput?.('');
|
2026-06-24 10:21:33 +02:00
|
|
|
ref?.focus({ preventScroll: true });
|
2025-12-11 18:21:21 +01:00
|
|
|
} else {
|
|
|
|
|
onClose?.();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<div class="relative {className}">
|
|
|
|
|
<Search
|
2026-07-15 20:31:45 +02:00
|
|
|
class="absolute top-1/2 left-3 z-10 {ICON_CLASS_DEFAULT} -translate-y-1/2 transform text-muted-foreground"
|
2025-12-11 18:21:21 +01:00
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<Input
|
2026-06-24 10:21:33 +02:00
|
|
|
{autofocus}
|
2025-12-11 18:21:21 +01:00
|
|
|
{id}
|
|
|
|
|
bind:value
|
|
|
|
|
bind:ref
|
|
|
|
|
class="pl-9 {showClearButton ? 'pr-9' : ''}"
|
|
|
|
|
oninput={handleInput}
|
|
|
|
|
onkeydown={onKeyDown}
|
|
|
|
|
{placeholder}
|
|
|
|
|
type="search"
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
{#if showClearButton}
|
|
|
|
|
<button
|
|
|
|
|
type="button"
|
2026-04-28 14:35:49 +03:00
|
|
|
class="absolute top-1/2 right-3 -translate-y-1/2 transform cursor-pointer text-muted-foreground transition-colors hover:text-foreground"
|
2025-12-11 18:21:21 +01:00
|
|
|
onclick={handleClear}
|
|
|
|
|
aria-label={value ? 'Clear search' : 'Close'}
|
|
|
|
|
>
|
2026-07-15 20:31:45 +02:00
|
|
|
<X class={ICON_CLASS_DEFAULT} />
|
2025-12-11 18:21:21 +01:00
|
|
|
</button>
|
|
|
|
|
{/if}
|
|
|
|
|
</div>
|