Skip to content

Commit 4da8a11

Browse files
committed
Storybook support
1 parent c05a2c7 commit 4da8a11

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

CHANGELOG.md

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

1212
- Support for object unions, with implicit default values.
13+
- [Storybook](https://storybook.js.org/recipes/@sveltejs/kit#@sveltejs/kit) support!
1314

1415
### Fixed
1516

src/lib/client/superForm.ts

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -361,18 +361,34 @@ function multipleFormIdError(id: string | undefined) {
361361
);
362362
}
363363

364+
/////////////////////////////////////////////////////////////////////
365+
364366
/**
365367
* V1 compatibilty. resetForm = false and taintedMessage = true
366368
*/
367-
let legacyMode = false;
369+
let LEGACY_MODE = false;
368370

369371
try {
370372
// @ts-expect-error Vite define check
371-
if (SUPERFORMS_LEGACY) legacyMode = true;
373+
if (SUPERFORMS_LEGACY) LEGACY_MODE = true;
372374
} catch {
373375
// No legacy mode defined
374376
}
375377

378+
/**
379+
* Storybook compatibility mode, basically disables the navigating store.
380+
*/
381+
let STORYBOOK_MODE = false;
382+
383+
try {
384+
// @ts-expect-error Storybook check
385+
if (globalThis.STORIES) STORYBOOK_MODE = true;
386+
} catch {
387+
// No Storybook
388+
}
389+
390+
/////////////////////////////////////////////////////////////////////
391+
376392
/**
377393
* Initializes a SvelteKit form, for convenient handling of values, errors and sumbitting data.
378394
* @param {SuperValidated} form Usually data.form from PageData.
@@ -393,11 +409,15 @@ export function superForm<
393409
let initialValidator: FormOptions<T, M, In>['validators'] | undefined = undefined;
394410

395411
{
396-
if (options.legacy ?? legacyMode) {
412+
if (options.legacy ?? LEGACY_MODE) {
397413
if (options.resetForm === undefined) options.resetForm = false;
398414
if (options.taintedMessage === undefined) options.taintedMessage = true;
399415
}
400416

417+
if (STORYBOOK_MODE) {
418+
if (options.applyAction === undefined) options.applyAction = false;
419+
}
420+
401421
initialValidator = options.validators;
402422

403423
options = {
@@ -1795,11 +1815,13 @@ export function superForm<
17951815
cancel
17961816
};
17971817

1798-
// Check for goto to a different route in the events
1799-
const unsubCheckforNav = navigating.subscribe(($nav) => {
1800-
if (!$nav || $nav.from?.route.id === $nav.to?.route.id) return;
1801-
cancel();
1802-
});
1818+
const unsubCheckforNav = STORYBOOK_MODE
1819+
? () => {}
1820+
: navigating.subscribe(($nav) => {
1821+
// Check for goto to a different route in the events
1822+
if (!$nav || $nav.from?.route.id === $nav.to?.route.id) return;
1823+
cancel();
1824+
});
18031825

18041826
for (const event of formEvents.onResult) {
18051827
await event(data);
@@ -1912,6 +1934,8 @@ export function superForm<
19121934
// Redirect messages are handled in onDestroy and afterNavigate in client/form.ts.
19131935
if (cancelled || result.type != 'redirect') {
19141936
htmlForm.completed({ cancelled });
1937+
} else if (STORYBOOK_MODE) {
1938+
htmlForm.completed({ cancelled, clearAll: true });
19151939
} else {
19161940
const unsub = navigating.subscribe(($nav) => {
19171941
if ($nav) return;

0 commit comments

Comments
 (0)