Skip to content

Commit 1fc10dd

Browse files
committed
Added options for scrollIntoView
Resetform fix
1 parent 29d4a8c commit 1fc10dd

File tree

5 files changed

+22
-9
lines changed

5 files changed

+22
-9
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,17 @@ Headlines: Added, Changed, Deprecated, Removed, Fixed, Security
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8-
## [Unreleased]
8+
## [1.2.0]
99

1010
### Added
1111

12+
- The `scrollToError` option can now take the same parameters as [scrollIntoView](https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView), which makes it work for nested scrollbars.
1213
- Added signature to more conveniently set a form-level error with `setError`.
1314

15+
### Fixed
16+
17+
- If the `resetForm` option was enabled, the form was reset even if `fail` was returned.
18+
1419
## [1.1.3] - 2023-06-29
1520

1621
### Fixed

src/lib/client/formEnhance.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,11 @@ export function formEnhance<T extends AnyZodObject, M>(
231231
: null;
232232

233233
if (!isElementInViewport(el, nav?.offsetHeight ?? 0)) {
234-
scrollToAndCenter(el, undefined, options.scrollToError);
234+
if (typeof options.scrollToError == 'string') {
235+
scrollToAndCenter(el, undefined, options.scrollToError);
236+
} else {
237+
el.scrollIntoView(options.scrollToError);
238+
}
235239
}
236240

237241
// Don't focus on the element if on mobile, it will open the keyboard
@@ -396,7 +400,9 @@ export function formEnhance<T extends AnyZodObject, M>(
396400

397401
// Deprecation fix
398402
const submitData =
399-
submit.formData ?? (submit as { data: FormData }).data;
403+
'formData' in submit
404+
? submit.formData
405+
: (submit as { data: FormData }).data;
400406

401407
if (options.SPA) {
402408
cancel();

src/lib/client/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export type FormOptions<T extends ZodValidation<AnyZodObject>, M> = Partial<{
7777
applyAction: boolean;
7878
invalidateAll: boolean;
7979
resetForm: boolean | (() => boolean);
80-
scrollToError: 'auto' | 'smooth' | 'off';
80+
scrollToError: 'auto' | 'smooth' | 'off' | boolean | ScrollIntoViewOptions;
8181
autoFocusOnError: boolean | 'detect';
8282
errorSelector: string;
8383
selectErrorText: boolean;
@@ -375,9 +375,9 @@ export function superForm<
375375
if (duplicateId.has(form.id)) {
376376
console.warn(
377377
`Duplicate form id found: "${form.id}"` +
378-
'. Multiple forms will receive the same data. Use the id option to differentiate between them, or if this is intended, set warnings.duplicateId option to false to disable this message.'
378+
'. Multiple forms will receive the same data. Use the id option to differentiate between them, ' +
379+
'or if this is intended, set warnings.duplicateId option to false to disable this message.'
379380
);
380-
break;
381381
} else {
382382
duplicateId.add(form.id);
383383
}
@@ -483,6 +483,7 @@ export function superForm<
483483
) {
484484
if (
485485
form.valid &&
486+
untaint &&
486487
options.resetForm &&
487488
(options.resetForm === true || options.resetForm())
488489
) {

src/routes/+page.server.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { z } from 'zod';
33
import { error, fail } from '@sveltejs/kit';
44
import type { Actions, PageServerLoad } from './$types';
55
import { redirect } from 'sveltekit-flash-message/server';
6-
import { RateLimiter } from 'sveltekit-rate-limiter';
6+
import { RateLimiter } from 'sveltekit-rate-limiter/server';
77

88
const limiter = new RateLimiter({
99
rates: {
@@ -86,7 +86,7 @@ export const actions = {
8686
console.log('FORM', form);
8787
if (!form.valid) return fail(400, { form });
8888

89-
if (!(await limiter.check(event))) {
89+
if (await limiter.isLimited(event)) {
9090
form.valid = false;
9191
form.message = { type: 'error', message: 'You are rate limited' };
9292
return fail(429, { form });

src/routes/Navigation.svelte

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
<a href="/tests/superform-c">Side-effect 2</a> |
2424
<a href="/tests/multiselect">Multi-select</a> |
2525
<a href="/tests/spa-values-disappearing">SPA onUpdate</a> |
26-
<a href="/posted">Posted store</a>
26+
<a href="/posted">Posted store</a> |
27+
<a href="/tests/flash-onerror">Flash onError</a>
2728
</nav>
2829

2930
<style>

0 commit comments

Comments
 (0)