-
Notifications
You must be signed in to change notification settings - Fork 84
/
dom-value.tsx
80 lines (73 loc) · 2.14 KB
/
dom-value.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import {
getCollectionProps,
getFormProps,
getInputProps,
useForm,
} from '@conform-to/react';
import { parseWithZod } from '@conform-to/zod';
import type { ActionFunctionArgs, LoaderFunctionArgs } from '@remix-run/node';
import { json } from '@remix-run/node';
import { Form, useActionData, useLoaderData } from '@remix-run/react';
import { z } from 'zod';
import { Playground, Field } from '~/components';
const schema = z.discriminatedUnion('type', [
z.object({
type: z.literal('message'),
message: z.string(),
}),
z.object({
type: z.literal('title'),
title: z.string(),
}),
]);
export async function loader({ request }: LoaderFunctionArgs) {
const url = new URL(request.url);
return {
noClientValidate: url.searchParams.get('noClientValidate') === 'yes',
};
}
export async function action({ request }: ActionFunctionArgs) {
const formData = await request.formData();
const submission = parseWithZod(formData, { schema });
return json(submission.reply());
}
export default function Example() {
const { noClientValidate } = useLoaderData<typeof loader>();
const lastResult = useActionData<typeof action>();
const [form, fields] = useForm({
lastResult,
defaultValue: {
type: 'message',
message: 'Hello',
},
onValidate: !noClientValidate
? ({ formData }) => parseWithZod(formData, { schema })
: undefined,
});
return (
<Form method="post" {...getFormProps(form)}>
<Playground title="Observe DOM Value" result={{ value: form.value }}>
<Field label="Type" meta={fields.type}>
{getCollectionProps(fields.type, {
type: 'radio',
options: ['title', 'message'],
}).map((props) => (
<label key={props.key}>
<input {...props} />
{props.value}
</label>
))}
</Field>
{fields.type.value === 'message' ? (
<Field key="message" label="Message" meta={fields.message}>
<input {...getInputProps(fields.message, { type: 'text' })} />
</Field>
) : fields.type.value === 'title' ? (
<Field key="title" label="Title" meta={fields.title}>
<input {...getInputProps(fields.title, { type: 'text' })} />
</Field>
) : null}
</Playground>
</Form>
);
}