-
Notifications
You must be signed in to change notification settings - Fork 85
/
form-control.tsx
136 lines (129 loc) · 3.73 KB
/
form-control.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import {
useForm,
getFormProps,
getInputProps,
getTextareaProps,
FormStateInput,
FormProvider,
} 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.object({
name: z.string({ required_error: 'Name is required' }),
message: z.string({ required_error: 'Message is required' }),
number: z.number({ required_error: 'Number is required' }),
});
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 FormControl() {
const { noClientValidate } = useLoaderData<typeof loader>();
const lastResult = useActionData<typeof action>();
const [form, fields] = useForm({
lastResult,
onValidate: !noClientValidate
? ({ formData }) => parseWithZod(formData, { schema })
: undefined,
});
return (
<FormProvider context={form.context}>
<Form method="post" {...getFormProps(form)}>
<FormStateInput formId={form.id} />
<Playground title="Form Control" result={lastResult}>
<Field label="Name" meta={fields.name}>
<input {...getInputProps(fields.name, { type: 'text' })} />
</Field>
<Field label="Message" meta={fields.message}>
<textarea {...getTextareaProps(fields.message)} />
</Field>
<Field label="Number" meta={fields.number}>
<input {...getInputProps(fields.number, { type: 'number' })} />
</Field>
<div className="flex flex-col gap-2">
<button
className="rounded-md border p-2 hover:border-black"
{...form.validate.getButtonProps()}
>
Validate Form
</button>
<button
className="rounded-md border p-2 hover:border-black"
{...form.validate.getButtonProps({
name: fields.message.name,
})}
>
Validate Message
</button>
<button
className="rounded-md border p-2 hover:border-black"
{...form.update.getButtonProps({
name: fields.message.name,
value: 'Hello World',
})}
>
Update message
</button>
<button
className="rounded-md border p-2 hover:border-black"
{...form.update.getButtonProps({
value: {
number: 13579,
name: 'Conform',
message:
'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
},
})}
>
Update form
</button>
<button
className="rounded-md border p-2 hover:border-black"
{...form.update.getButtonProps({
name: fields.number.name,
value: 123,
})}
>
Update number
</button>
<button
className="rounded-md border p-2 hover:border-black"
{...form.update.getButtonProps({
name: fields.message.name,
value: '',
validated: true,
})}
>
Clear message
</button>
<button
className="rounded-md border p-2 hover:border-black"
{...form.reset.getButtonProps({
name: fields.message.name,
})}
>
Reset message
</button>
<button
className="rounded-md border p-2 hover:border-black"
{...form.reset.getButtonProps()}
>
Reset form
</button>
</div>
</Playground>
</Form>
</FormProvider>
);
}