Skip to content

Commit af0f1e8

Browse files
committed
isTainted now works in generic components.
Fixes #395.
1 parent 27b9b8e commit af0f1e8

File tree

7 files changed

+107
-2
lines changed

7 files changed

+107
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1414
### Fixed
1515

1616
- `submit` method now falls back to submit, if no support for requestSubmit in browser.
17+
- `isTainted` now handles the type of `$tainted` in generic components.
1718

1819
## [2.12.2] - 2024-03-29
1920

src/lib/client/superForm.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ export type SuperForm<
239239
options: T extends T ? FormOptions<T, M> : never; // Need this to distribute T so it works with unions
240240

241241
enhance: (el: HTMLFormElement, events?: SuperFormEvents<T, M>) => ReturnType<typeof kitEnhance>;
242-
isTainted: (path?: T extends T ? FormPath<T> | TaintedFields<T> | boolean : never) => boolean;
242+
isTainted: (path?: FormPath<T> | Record<string, unknown> | boolean | undefined) => boolean;
243243
reset: (options?: ResetOptions<T>) => void;
244244
submit: (submitter?: HTMLElement | Event | EventTarget | null) => void;
245245

@@ -1134,7 +1134,7 @@ export function superForm<
11341134
}
11351135

11361136
function Tainted_isTainted(
1137-
path?: FormPath<T> | TaintedFields<Record<string, unknown>> | boolean
1137+
path?: FormPath<T> | Record<string, unknown> | boolean | undefined
11381138
): boolean {
11391139
if (typeof path === 'boolean') return path;
11401140
if (typeof path === 'object') return Tainted__isObjectTainted(path);

src/routes/(v2)/v2/tainted-array/+page.svelte

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
const { form, enhance, tainted, isTainted } = superForm(data.form, {
88
dataType: 'json'
99
});
10+
11+
isTainted('details');
12+
isTainted($tainted?.details);
13+
// @ts-expect-error Invalid path
14+
isTainted('nope');
1015
</script>
1116

1217
<SuperDebug data={{ $form, $tainted }} />
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(form);
18+
19+
if (!form.valid) return fail(400, { form });
20+
21+
return message(form, 'Posted OK!');
22+
}
23+
};
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<script lang="ts">
2+
import { superForm } from '$lib/client/index.js';
3+
import SuperDebug from '$lib/client/SuperDebug.svelte';
4+
import Button from './Button.svelte';
5+
//import { zod } from '$lib/adapters/zod.js'
6+
//import { schema } from './schema.js';
7+
8+
export let data;
9+
10+
const superform = superForm(data.form, { taintedMessage: false });
11+
const { form, errors, message, enhance } = superform;
12+
13+
const set = new Map([
14+
['test', 123],
15+
['hello', 456]
16+
]);
17+
</script>
18+
19+
<SuperDebug data={set} />
20+
21+
{#if $message}<h4>{$message}</h4>{/if}
22+
23+
<form method="POST" use:enhance>
24+
<label>
25+
Name: <input
26+
name="name"
27+
bind:value={$form.name}
28+
aria-invalid={$errors.name ? 'true' : undefined}
29+
/>
30+
{#if $errors.name}<span class="invalid">{$errors.name}</span>{/if}
31+
</label>
32+
<label>
33+
Email: <input
34+
name="email"
35+
bind:value={$form.email}
36+
aria-invalid={$errors.email ? 'true' : undefined}
37+
/>
38+
{#if $errors.email}<span class="invalid">{$errors.email}</span>{/if}
39+
</label>
40+
<div>
41+
<Button form={superform} />
42+
</div>
43+
</form>
44+
45+
<style lang="scss">
46+
form {
47+
margin: 2rem 0;
48+
49+
input {
50+
background-color: #dedede;
51+
}
52+
53+
.invalid {
54+
color: crimson;
55+
}
56+
}
57+
</style>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<script lang="ts" context="module">
2+
type T = Record<string, unknown>;
3+
</script>
4+
5+
<script lang="ts" generics="T extends Record<string, unknown>">
6+
import type { SuperForm } from '$lib/index.js';
7+
8+
export let form: SuperForm<T>;
9+
10+
const { tainted, isTainted } = form;
11+
</script>
12+
13+
<button disabled={!isTainted($tainted)}>Submit</button>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { z } from 'zod';
2+
3+
export const schema = z.object({
4+
name: z.string().min(2),
5+
email: z.string().email()
6+
});

0 commit comments

Comments
 (0)