Skip to content

Commit

Permalink
Merge pull request #358 from hearchco/as/fix/suggestions
Browse files Browse the repository at this point in the history
fix(searchbox): sync query with selection and allow reverting back to original
  • Loading branch information
aleksasiriski authored Jul 7, 2024
2 parents 03b75eb + cc84a2a commit 3ddf110
Showing 1 changed file with 40 additions and 4 deletions.
44 changes: 40 additions & 4 deletions src/lib/components/searchbox/main.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
$effect(() => {
if (query === '' || getQueryWithoutCategory(query) === '') {
suggestions = [];
} else {
} else if (currentIndex === -1) {
fetchSuggestions(getQueryWithoutCategory(query)).then((suggs) => {
const maxSize = 10;
if (suggs.length > maxSize) suggs.splice(maxSize, suggs.length - maxSize);
Expand All @@ -52,8 +52,22 @@
}
});
// Old query to allow returning to it.
let oldQuery = $state(query);
// Changes the query when the focus is lost.
$effect(() => {
if (!shouldShowSuggs) {
if (currentIndex !== -1) {
query = suggestions[currentIndex].value;
currentIndex = -1;
}
oldQuery = query;
}
});
/** @param {SubmitEvent} e */
async function handleSubmit(e) {
function handleSubmit(e) {
if (query === '') {
e.preventDefault();
return;
Expand All @@ -73,26 +87,47 @@
currentIndex = -1;
shouldShowSuggs = false;
suggestions = [];
oldQuery = query;
}
/** @param {KeyboardEvent} event */
async function handleKeyDown(event) {
function handleKeyDown(event) {
switch (event.key) {
case 'ArrowDown':
case 'ArrowUp':
event.preventDefault();
if (event.key === 'ArrowDown') {
currentIndex = Math.min(currentIndex + 1, (await suggestions).length - 1);
currentIndex = Math.min(currentIndex + 1, suggestions.length - 1);
} else {
currentIndex = Math.max(currentIndex - 1, -1);
}
if (currentIndex !== -1) {
query = suggestions[currentIndex].value;
} else {
query = oldQuery;
}
break;
case 'Enter':
break;
default:
currentIndex = -1;
}
}
/** @param {KeyboardEvent} event */
function handleKeyUp({ key }) {
switch (key) {
case 'ArrowDown':
case 'ArrowUp':
case 'Enter':
break;
default:
oldQuery = query;
}
}
</script>

<svelte:window
Expand Down Expand Up @@ -143,6 +178,7 @@
bind:this={searchInput}
onfocusin={() => (shouldShowSuggs = true)}
onkeydown={handleKeyDown}
onkeyup={handleKeyUp}
name="q"
class="ml-4 mr-1.5 h-full w-full bg-transparent focus:outline-none"
type="text"
Expand Down

0 comments on commit 3ddf110

Please sign in to comment.