Skip to content

Commit

Permalink
Timing issue in SPA mode displayed errors when pressing enter.
Browse files Browse the repository at this point in the history
  • Loading branch information
ciscoheat committed Sep 29, 2023
1 parent bf16374 commit 68cec07
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ Headlines: Added, Changed, Deprecated, Removed, Fixed, Security
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.7.4] - 2023-09-29

### Fixed

- Timing issue in [SPA mode](https://superforms.rocks/concepts/spa) displayed errors for valid data, when submitting a form by pressing enter.

## [1.7.3] - 2023-09-28

### Fixed
Expand Down
7 changes: 7 additions & 0 deletions src/lib/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,13 @@ export function superForm<
page.subscribe(async (pageUpdate) => {
if (!options.applyAction) return;

// Strange timing issue in SPA mode forces a wait here,
// otherwise errors will appear even if the form is valid
// when pressing enter to submit the form (not when clicking a submit button!)
if (options.SPA) {
await new Promise((r) => setTimeout(r, 0));
}

const untaint = pageUpdate.status >= 200 && pageUpdate.status < 300;

if (pageUpdate.form && typeof pageUpdate.form === 'object') {
Expand Down
76 changes: 76 additions & 0 deletions src/routes/tests/spa-submit/+page.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<script lang="ts">
import { superForm } from '$lib/client';
import SuperDebug from '$lib/client/SuperDebug.svelte';
export let data;
let status = '';
const { form, enhance, errors } = superForm(data.form, {
SPA: true,
resetForm: true,
validators: {
title: (value) =>
value.trim().length >= 3
? null
: 'Bitte geben Sie mindestens 3 Zeichen ein'
},
onUpdate: ({ form }) => {
if (form.valid) {
// send data;
status = 'OK';
} else {
status = 'Not valid';
}
}
});
</script>

<SuperDebug data={{ $form, $errors }} />

<h3>Superforms testing ground</h3>

<p>{status}</p>

<form method="POST" use:enhance>
<label>
Name<br />
<input
class="input"
type="text"
bind:value={$form.title}
aria-invalid={errors ? 'true' : undefined}
/>
</label>
{#if $errors.title}<div class="invalid">{$errors.title}</div>{/if}

<button>Submit</button>
</form>

<hr />
<p>
<a target="_blank" href="https://superforms.rocks/api">API Reference</a>
</p>

<style>
.invalid {
color: red;
}
input {
background-color: #ddd;
}
a {
text-decoration: underline;
}
hr {
margin-top: 4rem;
}
form {
padding-top: 1rem;
padding-bottom: 1rem;
}
</style>
11 changes: 11 additions & 0 deletions src/routes/tests/spa-submit/+page.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { z } from 'zod';
import { superValidate } from '$lib/client';

const Schema = z.object({
title: z.string().min(3)
});

export const load = async () => {
const form = await superValidate(Schema);
return { form };
};

0 comments on commit 68cec07

Please # to comment.