Skip to content

Commit 2aff430

Browse files
committed
dateProxy fix for snapshots
1 parent 84b944e commit 2aff430

File tree

5 files changed

+47
-1
lines changed

5 files changed

+47
-1
lines changed

CHANGELOG.md

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

1717
- Fixed diff algorithm problem with tainted objects.
1818
- Prevented crash when custom validity doesn't exist for an element.
19+
- `dateProxy` didn't restore properly with [snapshots](https://superforms.rocks/concepts/snapshots).
1920

2021
### Changed
2122

src/lib/client/proxies.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,9 @@ function _stringProxy<T extends Record<string, unknown>, Path extends FormPaths<
350350
if (typeof value === 'number' && isNaN(value)) return '';
351351
return String(value);
352352
} else if (type == 'date') {
353-
const date = value as unknown as Date;
353+
const date =
354+
typeof value === 'string' || typeof value === 'number' ? new Date(value) : (value as Date);
355+
354356
if (isNaN(date as unknown as number)) return '';
355357

356358
switch (options.dateFormat) {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Schema } from './schemas.js';
2+
import { superValidate } from '$lib/index.js';
3+
import { valibot } from '$lib/adapters/valibot.js';
4+
5+
export const load = async () => {
6+
const initialData = {
7+
date: new Date()
8+
};
9+
10+
const form = await superValidate(initialData, valibot(Schema));
11+
12+
return {
13+
form
14+
};
15+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<script lang="ts">
2+
import { Schema } from './schemas.js';
3+
import { valibotClient } from '$lib/adapters/valibot.js';
4+
import { dateProxy, superForm } from '$lib/client/index.js';
5+
6+
let { data } = $props();
7+
8+
const { form, capture, restore } = superForm(data.form, {
9+
validators: valibotClient(Schema)
10+
});
11+
12+
const proxy = dateProxy(form, 'date', {
13+
format: 'datetime-local',
14+
empty: 'undefined'
15+
});
16+
17+
// works without this, fails with it when refreshing page, not when from another page
18+
export const snapshot = { capture, restore };
19+
20+
$inspect({ $form, $proxy });
21+
</script>
22+
23+
<input type="datetime-local" bind:value={$proxy} />
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import * as v from 'valibot';
2+
3+
export const Schema = v.object({
4+
date: v.date()
5+
});

0 commit comments

Comments
 (0)