Skip to content

Commit 211ddd2

Browse files
committed
Fixed #164 finally!
1 parent cf4602f commit 211ddd2

File tree

5 files changed

+185
-4
lines changed

5 files changed

+185
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Fixed
1111

12-
- Fixed exception message when the `dataType` option isn't set to `'json'` and the schema contains a nested object.
12+
- Fixed persisting form data when component used data from `$page` in combination with `onDestroy`. ([#164](https://github.com/ciscoheat/sveltekit-superforms/issues/164), thank you to everyone in that thread!)
13+
- Fixed exception message when the `dataType` option isn't set to `'json'` and the schema contains a nested object. ([#225](https://github.com/ciscoheat/sveltekit-superforms/issues/225))
1314
- Superforms are now ignoring normal SvelteKit form actions when they are posted. ([#230](https://github.com/ciscoheat/sveltekit-superforms/issues/230))
1415

1516
## [1.2.0] - 2023-07-06

src/lib/client/index.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -423,13 +423,17 @@ export function superForm<
423423
value: Parameters<typeof _formData.set>[0],
424424
options: { taint?: TaintOption<UnwrappedT> } = {}
425425
) => {
426+
// Need to clone the value, so it won't refer
427+
// to $page for example.
428+
const newValue = clone(value);
429+
426430
Tainted_update(
427-
value,
431+
newValue,
428432
Context.taintedFormState,
429433
options.taint ?? true
430434
);
431-
Context.taintedFormState = clone(value);
432-
return _formData.set(value);
435+
Context.taintedFormState = newValue;
436+
return _formData.set(newValue);
433437
},
434438
update: (
435439
updater: Parameters<typeof _formData.update>[0],
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { superValidate, message } from '$lib/server';
2+
import { fail } from '@sveltejs/kit';
3+
import { z } from 'zod';
4+
5+
import type { Actions, PageServerLoad } from './$types';
6+
7+
const schema = z.object({
8+
name: z.string().min(1).default('TEST'),
9+
email: z.string().email()
10+
});
11+
12+
///// Load //////////////////////////////////////////////////////////
13+
14+
export const load: PageServerLoad = async () => {
15+
const form = await superValidate(schema);
16+
return { form };
17+
};
18+
19+
///// Form actions //////////////////////////////////////////////////
20+
21+
export const actions: Actions = {
22+
default: async ({ request }) => {
23+
const form = await superValidate(request, schema);
24+
25+
console.log('POST', form);
26+
27+
if (!form.valid) return fail(400, { form });
28+
29+
return message(form, 'Form posted successfully!');
30+
}
31+
};
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<script lang="ts">
2+
import Form from './Form.svelte';
3+
import { page } from '$app/stores';
4+
import SuperDebug from '$lib/client/SuperDebug.svelte';
5+
import { browser } from '$app/environment';
6+
7+
let isOpen = false;
8+
9+
page.subscribe(($page) => {
10+
console.log('🚀 ~ file: +page.svelte:12 ~ page:', $page.data.form.data);
11+
});
12+
13+
/*
14+
if (browser) {
15+
setInterval(() => {
16+
console.log('$page - +page.svelte', $page.data.form.data);
17+
}, 500);
18+
}
19+
*/
20+
</script>
21+
22+
<SuperDebug label="$page.data" data={$page.data.form.data} /><br />
23+
24+
{#if isOpen}
25+
<Form
26+
on:cancel={() => {
27+
isOpen = false;
28+
}}
29+
/>
30+
{:else}
31+
<button
32+
style="margin-bottom: 12px;"
33+
on:click={() => {
34+
isOpen = !isOpen;
35+
}}
36+
>
37+
Toggle
38+
</button>
39+
{/if}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<script lang="ts">
2+
import { page } from '$app/stores';
3+
import { superForm } from '$lib/client';
4+
import { createEventDispatcher, onDestroy } from 'svelte';
5+
import SuperDebug from '$lib/client/SuperDebug.svelte';
6+
import { get } from 'svelte/store';
7+
8+
const dispatch = createEventDispatcher();
9+
10+
const frm = superForm($page.data.form);
11+
const { form, errors, message, enhance, reset } = frm;
12+
13+
page.subscribe(($page) => {
14+
console.log(
15+
'🚀 ~ file: Form.svelte:18 ~ page.subscribe ~ page:',
16+
$page.data.form.data
17+
);
18+
});
19+
</script>
20+
21+
<SuperDebug label="Form" data={$form} />
22+
23+
{#if $message}
24+
<div
25+
class="status"
26+
class:error={$page.status >= 400}
27+
class:success={$page.status == 200}
28+
>
29+
{$message}
30+
</div>
31+
{/if}
32+
33+
<form method="POST" use:enhance>
34+
<label>
35+
Name<br />
36+
<input name="name" data-invalid={$errors.name} bind:value={$form.name} />
37+
{#if $errors.name}<span class="invalid">{$errors.name}</span>{/if}
38+
</label>
39+
40+
<label>
41+
Email<br />
42+
<input
43+
name="email"
44+
type="email"
45+
data-invalid={$errors.email}
46+
bind:value={$form.email}
47+
/>
48+
{#if $errors.email}<span class="invalid">{$errors.email}</span>{/if}
49+
</label>
50+
51+
<button disabled>Submit</button>
52+
<button
53+
type="button"
54+
on:click={() => {
55+
reset();
56+
console.log('Resetted:', get(frm.form));
57+
setTimeout(() => dispatch('cancel'), 500);
58+
}}>Cancel</button
59+
>
60+
</form>
61+
62+
<hr />
63+
<p>
64+
<a target="_blank" href="https://superforms.vercel.app/api"
65+
>API Reference</a
66+
>
67+
</p>
68+
69+
<style>
70+
.invalid {
71+
color: red;
72+
}
73+
74+
.status {
75+
color: white;
76+
padding: 4px;
77+
padding-left: 8px;
78+
border-radius: 2px;
79+
font-weight: 500;
80+
}
81+
82+
.status.success {
83+
background-color: seagreen;
84+
}
85+
86+
.status.error {
87+
background-color: #ff2a02;
88+
}
89+
90+
input {
91+
background-color: #ddd;
92+
}
93+
94+
a {
95+
text-decoration: underline;
96+
}
97+
98+
hr {
99+
margin-top: 4rem;
100+
}
101+
102+
form {
103+
padding-top: 1rem;
104+
padding-bottom: 1rem;
105+
}
106+
</style>

0 commit comments

Comments
 (0)