Enable clicking on autocomplete

This commit is contained in:
2024-08-13 18:42:21 +02:00
parent 12b00e5147
commit 32563d7d33
2 changed files with 32 additions and 17 deletions

View File

@@ -4,7 +4,6 @@
import { CreateFood, GetLastPer100 } from "$wails/main/App"; import { CreateFood, GetLastPer100 } from "$wails/main/App";
import { foodStore } from "$lib/store/Energy/foodStore"; import { foodStore } from "$lib/store/Energy/foodStore";
import FoodSearchEntry from "./FoodSearchEntry.svelte"; import FoodSearchEntry from "./FoodSearchEntry.svelte";
import { onMount } from "svelte";
let item: main.Food = { let item: main.Food = {
food: "", food: "",
@@ -28,6 +27,7 @@
// Maybe it would be a good idea to use $ instead of update down there... // Maybe it would be a good idea to use $ instead of update down there...
// Maybe it's a topic for another day // Maybe it's a topic for another day
$: { $: {
name = name.trim();
if (!name) { if (!name) {
foodSearch = []; foodSearch = [];
} }
@@ -72,27 +72,33 @@
if (!per100Edited) if (!per100Edited)
GetLastPer100(name.trim()).then((res) => { GetLastPer100(name.trim()).then((res) => {
if (res.success && res.data) { // Prevent search when there's nothing to search
// Sometimes we get search results after deleting name
if (res.success && res.data && name) {
foodSearch = res.data; foodSearch = res.data;
} else {
foodSearch = [];
} }
}); });
} }
let hiLiteIndex: number | null = null; let hiLiteIndex: number | null = null;
function navigateList(e: KeyboardEvent) { // function navigateList(e: KeyboardEvent) {
if (e.key === "ArrowDown" && hiLiteIndex && hiLiteIndex <= foodSearch.length - 1) { // console.log(foodSearch, hiLiteIndex);
hiLiteIndex === null ? (hiLiteIndex = 0) : (hiLiteIndex += 1); // // @ts-ignore shut the fuck up
} else if (e.key === "ArrowUp" && hiLiteIndex !== null) { // if (e.key == "ArrowDown" && hiLiteIndex <= foodSearch.length - 2) {
hiLiteIndex === 0 ? (hiLiteIndex = foodSearch.length - 1) : (hiLiteIndex -= 1); // hiLiteIndex == null ? (hiLiteIndex = 0) : (hiLiteIndex += 1);
} else if (e.key === "Enter") { // } else if (e.key == "ArrowUp" && hiLiteIndex !== null) {
// @ts-ignore ITS NOT NULL YOU ASSHAT // hiLiteIndex == 0 ? 0 : (hiLiteIndex -= 1);
// WE CHECKED // } else if (e.key == "Enter") {
// ITS NOT // // @ts-ignore ITS NOT NULL YOU ASSHAT
setInputVal(foodSearch[hiLiteIndex]); // // WE CHECKED
} else { // // ITS NOT
return; // setInputVal(foodSearch[hiLiteIndex]);
} // } else {
} // return;
// }
// }
function setInputVal(val: string) { function setInputVal(val: string) {
name = val; name = val;
hiLiteIndex = null; hiLiteIndex = null;
@@ -108,12 +114,15 @@
} }
</script> </script>
<!-- <svelte:window on:keydown={navigateList} /> -->
<template> <template>
<tr class="border-b border-gray-200 dark:border-gray-700 text-lg font-bold relative"> <tr class="border-b border-gray-200 dark:border-gray-700 text-lg font-bold relative">
<th <th
class="px-6 py-4 font-medium text-gray-900 whitespace-nowrap bg-gray-50 dark:text-white dark:bg-gray-800" class="px-6 py-4 font-medium text-gray-900 whitespace-nowrap bg-gray-50 dark:text-white dark:bg-gray-800"
scope="row" scope="row"
></th> ></th>
<!-- svelte-ignore a11y-autofocus -->
<td <td
bind:innerText={name} bind:innerText={name}
class:border-[3px]={!name} class:border-[3px]={!name}
@@ -151,7 +160,11 @@
{#if foodSearch.length > 0} {#if foodSearch.length > 0}
<ul bind:this={autocompleteList} class="z-50 fixed top-0 left-0 w-3/12 border border-x-gray-800"> <ul bind:this={autocompleteList} class="z-50 fixed top-0 left-0 w-3/12 border border-x-gray-800">
{#each foodSearch as f, i} {#each foodSearch as f, i}
<FoodSearchEntry itemLabel={f.food} highlighted={i === hiLiteIndex} on:click={() => setInputVal(f)} /> <FoodSearchEntry
itemLabel={f.food}
highlighted={i == hiLiteIndex}
on:click={() => setInputVal(f.food)}
/>
{/each} {/each}
</ul> </ul>
{/if} {/if}

View File

@@ -1,4 +1,6 @@
<script> <script>
// This doesn't update when navigating with arrows keys...
// Fucking svelte and reactivity again, here we fucking go
export let itemLabel; export let itemLabel;
export let highlighted; export let highlighted;
</script> </script>