Skip to content

Commit 5668961

Browse files
committed
Updating the same variable to the same value prevented onChange.
1 parent 5c1dafc commit 5668961

File tree

6 files changed

+73
-3
lines changed

6 files changed

+73
-3
lines changed

CHANGELOG.md

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

2222
### Fixed
2323

24+
- Updating the same variable to the same value prevented the [onChange](https://superforms.rocks/concepts/events#onchange) event from being triggered.
2425
- Factorized [SuperDebug](https://superforms.rocks/super-debug) clipboard script
2526

2627
## [2.18.1] - 2024-09-13

src/lib/client/superForm.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,9 +1282,9 @@ export function superForm<
12821282
return currentlyTainted;
12831283
});
12841284
}
1285-
}
12861285

1287-
NextChange_setHtmlEvent({ paths });
1286+
NextChange_setHtmlEvent({ paths });
1287+
}
12881288
}
12891289

12901290
/**

src/routes/(v2)/v2/Navigation.svelte

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@
6464
'simple-tainted',
6565
'validity-objects',
6666
'issue-466',
67-
'spa-error'
67+
'spa-error',
68+
'issue-470'
6869
].sort();
6970
</script>
7071

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import type { Actions, PageServerLoad } from './$types.js';
2+
3+
import { superValidate, message } from '$lib/index.js';
4+
import { zod } from '$lib/adapters/zod.js';
5+
import { fail } from '@sveltejs/kit';
6+
import { schema } from './schema.js';
7+
8+
export const load: PageServerLoad = async () => {
9+
return { form: await superValidate(zod(schema)) };
10+
};
11+
12+
export const actions: Actions = {
13+
default: async ({ request }) => {
14+
const form = await superValidate(request, zod(schema));
15+
console.log(form);
16+
17+
if (!form.valid) return fail(400, { form });
18+
19+
return message(form, 'Form posted successfully!');
20+
}
21+
};
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<script lang="ts">
2+
import { superForm } from '$lib/index.js';
3+
import SuperDebug from '$lib/index.js';
4+
5+
export let data;
6+
7+
let changeEvent: unknown | undefined = undefined;
8+
const { form } = superForm(data.form, {
9+
onChange: (e) => {
10+
changeEvent = e;
11+
}
12+
});
13+
14+
function setNumberTwice() {
15+
const value = Math.random();
16+
$form.value = value;
17+
$form.value = value;
18+
}
19+
function setNumberOnce() {
20+
const value = Math.random();
21+
$form.value = value;
22+
}
23+
</script>
24+
25+
<SuperDebug data={$form} />
26+
27+
<h3>Superforms testing ground - Zod</h3>
28+
29+
<p>
30+
Bug: If the same value is written twice, no change event is fired If first button is clicked, no
31+
change event is fired (however the value is updated). If the second button is clicked a change
32+
event is fired
33+
</p>
34+
35+
<p>
36+
Change event was:
37+
<code>{JSON.stringify(changeEvent)}</code>
38+
</p>
39+
40+
<button on:click={setNumberTwice}>Click me to set the same value twice</button>
41+
<button on:click={setNumberOnce}>Click me to set a value once</button>
42+
<button on:click={() => (changeEvent = undefined)}>Clear event</button>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { z } from 'zod';
2+
3+
export const schema = z.object({
4+
value: z.number().nullable()
5+
});

0 commit comments

Comments
 (0)