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 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>
|
||||
|
||||
<svelte:head>
|
||||
@@ -95,11 +98,19 @@
|
||||
</div>
|
||||
|
||||
<!-- Add item -->
|
||||
<form method="POST" action="?/addItem" use:enhance
|
||||
onsubmit={() => { newItemText[template.id] = ''; }}
|
||||
<form method="POST" action="?/addItem" use:enhance={() => {
|
||||
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">
|
||||
<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..."
|
||||
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>
|
||||
|
||||
Reference in New Issue
Block a user