Skip to content

Commit

Permalink
docs: improve example code in the Nested Object and Array documentati…
Browse files Browse the repository at this point in the history
…on (#646)
  • Loading branch information
jtakahashi64 committed May 27, 2024
1 parent 32306c0 commit f83d133
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 5 deletions.
46 changes: 43 additions & 3 deletions docs/complex-structures.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,24 @@ To set up a nested field, just call the `getFieldset()` method from the parent f

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

const schema = z.object({
address: z.object({
street: z.string(),
zipcode: z.string(),
city: z.string(),
country: z.string(),
}),
});

function Example() {
const [form, fields] = useForm();
const [form, fields] = useForm({
onValidate({ formData }) {
return parseWithZod(formData, { schema });
},
});
const address = fields.address.getFieldset();

return (
Expand All @@ -41,9 +56,19 @@ When you need to setup a list of fields, you can call the `getFieldList()` metho

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

const schema = z.object({
tasks: z.array(z.string()),
});

function Example() {
const [form, fields] = useForm();
const [form, fields] = useForm({
onValidate({ formData }) {
return parseWithZod(formData, { schema });
},
});
const tasks = fields.tasks.getFieldList();

return (
Expand All @@ -68,9 +93,24 @@ You can also combine both `getFieldset()` and `getFieldList()` for nested array.

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

const schema = z.object({
todos: z.array(
z.object({
title: z.string(),
notes: z.string(),
}),
),
});

function Example() {
const [form, fields] = useForm();
const [form, fields] = useForm({
onValidate({ formData }) {
return parseWithZod(formData, { schema });
},
});
const todos = fields.todos.getFieldList();

return (
Expand Down
34 changes: 32 additions & 2 deletions docs/ja/complex-structures.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,24 @@ Conform は、データ構造を示すために `object.property` および `arr

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

const schema = z.object({
address: z.object({
street: z.string(),
zipcode: z.string(),
city: z.string(),
country: z.string(),
}),
});

function Example() {
const [form, fields] = useForm();
const [form, fields] = useForm({
onValidate({ formData }) {
return parseWithZod(formData, { schema });
},
});
const address = fields.address.getFieldset();

return (
Expand All @@ -41,9 +56,24 @@ function Example() {

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

const schema = z.object({
todos: z.array(
z.object({
title: z.string(),
notes: z.string(),
}),
),
});

function Example() {
const [form, fields] = useForm();
const [form, fields] = useForm({
onValidate({ formData }) {
return parseWithZod(formData, { schema });
},
});
const tasks = fields.tasks.getFieldList();

return (
Expand Down

0 comments on commit f83d133

Please sign in to comment.