Skip to content

Commit f0c71d4

Browse files
committed
posted was reset to false after posting, when resetForm was true.
1 parent b5ca120 commit f0c71d4

File tree

3 files changed

+25
-17
lines changed

3 files changed

+25
-17
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ 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+
## [2.2.1] - 2024-02-16
99

1010
### Fixed
1111

1212
- Added `focusOnError` option to `SuperForm.validateForm` type (it was only in the implementation).
1313
- Enums could not be detected as an invalid value if the posted value was an empty string, instead it defaulted to the enum first value.
14+
- `$posted` was reset by mistake to `false` after posting, when `resetForm` option was true.
1415

1516
## [2.2.0] - 2024-02-15
1617

src/lib/client/superForm.ts

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -851,7 +851,7 @@ export function superForm<
851851

852852
async function Form_updateFromValidation(form: SuperValidated<T, M, In>, successResult: boolean) {
853853
if (form.valid && successResult && Form_shouldReset(form.valid, successResult)) {
854-
Form_reset(form.message);
854+
Form_reset({ message: form.message, posted: true });
855855
} else {
856856
rebind(form, successResult, undefined, true);
857857
}
@@ -867,12 +867,14 @@ export function superForm<
867867
}
868868
}
869869

870-
function Form_reset(message?: M, data?: Partial<T>, id?: string) {
870+
function Form_reset(
871+
opts: { message?: M; data?: Partial<T>; id?: string; posted?: boolean } = {}
872+
) {
871873
const resetData = clone(initialForm);
872-
resetData.data = { ...resetData.data, ...data };
873-
if (id !== undefined) resetData.id = id;
874+
resetData.data = { ...resetData.data, ...opts.data };
875+
if (opts.id !== undefined) resetData.id = opts.id;
874876

875-
rebind(resetData, true, message, false);
877+
rebind(resetData, true, opts.message, false, opts.posted);
876878
}
877879

878880
const Form_updateFromActionResult: FormUpdate = async (result) => {
@@ -885,7 +887,7 @@ export function superForm<
885887
if (result.type == 'redirect') {
886888
// All we need to do if redirected is to reset the form.
887889
// No events should be triggered because technically we're somewhere else.
888-
if (Form_shouldReset(true, true)) Form_reset();
890+
if (Form_shouldReset(true, true)) Form_reset({ posted: true });
889891
return;
890892
}
891893

@@ -1136,7 +1138,8 @@ export function superForm<
11361138
form: SuperValidated<T, M, In>,
11371139
untaint: TaintedFields<T> | boolean,
11381140
message?: M,
1139-
keepFiles?: boolean
1141+
keepFiles?: boolean,
1142+
posted?: boolean
11401143
) {
11411144
//console.log('🚀 ~ file: superForm.ts:721 ~ rebind ~ form:', form.data); //debug
11421145

@@ -1152,7 +1155,7 @@ export function superForm<
11521155
Message.set(message);
11531156
Errors.set(form.errors);
11541157
FormId.set(form.id);
1155-
Posted.set(form.posted);
1158+
Posted.set(posted ?? form.posted);
11561159
// Constraints and shape will only be set when they exist.
11571160
if (form.constraints) Constraints.set(form.constraints);
11581161
if (form.shape) Shape.set(form.shape);
@@ -1391,11 +1394,11 @@ export function superForm<
13911394
posted: Posted,
13921395

13931396
reset(options?: ResetOptions<T>) {
1394-
return Form_reset(
1395-
options?.keepMessage ? Data.message : undefined,
1396-
options?.data,
1397-
options?.id
1398-
);
1397+
return Form_reset({
1398+
message: options?.keepMessage ? Data.message : undefined,
1399+
data: options?.data,
1400+
id: options?.id
1401+
});
13991402
},
14001403

14011404
isTainted: Tainted_isTainted,

src/routes/(v2)/v2/posted/+page.svelte

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,21 @@
55
66
export let data;
77
8-
const { form, errors, posted, enhance } = superForm(data.form, {
8+
const { form, errors, posted, enhance, reset } = superForm(data.form, {
9+
resetForm: true,
910
taintedMessage: false
1011
});
1112
</script>
1213

1314
<SuperDebug data={{ $posted, $form }} />
1415

15-
<h3>Superforms testing ground - Zod</h3>
16+
<h3>Testing posted store</h3>
17+
18+
<h5>POSTED:{String($posted)}</h5>
1619

1720
{#if $posted}
1821
<div class="status" class:error={$page.status >= 400} class:success={$page.status == 200}>
19-
Form posted!
22+
{#if $page.status >= 400}Form failed{:else}Form posted!{/if}
2023
</div>
2124
{/if}
2225

@@ -39,6 +42,7 @@
3942
</label>
4043

4144
<button>Submit</button>
45+
<button style="background-color:brown" type="button" on:click={() => reset()}>Reset</button>
4246
</form>
4347

4448
<hr />

0 commit comments

Comments
 (0)