From 813f32de5007fa9477ecd82a5c9482825af639e2 Mon Sep 17 00:00:00 2001 From: Dean Sallinen <7519573+deansallinen@users.noreply.github.com> Date: Mon, 4 Nov 2024 16:20:54 -0800 Subject: [PATCH] feat: add clear to search history --- src/lib/components/input/search.svelte | 75 +++++++++++++++----------- src/lib/state/search.svelte.ts | 14 ++--- 2 files changed, 51 insertions(+), 38 deletions(-) diff --git a/src/lib/components/input/search.svelte b/src/lib/components/input/search.svelte index 61c0567f..f490baa8 100644 --- a/src/lib/components/input/search.svelte +++ b/src/lib/components/input/search.svelte @@ -7,7 +7,7 @@ import { preventDefault } from '$lib/utils'; import { goto } from '$app/navigation'; import { fade, scale } from 'svelte/transition'; - import { ArrowLeftRight, Box, Key, SearchIcon, UserSearch } from 'lucide-svelte'; + import { ArrowLeftRight, Box, Key, SearchIcon, UserSearch, X } from 'lucide-svelte'; import { SearchHistory } from '$lib/state/search.svelte'; import Button from '$lib/components/button/button.svelte'; import { Stack } from '$lib/components/layout'; @@ -31,7 +31,7 @@ let selectedIndex: number | undefined = $state(); const searchHistory = new SearchHistory(); - const history = searchHistory.get(); + const history = $derived(searchHistory.get()); const searchType = $derived.by(() => { /* eslint-disable @typescript-eslint/no-unused-vars */ @@ -126,7 +126,7 @@ } if (selectedIndex !== undefined && event.key === 'Enter') { - goTosearchHistory(history[selectedIndex].result); + goToSearchHistory(history[selectedIndex].result); } } } @@ -144,7 +144,7 @@ searchValue = ''; } - function goTosearchHistory(url: string) { + function goToSearchHistory(url: string) { goto(url); closeSearch(); } @@ -214,49 +214,60 @@ {#if history.length}
-
+ {/each}
diff --git a/src/lib/state/search.svelte.ts b/src/lib/state/search.svelte.ts index 8081a751..1a038254 100644 --- a/src/lib/state/search.svelte.ts +++ b/src/lib/state/search.svelte.ts @@ -6,10 +6,9 @@ type SearchResult = { searchValue: string; }; -const maxHistoryLength = 10; - export class SearchHistory { - history = $state([]) as SearchResult[]; + private history = $state([]) as SearchResult[]; + maxHistoryLength = 10; storageKey = 'searchHistory'; constructor() { @@ -35,13 +34,16 @@ export class SearchHistory { add(s: SearchResult) { this.history.unshift(s); - if (history.length > maxHistoryLength) { - this.history.splice(maxHistoryLength); + if (history.length > this.maxHistoryLength) { + this.history.splice(this.maxHistoryLength); } this.saveHistory(); } - // remove() {} + remove(i: number) { + this.history.splice(i, 1); + this.saveHistory(); + } get(): SearchResult[] { return this.history;