Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrap forms with RHF's FormProvider #127

Merged
merged 2 commits into from
Dec 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions apps/web/app/routes/examples.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ export default function Component() {
<SidebarLayout.NavLink to={'/examples/forms/use-fetcher'}>
useFetcher
</SidebarLayout.NavLink>
<SidebarLayout.NavLink to={'/examples/forms/use-form-state'}>
useFormState
</SidebarLayout.NavLink>
<SidebarLayout.NavLink to={'/examples/forms/multiple-forms'}>
Multiple forms
</SidebarLayout.NavLink>
Expand Down
62 changes: 62 additions & 0 deletions apps/web/app/routes/examples/forms/use-form-state.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import hljs from 'highlight.js/lib/common'
import type {
ActionFunction,
LoaderFunction,
MetaFunction,
} from '@remix-run/node'
import { formAction } from '~/formAction'
import { z } from 'zod'
import Form from '~/ui/form'
import { metaTags } from '~/helpers'
import { makeDomainFunction } from 'domain-functions'
import Example from '~/ui/example'
import { useFormState } from 'react-hook-form'
import SubmitButton from '~/ui/submit-button'

const title = 'useFormState'
const description = `In this example, we use the useFormState hook from React Hook Form to access the state of the form in our custom
components without having to use render props. This makes it easier to have certain functionality in custom components across all forms.`

export const meta: MetaFunction = () => metaTags({ title, description })

const code = `const schema = z.object({ age: z.number().min(1) })

const Button = ({ children, ...props }: JSX.IntrinsicElements['button']) => {
const { isDirty } = useFormState()
return (
<SubmitButton {...props} disabled={!isDirty}>
{children}
</SubmitButton>
)
}

export default () => (
<Form schema={schema} buttonComponent={Button} />
)
}`

const schema = z.object({ age: z.number().min(1) })

export const loader: LoaderFunction = () => ({
code: hljs.highlight(code, { language: 'ts' }).value,
})

const mutation = makeDomainFunction(schema)(async (values) => values)

export const action: ActionFunction = async ({ request }) =>
formAction({ request, schema, mutation })

const Button = ({ children, ...props }: JSX.IntrinsicElements['button']) => {
const { isDirty } = useFormState()
return (
<SubmitButton {...props} disabled={!isDirty}>
{children}
</SubmitButton>
)
}

export default () => (
<Example title={title} description={description}>
<Form schema={schema} buttonComponent={Button} />
</Example>
)
12 changes: 7 additions & 5 deletions packages/remix-forms/src/createForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import type {
ValidationMode,
DeepPartial,
} from 'react-hook-form'
import { useForm } from 'react-hook-form'
import { useForm, FormProvider } from 'react-hook-form'
import { zodResolver } from '@hookform/resolvers/zod'
import type { FormErrors, FormValues } from './mutations'
import type {
Expand Down Expand Up @@ -432,10 +432,12 @@ function createForm({
}, [transition.state])

return (
<Component method={method} onSubmit={onSubmit} {...props}>
{beforeChildren}
{customChildren ?? defaultChildren()}
</Component>
<FormProvider {...form}>
<Component method={method} onSubmit={onSubmit} {...props}>
{beforeChildren}
{customChildren ?? defaultChildren()}
</Component>
</FormProvider>
)
}
}
Expand Down