Fix checklist item add: form value cleared before submission
The bind:value + onsubmit pattern caused the input to be empty when enhance submitted the form. Replaced with post-submission DOM reset so the value is intact during submission. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -3,7 +3,10 @@
|
|||||||
|
|
||||||
let { data, form } = $props();
|
let { data, form } = $props();
|
||||||
let showNewTemplate = $state(false);
|
let showNewTemplate = $state(false);
|
||||||
let newItemText: Record<string, string> = $state({});
|
function resetForm(form: HTMLFormElement) {
|
||||||
|
const input = form.querySelector('input[name="text"]') as HTMLInputElement;
|
||||||
|
if (input) input.value = '';
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<svelte:head>
|
<svelte:head>
|
||||||
@@ -95,11 +98,19 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- Add item -->
|
<!-- Add item -->
|
||||||
<form method="POST" action="?/addItem" use:enhance
|
<form method="POST" action="?/addItem" use:enhance={() => {
|
||||||
onsubmit={() => { newItemText[template.id] = ''; }}
|
return async ({ update, result }) => {
|
||||||
|
await update();
|
||||||
|
if (result.type === 'success') {
|
||||||
|
const form = document.querySelector(`form[data-template="${template.id}"]`) as HTMLFormElement;
|
||||||
|
if (form) resetForm(form);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}}
|
||||||
|
data-template={template.id}
|
||||||
class="mt-2 flex gap-2">
|
class="mt-2 flex gap-2">
|
||||||
<input type="hidden" name="templateId" value={template.id} />
|
<input type="hidden" name="templateId" value={template.id} />
|
||||||
<input type="text" name="text" required bind:value={newItemText[template.id]}
|
<input type="text" name="text" required
|
||||||
placeholder="Add item..."
|
placeholder="Add item..."
|
||||||
class="flex-1 rounded-md border border-gray-200 px-3 py-1.5 text-sm focus:border-blue-500 focus:ring-1 focus:ring-blue-500 focus:outline-none dark:border-gray-600 dark:bg-gray-700 dark:text-white dark:placeholder-gray-400" />
|
class="flex-1 rounded-md border border-gray-200 px-3 py-1.5 text-sm focus:border-blue-500 focus:ring-1 focus:ring-blue-500 focus:outline-none dark:border-gray-600 dark:bg-gray-700 dark:text-white dark:placeholder-gray-400" />
|
||||||
<button type="submit" class="rounded-md border border-gray-300 px-3 py-1.5 text-sm text-gray-600 hover:bg-gray-100 dark:border-gray-600 dark:text-gray-400 dark:hover:bg-gray-700">Add</button>
|
<button type="submit" class="rounded-md border border-gray-300 px-3 py-1.5 text-sm text-gray-600 hover:bg-gray-100 dark:border-gray-600 dark:text-gray-400 dark:hover:bg-gray-700">Add</button>
|
||||||
|
|||||||
@@ -11,7 +11,10 @@
|
|||||||
let showLogForm = $state(false);
|
let showLogForm = $state(false);
|
||||||
let showNewChecklist = $state(false);
|
let showNewChecklist = $state(false);
|
||||||
let showImportChecklist = $state(false);
|
let showImportChecklist = $state(false);
|
||||||
let newItemText: Record<string, string> = $state({});
|
function resetInput(formEl: HTMLFormElement) {
|
||||||
|
const input = formEl.querySelector('input[name="text"]') as HTMLInputElement;
|
||||||
|
if (input) input.value = '';
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<svelte:head>
|
<svelte:head>
|
||||||
@@ -436,11 +439,19 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- Add item -->
|
<!-- Add item -->
|
||||||
<form method="POST" action="?/addChecklistItem" use:enhance
|
<form method="POST" action="?/addChecklistItem" use:enhance={() => {
|
||||||
onsubmit={() => { newItemText[checklist.id] = ''; }}
|
return async ({ update, result }) => {
|
||||||
|
await update();
|
||||||
|
if (result.type === 'success') {
|
||||||
|
const form = document.querySelector(`form[data-checklist="${checklist.id}"]`) as HTMLFormElement;
|
||||||
|
if (form) resetInput(form);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}}
|
||||||
|
data-checklist={checklist.id}
|
||||||
class="mt-2 flex gap-2">
|
class="mt-2 flex gap-2">
|
||||||
<input type="hidden" name="checklistId" value={checklist.id} />
|
<input type="hidden" name="checklistId" value={checklist.id} />
|
||||||
<input type="text" name="text" required bind:value={newItemText[checklist.id]}
|
<input type="text" name="text" required
|
||||||
placeholder="Add item..."
|
placeholder="Add item..."
|
||||||
class="flex-1 rounded-md border border-gray-200 px-2 py-1 text-sm focus:border-blue-500 focus:ring-1 focus:ring-blue-500 focus:outline-none dark:border-gray-600 dark:bg-gray-700 dark:text-white dark:placeholder-gray-400" />
|
class="flex-1 rounded-md border border-gray-200 px-2 py-1 text-sm focus:border-blue-500 focus:ring-1 focus:ring-blue-500 focus:outline-none dark:border-gray-600 dark:bg-gray-700 dark:text-white dark:placeholder-gray-400" />
|
||||||
<button type="submit" class="rounded-md px-2 py-1 text-sm text-blue-600 hover:bg-blue-50 dark:text-blue-400 dark:hover:bg-blue-900/20">
|
<button type="submit" class="rounded-md px-2 py-1 text-sm text-blue-600 hover:bg-blue-50 dark:text-blue-400 dark:hover:bg-blue-900/20">
|
||||||
|
|||||||
Reference in New Issue
Block a user