mirror of
https://github.com/appwrite/console.git
synced 2026-11-18 20:47:21 +00:00
fix: search x debounce.
This commit is contained in:
1 parent
9fcde84526
commit
16939ce1e4
1 file changed
+34
-44
@@ -1,61 +1,50 @@
|
||||
<script lang="ts">
|
||||
import { goto } from '$app/navigation';
|
||||
import { page } from '$app/state';
|
||||
import { goto } from '$app/navigation';
|
||||
import { onMount, onDestroy } from 'svelte';
|
||||
import { trackEvent } from '$lib/actions/analytics';
|
||||
import { onMount } from 'svelte';
|
||||
import { onDestroy } from 'svelte';
|
||||
import { Icon, Input } from '@appwrite.io/pink-svelte';
|
||||
import { IconSearch, IconX } from '@appwrite.io/pink-icons-svelte';
|
||||
import { debounce as createDebounce } from '$lib/helpers/debounce.js';
|
||||
|
||||
export let search = '';
|
||||
export let placeholder = '';
|
||||
export let debounce = 250;
|
||||
export let required = false;
|
||||
export let disabled = false;
|
||||
export let autofocus = false;
|
||||
|
||||
let element: HTMLInputElement;
|
||||
let timer: ReturnType<typeof setTimeout>;
|
||||
let inputValue = page.url.searchParams.get('search') ?? '';
|
||||
|
||||
onMount(() => {
|
||||
if (element && autofocus) {
|
||||
element.focus();
|
||||
const runSearch = createDebounce((value: string) => {
|
||||
const trimmed = value.trim();
|
||||
const url = new URL(page.url);
|
||||
const previous = url.searchParams.get('search') ?? '';
|
||||
|
||||
if (previous === trimmed) return;
|
||||
|
||||
if (page.data.page > 1) {
|
||||
url.searchParams.delete('page');
|
||||
}
|
||||
});
|
||||
|
||||
if (trimmed === '') {
|
||||
url.searchParams.delete('search');
|
||||
} else {
|
||||
url.searchParams.set('search', trimmed);
|
||||
}
|
||||
|
||||
trackEvent('search');
|
||||
goto(url, { keepFocus: true });
|
||||
}, debounce);
|
||||
|
||||
$: runSearch(inputValue);
|
||||
|
||||
function clearInput() {
|
||||
inputValue = '';
|
||||
}
|
||||
|
||||
onDestroy(() => {
|
||||
search = '';
|
||||
if (timer) {
|
||||
clearTimeout(timer);
|
||||
}
|
||||
runSearch.cancel?.();
|
||||
});
|
||||
|
||||
$: valueChange(search ?? '');
|
||||
|
||||
function valueChange(value: string) {
|
||||
clearTimeout(timer);
|
||||
timer = setTimeout(() => {
|
||||
const url = new URL(page.url);
|
||||
const previous = url.searchParams.get('search') ?? '';
|
||||
|
||||
if (previous === value) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (page.data.page > 1) {
|
||||
url.searchParams.delete('page');
|
||||
}
|
||||
|
||||
if (value === '') {
|
||||
url.searchParams.delete('search');
|
||||
} else {
|
||||
url.searchParams.set('search', value);
|
||||
}
|
||||
|
||||
trackEvent('search');
|
||||
goto(url, { keepFocus: true });
|
||||
}, debounce);
|
||||
}
|
||||
</script>
|
||||
|
||||
<div style:max-width="360px" style:width="100%">
|
||||
@@ -63,14 +52,15 @@
|
||||
{placeholder}
|
||||
{disabled}
|
||||
{required}
|
||||
bind:value={search}
|
||||
{autofocus}
|
||||
bind:value={inputValue}
|
||||
--bgcolor-neutral-default="var(--bgcolor-neutral-primary)">
|
||||
<svelte:fragment slot="start">
|
||||
<Icon icon={IconSearch} />
|
||||
</svelte:fragment>
|
||||
<svelte:fragment slot="end">
|
||||
{#if search}
|
||||
<Input.Action icon={IconX} on:click={() => (search = '')} />
|
||||
{#if inputValue}
|
||||
<Input.Action icon={IconX} on:click={clearInput} />
|
||||
{/if}
|
||||
</svelte:fragment>
|
||||
</Input.Text>
|
||||
|
||||
Reference in new issue
Block a user