diff --git a/frontend/src/lib/components/Energy/EmptyFoodComp.svelte b/frontend/src/lib/components/Energy/EmptyFoodComp.svelte index a6e0753..cc92ce1 100644 --- a/frontend/src/lib/components/Energy/EmptyFoodComp.svelte +++ b/frontend/src/lib/components/Energy/EmptyFoodComp.svelte @@ -3,6 +3,8 @@ import { main } from "$wails/models"; import { CreateFood, GetLastPer100 } from "$wails/main/App"; import { foodStore } from "$lib/store/Energy/foodStore"; + import FoodSearchEntry from "./FoodSearchEntry.svelte"; + import { onMount } from "svelte"; let item: main.Food = { food: "", @@ -20,6 +22,16 @@ let per100Edited: boolean = false; let per100Element: HTMLTableCellElement; let nameElement: HTMLTableCellElement; + let autocompleteList: HTMLUListElement; + let foodSearch: main.Food[] = []; + + // Maybe it would be a good idea to use $ instead of update down there... + // Maybe it's a topic for another day + $: { + if (!name) { + foodSearch = []; + } + } async function update(event: KeyboardEvent & { currentTarget: EventTarget & HTMLTableCellElement }) { name = name.trim(); @@ -27,6 +39,11 @@ description = description.trim(); per100 = per100.trim(); + if (!name) { + foodSearch = []; + return; + } + if (!per100Edited && event.currentTarget == per100Element) per100Edited = true; if (event.key == "Enter") { @@ -50,27 +67,53 @@ foodStore.update((value) => [res.data, ...value]); nameElement.focus(); + foodSearch = []; } if (!per100Edited) GetLastPer100(name.trim()).then((res) => { - console.log(res.data); if (res.success && res.data) { - console.log(res.data[0].food); - per100 = res.data[0].per100.toString(); + foodSearch = res.data; } }); } + + let hiLiteIndex: number | null = null; + function navigateList(e: KeyboardEvent) { + if (e.key === "ArrowDown" && hiLiteIndex && hiLiteIndex <= foodSearch.length - 1) { + hiLiteIndex === null ? (hiLiteIndex = 0) : (hiLiteIndex += 1); + } else if (e.key === "ArrowUp" && hiLiteIndex !== null) { + hiLiteIndex === 0 ? (hiLiteIndex = foodSearch.length - 1) : (hiLiteIndex -= 1); + } else if (e.key === "Enter") { + // @ts-ignore ITS NOT NULL YOU ASSHAT + // WE CHECKED + // ITS NOT + setInputVal(foodSearch[hiLiteIndex]); + } else { + return; + } + } + function setInputVal(val: string) { + name = val; + hiLiteIndex = null; + foodSearch = []; + } + + $: { + if (nameElement && autocompleteList) { + const { top, left, height } = nameElement.getBoundingClientRect(); + autocompleteList.style.top = `${top + height}px`; + autocompleteList.style.left = `${left}px`; + } + } diff --git a/frontend/src/lib/components/Energy/FoodSearchEntry.svelte b/frontend/src/lib/components/Energy/FoodSearchEntry.svelte new file mode 100644 index 0000000..863b130 --- /dev/null +++ b/frontend/src/lib/components/Energy/FoodSearchEntry.svelte @@ -0,0 +1,13 @@ + + + +
  • + {itemLabel} +