Skip to content

Commit 05cca10

Browse files
committed
remove preflight method in favor of option, simplify factory
1 parent e6ebca5 commit 05cca10

File tree

8 files changed

+25
-48
lines changed

8 files changed

+25
-48
lines changed

packages/kit/src/exports/public.d.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2039,8 +2039,6 @@ export type RemoteForm<Input extends RemoteFormInput | void, Output> = {
20392039
action: string;
20402040
[attachment: symbol]: (node: HTMLFormElement) => void;
20412041
};
2042-
/** Preflight checks */
2043-
preflight(schema: StandardSchemaV1<Input, any>): RemoteForm<Input, Output>;
20442042
/** Validate the form contents programmatically */
20452043
validate(options?: {
20462044
/** Set this to `true` to also show validation issues of fields that haven't been touched yet. */

packages/kit/src/runtime/app/server/remote/form.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -276,11 +276,6 @@ export function form(validate_or_fn, maybe_fn) {
276276
get: () => 0
277277
});
278278

279-
Object.defineProperty(instance, 'preflight', {
280-
// preflight is a noop on the server
281-
value: () => instance
282-
});
283-
284279
Object.defineProperty(instance, 'validate', {
285280
value: () => {
286281
throw new Error('Cannot call validate() on the server');

packages/kit/src/runtime/client/remote-functions/form.svelte.js

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,14 @@ export function form(id) {
5555

5656
/** @param {RemoteFormFactoryOptions<T>} options */
5757
function create_instance(options) {
58-
const { key, resetAfterSuccess = true } = options;
58+
const { key, preflight, initialData, resetAfterSuccess = true } = options;
5959
const action_id = id + (key !== undefined ? `/${JSON.stringify(key)}` : '');
6060
const action = '?/remote=' + encodeURIComponent(action_id);
6161

6262
/**
6363
* @type {Record<string, string | string[] | File | File[]>}
6464
*/
65-
let input = $state({});
65+
let input = $state(initialData ?? {});
6666

6767
/** @type {InternalRemoteFormIssue[]} */
6868
let raw_issues = $state.raw([]);
@@ -76,7 +76,7 @@ export function form(id) {
7676
let pending_count = $state(0);
7777

7878
/** @type {StandardSchemaV1 | undefined} */
79-
let preflight_schema = undefined;
79+
let preflight_schema = preflight;
8080

8181
/** @type {HTMLFormElement | null} */
8282
let element = null;
@@ -511,13 +511,6 @@ export function form(id) {
511511
pending: {
512512
get: () => pending_count
513513
},
514-
preflight: {
515-
/** @type {RemoteForm<T, U>['preflight']} */
516-
value: (schema) => {
517-
preflight_schema = schema;
518-
return instance;
519-
}
520-
},
521514
validate: {
522515
/** @type {RemoteForm<any, any>['validate']} */
523516
value: async ({ includeUntouched = false, preflightOnly = false, submitter } = {}) => {
@@ -593,22 +586,7 @@ export function form(id) {
593586
/** @type {RemoteFormFactoryOptions<T> | undefined } */
594587
const options = arg && typeof arg === 'object' ? arg : undefined;
595588
const key = options ? options.key : /** @type {ExtractId<T> | undefined} */ (arg);
596-
597-
let entry = instances.get(key);
598-
if (!entry) {
599-
const instance = create_instance(options ?? { key });
600-
601-
if (options?.preflight) {
602-
instance.preflight(options.preflight);
603-
}
604-
// seed optional initial input data
605-
if (options?.initialData) {
606-
instance.fields.set(options.initialData);
607-
}
608-
609-
entry = { count: 0, instance };
610-
instances.set(key, entry);
611-
}
589+
const entry = instances.get(key) ?? { count: 0, instance: create_instance(options ?? { key }) };
612590

613591
try {
614592
$effect.pre(() => {

packages/kit/test/apps/basics/src/routes/remote/form/imperative/+page.svelte

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@
22
import { set_message } from '../form.remote.js';
33
import * as v from 'valibot';
44
5-
const message_form = set_message();
6-
75
const schema = v.object({
86
message: v.picklist(
97
['hello', 'goodbye', 'unexpected error', 'expected error', 'redirect'],
108
'message is invalid'
119
)
1210
});
11+
const message_form = set_message({
12+
preflight: schema
13+
});
1314
</script>
1415

15-
<form {...message_form.preflight(schema)}>
16+
<form {...message_form}>
1617
<label>
1718
<span>Message</span>
1819
<input {...message_form.fields.message.as('text')} />

packages/kit/test/apps/basics/src/routes/remote/form/preflight-only/+page.svelte

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
55
const data = get();
66
7-
const set_form = set();
8-
97
const schema = v.object({
108
a: v.pipe(v.string(), v.maxLength(7, 'a is too long')),
119
b: v.string(),
1210
c: v.string()
1311
});
12+
const set_form = set({
13+
preflight: schema
14+
});
1415
</script>
1516

1617
<!-- TODO use await here once async lands -->
@@ -23,7 +24,7 @@
2324
<hr />
2425

2526
<form
26-
{...set_form.preflight(schema)}
27+
{...set_form}
2728
oninput={() => set_form.validate({ preflightOnly: true })}
2829
onchange={() => set_form.validate()}
2930
>

packages/kit/test/apps/basics/src/routes/remote/form/preflight/+page.svelte

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@
44
55
const number = get_number();
66
7-
const number_form = set_number();
8-
const enhanced = set_number('enhanced');
9-
107
const schema = v.object({
118
number: v.pipe(v.number(), v.maxValue(20, 'too big'))
129
});
10+
const number_form = set_number({
11+
preflight: schema
12+
});
13+
const enhanced = set_number({
14+
key: 'enhanced',
15+
preflight: schema
16+
});
1317
</script>
1418

1519
<p>number.current: {number.current}</p>
@@ -21,7 +25,7 @@
2125

2226
<hr />
2327

24-
<form data-default {...number_form.preflight(schema)}>
28+
<form data-default {...number_form}>
2529
{#each number_form.fields.number.issues() as issue}
2630
<p>{issue.message}</p>
2731
{/each}
@@ -38,7 +42,7 @@
3842

3943
<form
4044
data-enhanced
41-
{...enhanced.preflight(schema).enhance(async ({ submit }) => {
45+
{...enhanced.enhance(async ({ submit }) => {
4246
await submit();
4347
})}
4448
>

packages/kit/test/apps/basics/src/routes/remote/form/validate/+page.svelte

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@
22
import { issue_path_form, my_form } from './form.remote.js';
33
import * as v from 'valibot';
44
5-
const my_form_form = my_form();
65
const schema = v.object({
76
foo: v.picklist(['a', 'b', 'c']),
87
bar: v.picklist(['d', 'e']),
98
button: v.optional(v.literal('submitter'))
109
});
10+
const my_form_form = my_form({
11+
preflight: schema
12+
});
1113
let submitter;
1214
$inspect(my_form_form.fields.allIssues());
1315
</script>
1416

15-
<form id="my-form" {...my_form_form.preflight(schema)} oninput={() => my_form_form.validate()}>
17+
<form id="my-form" {...my_form_form} oninput={() => my_form_form.validate()}>
1618
{#each my_form_form.fields.foo.issues() as issue}
1719
<p>{issue.message}</p>
1820
{/each}

packages/kit/types/index.d.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2013,8 +2013,6 @@ declare module '@sveltejs/kit' {
20132013
action: string;
20142014
[attachment: symbol]: (node: HTMLFormElement) => void;
20152015
};
2016-
/** Preflight checks */
2017-
preflight(schema: StandardSchemaV1<Input, any>): RemoteForm<Input, Output>;
20182016
/** Validate the form contents programmatically */
20192017
validate(options?: {
20202018
/** Set this to `true` to also show validation issues of fields that haven't been touched yet. */

0 commit comments

Comments
 (0)