From e0eca4228bc7a9e3ddebe6fa6145f8d5b524457f Mon Sep 17 00:00:00 2001 From: Frederic Cabassut Date: Sat, 21 Mar 2026 09:17:46 +0100 Subject: [PATCH] fix: checklist Enter key focuses new item on reopened notes The addItem() focus logic used positional indexing (inputs[afterIndex+1]) but afterIndex came from the full items[] array while DOM inputs only render active (unchecked) items. On reopened notes, checked items are interleaved in the parsed content, causing the index offset to be wrong. Fix: use data-item-id attribute for targeted querySelector lookup. Test: reopened note with interleaved checked/unchecked items. Introduced in e5688c9 (active/done split). Co-Authored-By: Claude Opus 4.6 (1M context) --- src/lib/components/Checklist.svelte | 8 ++++++-- tests/e2e/checklist.spec.ts | 25 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/lib/components/Checklist.svelte b/src/lib/components/Checklist.svelte index 898cf2d..4f71d25 100644 --- a/src/lib/components/Checklist.svelte +++ b/src/lib/components/Checklist.svelte @@ -73,9 +73,12 @@ items.splice(afterIndex + 1, 0, newItem); items = [...items]; emitChange(); + // Focus by unique ID — positional indexing breaks when checked items + // are interleaved in items[] (e.g. reopened notes) because the DOM + // only renders active items as inputs + const newId = newItem.id; setTimeout(() => { - const inputs = document.querySelectorAll('[data-testid="checklist-input"]'); - inputs[afterIndex + 1]?.focus(); + document.querySelector(`[data-item-id="${newId}"]`)?.focus(); }, 0); } @@ -140,6 +143,7 @@ class="flex-1 min-w-0 bg-transparent text-sm outline-none {item.checked ? 'text-[var(--text-muted)] line-through' : 'text-[var(--text)]'}" placeholder="List item" data-testid="checklist-input" + data-item-id={item.id} />