|
1 | 1 | /** @import { StandardSchemaV1 } from '@standard-schema/spec' */ |
2 | | -/** @import { RemoteFormInput, RemoteForm, RemoteQueryOverride, RemoteFormFactory } from '@sveltejs/kit' */ |
3 | | -/** @import { InternalRemoteFormIssue, RemoteFunctionResponse } from 'types' */ |
| 2 | +/** @import { RemoteFormInput, RemoteForm, RemoteQueryOverride, RemoteFormFactory, RemoteFormFactoryOptions } from '@sveltejs/kit' */ |
| 3 | +/** @import { ExtractId, InternalRemoteFormIssue, RemoteFunctionResponse } from 'types' */ |
4 | 4 | /** @import { Query } from './query.svelte.js' */ |
5 | 5 | import { app_dir, base } from '$app/paths/internal/client'; |
6 | 6 | import * as devalue from 'devalue'; |
@@ -53,8 +53,9 @@ export function form(id) { |
53 | 53 | /** @type {Map<any, { count: number, instance: RemoteForm<T, U> }>} */ |
54 | 54 | const instances = new Map(); |
55 | 55 |
|
56 | | - /** @param {string | number | boolean} [key] */ |
57 | | - function create_instance(key) { |
| 56 | + /** @param {RemoteFormFactoryOptions<T>} options */ |
| 57 | + function create_instance(options) { |
| 58 | + const { key, resetAfterSuccess = true } = options; |
58 | 59 | const action_id = id + (key !== undefined ? `/${JSON.stringify(key)}` : ''); |
59 | 60 | const action = '?/remote=' + encodeURIComponent(action_id); |
60 | 61 |
|
@@ -411,7 +412,7 @@ export function form(id) { |
411 | 412 | instance[createAttachmentKey()] = create_attachment( |
412 | 413 | form_onsubmit(({ submit, form }) => |
413 | 414 | submit().then(() => { |
414 | | - if (!issues.$) { |
| 415 | + if (!issues.$ && resetAfterSuccess) { |
415 | 416 | form.reset(); |
416 | 417 | } |
417 | 418 | }) |
@@ -452,7 +453,7 @@ export function form(id) { |
452 | 453 | formaction: action, |
453 | 454 | onclick: form_action_onclick(({ submit, form }) => |
454 | 455 | submit().then(() => { |
455 | | - if (!issues.$) { |
| 456 | + if (!issues.$ && resetAfterSuccess) { |
456 | 457 | form.reset(); |
457 | 458 | } |
458 | 459 | }) |
@@ -588,8 +589,26 @@ export function form(id) { |
588 | 589 | } |
589 | 590 |
|
590 | 591 | /** @type {RemoteFormFactory<T, U>} */ |
591 | | - const factory = (key) => { |
592 | | - const entry = instances.get(key) ?? { count: 0, instance: create_instance(key) }; |
| 592 | + const factory = (arg) => { |
| 593 | + /** @type {RemoteFormFactoryOptions<T> | undefined } */ |
| 594 | + const options = arg && typeof arg === 'object' ? arg : undefined; |
| 595 | + 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 | + } |
593 | 612 |
|
594 | 613 | try { |
595 | 614 | $effect.pre(() => { |
|
0 commit comments