Skip to content

Commit

Permalink
feat: more scope docs
Browse files Browse the repository at this point in the history
  • Loading branch information
airjp73 committed Jun 24, 2024
1 parent 616960c commit eba9ff4
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
52 changes: 50 additions & 2 deletions apps/docs-v2/app/routes/_docs.scoping.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,58 @@ A core feature (and one of the most powerful features) of RVF is the ability to
down to just one part of your data.
This gives us the ability to create flexible abstractions for whole subforms or even just a single field.

## Subforms
## Subform example

To demonstrate this, let's create a simple form for entering people's names
and ages. If you want to skip to the final code, go ahead and check out the
and emails. If you want to skip to the final code, go ahead and check out the
code in this demo.

<ScopingNameForm />

## Using `scope`

<Row>
<Col>
When you call `useForm`, it returns a [`ReactFormApi`](/reference/form-api) object.
The type of this object takes a single generic paramter, which is the type of the default values.
</Col>
<Col>
```tsx
// `form` has the type `ReactFormApi<{ foo: string }>`
const form = useForm<DefaultValuesType>({
defaultValues: { foo: "bar" },
// ...etc
});
```
</Col>
</Row>

<Row>
<Col>
One of the methods on `ReactFormApi` is `scope`.
When you call `scope` and pass it the name of a field, it returns a `FormScope<FieldType>`
where `FieldType` is the type of the field.
You can even continue chaining `scope` calls to get even deeper, which is useful for deeply nested or recursive data.
</Col>
<Col>
```tsx
const form = useForm<DefaultValuesType>({
defaultValues: {
foo: "foo",
bar: { baz: "baz" }
},
// ...etc
});

const fooScope = form.scope("foo");
// `fooScope` has the type `FormScope<string>`

const barScope = form.scope("bar");
// `barScope` has the type `FormScope<{ baz: string }>`

const bazScope = barScope.scope("baz");
// `barScope` has the type `FormScope<string>`
```

</Col>
</Row>
5 changes: 4 additions & 1 deletion apps/docs-v2/app/ui/mdx/mdx.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ export function Note({ children }: { children: React.ReactNode }) {

export function Row({ children }: { children: React.ReactNode }) {
return (
<div className="grid grid-cols-1 items-start gap-x-16 gap-y-10 xl:max-w-none xl:grid-cols-2">
<div
data-row
className="grid grid-cols-1 items-start gap-x-16 gap-y-10 xl:max-w-none xl:grid-cols-2 peer-data-[row]:mt-6 peer"
>
{children}
</div>
);
Expand Down

0 comments on commit eba9ff4

Please sign in to comment.