Files
console/src/lib/elements/forms/inputSearch.svelte
T
Torsten Dittmann 4debbd9023 fix: team and users
2022-09-30 11:09:15 +02:00

60 lines
1.5 KiB
Svelte

<script lang="ts">
import { onMount } from 'svelte';
import { onDestroy } from 'svelte';
export let value = '';
export let placeholder = '';
export let debounce = 250;
export let required = false;
export let disabled = false;
export let autofocus = false;
export let isWithEndButton = true;
let element: HTMLInputElement;
let timer: ReturnType<typeof setTimeout>;
onMount(() => {
if (element && autofocus) {
element.focus();
}
});
onDestroy(() => {
value = '';
if (timer) {
clearTimeout(timer);
}
});
const valueChange = (event: Event) => {
clearTimeout(timer);
timer = setTimeout(() => {
const target = event.target as HTMLInputElement;
value = target.value;
}, debounce);
};
$: if (!value) {
if (element) {
element.value = value;
}
}
</script>
<div class="input-text-wrapper" class:is-with-end-button={isWithEndButton}>
<input
{placeholder}
{disabled}
{required}
type="search"
class="input-text"
bind:this={element}
on:input={valueChange} />
<span class="icon-search" aria-hidden="true" />
{#if isWithEndButton && value}
<button class="x-button" aria-label="Clear search" on:click={() => (value = '')}>
<span class="icon-x" aria-hidden="true" />
</button>
{/if}
</div>