-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c550453
commit 3752ca9
Showing
16 changed files
with
328 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 80 additions & 1 deletion
81
exercises/05.complex-structures/01.problem.nested/README.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,82 @@ | ||
# Nested Object | ||
|
||
TODO: write this | ||
So far we've just put the image information as properties on our | ||
`NoteEditorSchema`, but the `id`, `file`, and `altText` fields are really all | ||
just part of a single object: An image. So it would be better to represent this | ||
as a nested set of field properties under the `NoteEditorSchema` under `image`. | ||
|
||
However, because forms don't support nested objects, we'll need to use a utility | ||
from Conform to help us out. Here's an example that resembles what you'll be | ||
doing: | ||
|
||
```tsx lines=16,22-23 | ||
// example inspired from the Conform docs | ||
import { | ||
useForm, | ||
useFieldset, | ||
conform, | ||
type FieldConfig, | ||
} from '@conform-to/react' | ||
|
||
function Example() { | ||
const [form, fields] = useForm<Schema>({ | ||
// ... config stuff including the schema | ||
}) | ||
|
||
return ( | ||
<form {...form.props}> | ||
<AddressFields config={fields.address} /> | ||
</form> | ||
) | ||
} | ||
|
||
function AddressFields({ config }: { config: FieldConfig<Address> }) { | ||
const ref = useRef<HTMLFieldSetElement>(null) | ||
const fields = useFieldset(ref, config) | ||
return ( | ||
<fieldset ref={ref}> | ||
<input {...conform.input(fields.street)} /> | ||
<input {...conform.input(fields.zipcode)} /> | ||
<input {...conform.input(fields.city)} /> | ||
<input {...conform.input(fields.country)} /> | ||
</fieldset> | ||
) | ||
} | ||
``` | ||
|
||
We'll also get our type by using Zod's inference utility: | ||
|
||
```tsx | ||
const RocketSchema = z.object({ | ||
// ... | ||
}) | ||
type RocketType = z.infer<typeof RocketSchema> | ||
|
||
function RocketFields({ config }: { config: FieldConfig<RocketType> }) { | ||
// ... | ||
} | ||
``` | ||
|
||
So, fundamentally, we want to make this change: | ||
|
||
```ts remove=4-6 add=7-11 | ||
{ | ||
title: string | ||
content: string | ||
imageId: string | ||
file: File | ||
altText: string | ||
image: { | ||
id: string | ||
file: File | ||
altText: string | ||
} | ||
} | ||
``` | ||
|
||
And we want that hooked up to our form. That should be enough to get you going! | ||
|
||
- [📜 `useFieldset`](https://conform.guide/api/react#usefieldset) | ||
- [📜 Conform Complex Structures](https://conform.guide/complex-structures) | ||
- [📜 Zod Type Inference](https://zod.dev/?id=type-inference) | ||
- [📜 React ref](https://react.dev/reference/react/useRef) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 11 additions & 1 deletion
12
exercises/05.complex-structures/01.solution.nested/README.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,13 @@ | ||
# Nested Object | ||
|
||
TODO: write this | ||
👨💼 Great! Even though forms don't technically support nested objects (again, | ||
nested forms aren't allowed), Conform allows us to simulate that. This makes it | ||
much easier for us to maintain a sensible data structure for our forms. | ||
|
||
But did you notice that our database allows notes to have more than a single | ||
image? Sure would be nice if we could add more than one image to a note, right? | ||
Let's do that next! | ||
|
||
💯 Now that our form is wired up with conform, we can render the errors for | ||
these fields. Feel free to try that if you've got extra time (🧝♂️ Kellie will | ||
do it for you if you don't). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 56 additions & 1 deletion
57
exercises/05.complex-structures/02.problem.lists/README.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,58 @@ | ||
# Field Lists | ||
|
||
TODO: write this | ||
👨💼 We want users to be able to upload multiple images to the notes. So we're | ||
going to need to adjust our Zod config to allow for an array: | ||
|
||
```tsx | ||
const RocketSchema = z.object({}) | ||
const RocketsSchema = z.array(RocketSchema) | ||
``` | ||
|
||
This is easy enough, but remember that Forms don't support arrays. So we need to | ||
reach for Conform's utilities to make this a possibility. Luckily, Conform has | ||
a handy utility called | ||
[`useFieldList`](https://conform.guide/api/react#usefieldlist) which allows you | ||
to represent an array of fields in a Form. Here's the example from the Conform | ||
docs: | ||
|
||
```tsx | ||
import { useForm, useFieldList, list } from '@conform-to/react' | ||
|
||
/** | ||
* Consider the schema as follow: | ||
*/ | ||
type Schema = { | ||
items: string[] | ||
} | ||
|
||
function Example() { | ||
const [form, { items }] = useForm<Schema>() | ||
const itemsList = useFieldList(form.ref, items) | ||
|
||
return ( | ||
<fieldset ref={ref}> | ||
{itemsList.map((item, index) => ( | ||
<div key={item.key}> | ||
{/* Setup an input per item */} | ||
<input name={item.name} /> | ||
|
||
{/* Error of each item */} | ||
<span>{item.error}</span> | ||
|
||
{/* Setup a delete button (Note: It is `items` not `item`) */} | ||
<button {...list.remove(items.name, { index })}>Delete</button> | ||
</div> | ||
))} | ||
|
||
{/* Setup a button that can append a new row with optional default value */} | ||
<button {...list.append(items.name, { defaultValue: '' })}>add</button> | ||
</fieldset> | ||
) | ||
} | ||
``` | ||
|
||
You'll notice the `list` utilities in there. We'll get to that in the next step. | ||
For now, just focus on getting the `useFieldList` working for our images. | ||
|
||
- [📜 Zod arrays](https://zod.dev/?id=arrays) | ||
- [📜 Conform `useFieldList`](https://conform.guide/api/react#usefieldlist) |
Oops, something went wrong.