Skip to content

Commit 1d22ed6

Browse files
committed
Added test for onChange target
1 parent 8212ebd commit 1d22ed6

File tree

4 files changed

+76
-1
lines changed

4 files changed

+76
-1
lines changed

src/routes/(v2)/v2/Navigation.svelte

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
'validators-clear',
3636
'submit-prog',
3737
'modify-reset',
38-
'index-errors'
38+
'index-errors',
39+
'onchange-target'
3940
].sort();
4041
</script>
4142

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { zod } from '$lib/adapters/zod.js';
2+
import { message, superValidate } from '$lib/server/index.js';
3+
import { schema } from './schema.js';
4+
import { fail } from '@sveltejs/kit';
5+
6+
export const load = async () => {
7+
const form = await superValidate(zod(schema));
8+
return { form };
9+
};
10+
11+
export const actions = {
12+
default: async ({ request }) => {
13+
const formData = await request.formData();
14+
console.log(formData);
15+
16+
const form = await superValidate(formData, zod(schema));
17+
console.log('POST', form);
18+
19+
if (!form.valid) return fail(400, { form });
20+
21+
return message(form, 'Posted OK!');
22+
}
23+
};
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<script lang="ts">
2+
import { superForm } from '$lib/client/index.js';
3+
4+
export let data;
5+
6+
let changes = 0;
7+
8+
const { form, errors, enhance } = superForm(data.form, {
9+
taintedMessage: null,
10+
onChange(event) {
11+
if (event.target) {
12+
changes = changes + 1;
13+
}
14+
}
15+
});
16+
</script>
17+
18+
<div id="name">NAME:{$form.name}</div>
19+
20+
<div id="changes">CHANGES:{changes}</div>
21+
22+
<form method="POST" use:enhance>
23+
Name: <div class="edit" contenteditable="true" bind:textContent={$form.name}></div>
24+
{#if $errors.name}<span class="invalid">{$errors.name}</span>{/if}
25+
</form>
26+
27+
<style lang="scss">
28+
.edit {
29+
display: inline-block;
30+
min-width: 50%;
31+
background-color: #ddd;
32+
margin: 0.5rem;
33+
}
34+
35+
form {
36+
margin: 2rem 0;
37+
38+
input {
39+
background-color: #dedede;
40+
}
41+
42+
.invalid {
43+
color: crimson;
44+
}
45+
}
46+
</style>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { z } from 'zod';
2+
3+
export const schema = z.object({
4+
name: z.string().min(1)
5+
});

0 commit comments

Comments
 (0)