From 6ebc314a2b8efa82b1efd6580cce0425aed3dc59 Mon Sep 17 00:00:00 2001 From: Stephan Richter Date: Thu, 23 Apr 2026 15:45:47 +0200 Subject: [PATCH] =?UTF-8?q?overhauled=20autocomplete=20=E2=80=93=20should?= =?UTF-8?q?=20now=20work=20with=20mobile=20devices,=20too?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Stephan Richter --- frontend/src/Components/Autocomplete.svelte | 58 ++++++++++++++------- 1 file changed, 39 insertions(+), 19 deletions(-) diff --git a/frontend/src/Components/Autocomplete.svelte b/frontend/src/Components/Autocomplete.svelte index 64133f3b..30ade901 100644 --- a/frontend/src/Components/Autocomplete.svelte +++ b/frontend/src/Components/Autocomplete.svelte @@ -14,7 +14,7 @@ const ignore = ['ArrowLeft','ArrowRight']; //let candidate = $state({ display : '' }); - let selected = $state([]); + let selected = $state(null); let candidates = $state([]); let timer = null; @@ -53,12 +53,13 @@ const idx = select.value; candidate = candidates[idx]; candidates = []; - selected = []; + selected = null; onSelect(candidate); } async function fetchCandidates(){ candidates = await getCandidates(candidate.display); + selected = null; if (selected>candidates.length) selected = candidates.length; } @@ -66,28 +67,28 @@ if (ignore.includes(ev.key)) return; if (ev.key == 'ArrowDown'){ ev.preventDefault(); - selected = selected.length < 1 ? [0] : [selected[0]+1] - if (selected[0] >= candidates.length) selected = [0]; + selected = selected == null ? 0: selected +1; + if (selected >= candidates.length) selected = 0; return false; } if (ev.key == 'ArrowUp'){ ev.preventDefault(); - selected = selected.length < 1 ? [-1] : [selected[0]-1] - if (selected[0] < 0) selected = [candidates.length-1]; + selected = selected == null ? candidates.length -1 : selected -1; + if (selected < 0) selected = candidates.length -1; return false; } if (ev.key == 'Enter'|| ev.key == 'Tab'){ ev.preventDefault(); - if (selected.length>0) { - candidate = candidates[selected[0]]; + if (selected != null && selected < candidates.length) { + candidate = candidates[selected]; candidates = []; - selected = []; + selected = null; onSelect(candidate); return false; } if (ev.key == 'Enter') { candidates = []; - selected = []; + selected = null; if (onCommit(candidate)) candidate = { display : '' }; } return false; @@ -95,7 +96,7 @@ if (ev.key == 'Escape'){ ev.preventDefault(); candidates = []; - selected = []; + selected = null; return false; } @@ -104,23 +105,42 @@ timer = setTimeout(fetchCandidates,400); return false; } + + function select(index){ + candidate = candidates[index]; + selected = null; + candidates = []; + onSelect(candidate); + + } - + {#if candidates && candidates.length > 0} - + {/if} - \ No newline at end of file +