|
| 1 | +<script> |
| 2 | + import { page } from '$app/stores'; |
| 3 | + import { goto, invalidateAll } from '$app/navigation'; |
| 4 | + import { superForm } from '$lib/client'; |
| 5 | + import SuperDebug from '$lib/client/SuperDebug.svelte'; |
| 6 | +
|
| 7 | + export let data; |
| 8 | +
|
| 9 | + const baseUrl = '/tests/issue-164-3'; |
| 10 | +
|
| 11 | + const { form, errors, reset, enhance } = superForm(data.form, { |
| 12 | + taintedMessage: null |
| 13 | + }); |
| 14 | +
|
| 15 | + $: editing = $page.params.viewOrEdit === 'edit'; |
| 16 | +
|
| 17 | + const gotoView = async () => { |
| 18 | + goto(baseUrl + '/view'); |
| 19 | + }; |
| 20 | + const back = async () => { |
| 21 | + history.back(); |
| 22 | + }; |
| 23 | + const resetAndBack = async () => { |
| 24 | + console.log('BEFORE reset', $form, $page.data.form.data); |
| 25 | + reset(); |
| 26 | + console.log('AFTER reset', $form, $page.data.form.data); |
| 27 | + await gotoView(); |
| 28 | + console.log('AFTER goto', $form, $page.data.form.data); |
| 29 | + }; |
| 30 | + const resetInvalidateBack = async () => { |
| 31 | + reset(); |
| 32 | + await invalidateAll(); |
| 33 | + gotoView(); |
| 34 | + }; |
| 35 | +</script> |
| 36 | + |
| 37 | +<form method="POST" use:enhance> |
| 38 | + Name: <input name="name" bind:value={$form.name} disabled={!editing} /> |
| 39 | + {#if $errors.name} |
| 40 | + <span style:color="red">{$errors.name}</span> |
| 41 | + {/if} |
| 42 | +</form> |
| 43 | + |
| 44 | +{#if !editing} |
| 45 | + <a href={baseUrl + '/edit'}>Edit</a> |
| 46 | +{:else} |
| 47 | + <button on:click={gotoView}>Goto View</button> |
| 48 | + <button on:click={back}>Back to View</button> |
| 49 | + <button on:click={() => reset()}>Reset</button> |
| 50 | + <button on:click={resetAndBack}>Reset & back</button> |
| 51 | + <button on:click={resetInvalidateBack}>Reset, invalidate, back</button> |
| 52 | + |
| 53 | + <ul> |
| 54 | + <li>Edit the name above</li> |
| 55 | + <li>Clicking the "Reset" will reset the form as expected</li> |
| 56 | + <li>Clicking the "Reset & back" button doesn't reset the form</li> |
| 57 | + <li> |
| 58 | + Clicking the browser's back button, "Goto View" or "Back to View" all |
| 59 | + are similar to the "Reset & back" button - form is not reset |
| 60 | + </li> |
| 61 | + <li>Clicking the "Reset, invalidate, back" button works</li> |
| 62 | + </ul> |
| 63 | +{/if} |
0 commit comments