Skip to content

Commit bf3df8d

Browse files
committed
Added test for drop menu without js.
1 parent 2a34246 commit bf3df8d

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { message, superValidate } 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
8+
.object({
9+
scoops: z.number().int().min(1).default(1),
10+
flavours: z
11+
.string()
12+
.array()
13+
.min(1, 'Please select at least one flavour')
14+
.default(['Mint choc chip'])
15+
})
16+
.refine((data) => data.flavours.length <= data.scoops, {
17+
message: "Can't order more flavours than scoops!",
18+
path: ['flavours']
19+
});
20+
21+
///// Load //////////////////////////////////////////////////////////
22+
23+
export const load: PageServerLoad = async () => {
24+
const form = await superValidate(schema);
25+
return { form };
26+
};
27+
28+
///// Form actions //////////////////////////////////////////////////
29+
30+
function join(flavours: string[]) {
31+
if (flavours.length === 1) return flavours[0];
32+
return `${flavours.slice(0, -1).join(', ')} and ${
33+
flavours[flavours.length - 1]
34+
}`;
35+
}
36+
37+
export const actions: Actions = {
38+
default: async ({ request }) => {
39+
const form = await superValidate(request, schema);
40+
41+
console.log('POST', form);
42+
43+
if (!form.valid) return fail(400, { form });
44+
45+
return message(
46+
form,
47+
`You ordered ${form.data.scoops} ${
48+
form.data.scoops === 1 ? 'scoop' : 'scoops'
49+
}
50+
of ${join(form.data.flavours)}`
51+
);
52+
}
53+
};
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<script lang="ts">
2+
import { superForm } from '$lib/client';
3+
import SuperDebug from '$lib/client/SuperDebug.svelte';
4+
5+
export let data;
6+
7+
const { form, errors, enhance, message } = superForm(data.form, {
8+
clearOnSubmit: 'none'
9+
});
10+
11+
let menu = ['Cookies and cream', 'Mint choc chip', 'Raspberry ripple'];
12+
</script>
13+
14+
<SuperDebug data={$form} />
15+
16+
<form method="POST" use:enhance>
17+
<h2>Size</h2>
18+
19+
<select name="scoops" bind:value={$form.scoops}>
20+
{#each ['One scoop', 'Two scoops', 'Three scoops'] as scoop, i}
21+
<option value={i + 1} selected={$form.scoops == i + 1}>{scoop}</option>
22+
{/each}
23+
</select>
24+
25+
{#if $errors.scoops}<p>{$errors.scoops}</p>{/if}
26+
27+
<h2>Flavours</h2>
28+
29+
{#each menu as flavour}
30+
<label>
31+
<input
32+
type="checkbox"
33+
bind:group={$form.flavours}
34+
name="flavours"
35+
value={flavour}
36+
/>
37+
{flavour}
38+
</label>
39+
{/each}
40+
41+
{#if $errors.flavours?._errors}<p>{$errors.flavours._errors}</p>{/if}
42+
43+
{#if $message}<p>{$message}</p>{/if}
44+
<button>Submit</button>
45+
</form>
46+
47+
<p class="info">
48+
<a href="https://svelte.dev/tutorial/group-inputs" target="_blank"
49+
>Original code from Svelte documentation</a
50+
>
51+
</p>
52+
53+
<style>
54+
.info {
55+
border-top: 1px solid gray;
56+
margin-top: 4rem;
57+
}
58+
</style>

0 commit comments

Comments
 (0)