Skip to content

Commit

Permalink
fix tests and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
edmundhung committed Jul 22, 2023
1 parent 4dd0ed5 commit b18b8e3
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 44 deletions.
13 changes: 5 additions & 8 deletions docs/checkbox-and-radio-group.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ function Example() {
.collection(color, {
type: 'radio',
options: ['red', 'green', 'blue'],
ariaAttributes: true,
})
.map((props, index) => (
<div key={index}>
Expand All @@ -57,19 +56,18 @@ function Example() {

## Checkbox

Setting up a checkbox group would be similar to a radio group with `type` being set to `checkbox`. However, there is one caveat when validating a checkbox group: Conform will transform the value to an array only when there are more than one checkboxes selected. To ensure a consistent data structure, you need to preprocess the data as shown in the snippet.
Setting up a checkbox group would be similar to a radio group except the `type` is set to `checkbox`.

```tsx
import { useForm } from '@conform-to/react';
import { parse } from '@conform-to/zod';
import { z } from 'zod';

const schema = z.object({
answer: z.preprocess(
// To ensure the value is always an array
(value) => !Array.isArray(value) ? [value] : value,
z.string().array().nonEmpty('At least one answer is required')
),
answer: z
.string()
.array()
.nonEmpty('At least one answer is required'),
});

function Example() {
Expand All @@ -87,7 +85,6 @@ function Example() {
.collection(answer, {
type: 'checkbox',
options: ['a', 'b', 'c', 'd'],
ariaAttributes: true,
})
.map((props, index) => (
<div key={index}>
Expand Down
45 changes: 10 additions & 35 deletions playground/app/routes/collection.tsx
Original file line number Diff line number Diff line change
@@ -1,39 +1,14 @@
import { conform, parse, useForm } from '@conform-to/react';
import { conform, useForm } from '@conform-to/react';
import { parse } from '@conform-to/zod';
import { type LoaderArgs, type ActionArgs, json } from '@remix-run/node';
import { Form, useActionData, useLoaderData } from '@remix-run/react';
import { z } from 'zod';
import { Playground, Field } from '~/components';

interface Schema {
singleChoice: string;
multipleChoice: string;
}

function parseForm(formData: FormData) {
return parse(formData, {
resolve({ singleChoice, multipleChoice }) {
const error: Record<string, string> = {};

if (!singleChoice) {
error.singleChoice = 'Required';
}

if (!multipleChoice) {
error.multipleChoice = 'Required';
}

if (error.singleChoice || error.multipleChoice) {
return { error };
}

return {
value: {
singleChoice,
multipleChoice,
},
};
},
});
}
const schema = z.object({
singleChoice: z.string({ required_error: 'Required' }),
multipleChoice: z.string().array().min(1, 'Required'),
});

export async function loader({ request }: LoaderArgs) {
const url = new URL(request.url);
Expand All @@ -45,20 +20,20 @@ export async function loader({ request }: LoaderArgs) {

export async function action({ request }: ActionArgs) {
const formData = await request.formData();
const submission = parseForm(formData);
const submission = parse(formData, { schema });

return json(submission);
}

export default function Example() {
const { noClientValidate } = useLoaderData<typeof loader>();
const lastSubmission = useActionData<typeof action>();
const [form, { singleChoice, multipleChoice }] = useForm<Schema>({
const [form, { singleChoice, multipleChoice }] = useForm({
id: 'collection',
lastSubmission,
shouldRevalidate: 'onInput',
onValidate: !noClientValidate
? ({ formData }) => parseForm(formData)
? ({ formData }) => parse(formData, { schema })
: undefined,
});

Expand Down
2 changes: 1 addition & 1 deletion tests/integrations/collection.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ async function runValidationScenario(page: Page) {
error: {},
value: {
singleChoice: 'y',
multipleChoice: 'c',
multipleChoice: ['c'],
},
},
null,
Expand Down

0 comments on commit b18b8e3

Please sign in to comment.