-
-
Notifications
You must be signed in to change notification settings - Fork 139
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
5115e8f
commit 78e70fb
Showing
10 changed files
with
266 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@saas-ui/forms': major | ||
--- | ||
|
||
New useForm hook that returns typed form components |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,128 @@ | ||
import { forwardRef } from 'react' | ||
|
||
import { type HTMLChakraProps, chakra } from '@chakra-ui/react' | ||
import { zodResolver } from '@hookform/resolvers/zod' | ||
import { cx } from '@saas-ui/core/utils' | ||
import { | ||
type DefaultValues, | ||
type FieldValues, | ||
UseFormProps as UseHookFormProps, | ||
type UseFormReturn as UseHookFormReturn, | ||
useForm as useHookForm, | ||
} from 'react-hook-form' | ||
import type { z } from 'zod' | ||
|
||
import { ArrayField, type ArrayFieldProps } from './array-field' | ||
import { DisplayIf, type DisplayIfProps } from './display-if' | ||
import { Field } from './field.tsx' | ||
import { FormProvider } from './form-context' | ||
import { ObjectField, type ObjectFieldProps } from './object-field' | ||
import type { FieldProps } from './types' | ||
import type { UseArrayFieldReturn } from './use-array-field' | ||
|
||
export interface UseFormProps< | ||
TFieldValues extends FieldValues, | ||
TContext extends object, | ||
> extends UseHookFormProps<TFieldValues, TContext> {} | ||
|
||
export interface UseFormReturn< | ||
TFieldValues extends FieldValues, | ||
TContext extends object, | ||
> extends UseHookFormReturn<TFieldValues, TContext> { | ||
Form: React.FC<Omit<FormProps<TFieldValues, TContext>, 'form'>> | ||
Field: React.FC<FieldProps<TFieldValues>> | ||
DisplayIf: React.FC<DisplayIfProps<TFieldValues>> | ||
ArrayField: React.FC< | ||
ArrayFieldProps<TFieldValues> & React.RefAttributes<UseArrayFieldReturn> | ||
> | ||
ObjectField: React.FC<ObjectFieldProps<TFieldValues>> | ||
} | ||
|
||
export function useForm< | ||
TFieldValues extends FieldValues, | ||
TContext extends object, | ||
>(props: UseFormProps<TFieldValues, TContext> = {}) { | ||
const form = useHookForm<TFieldValues, TContext>(props) | ||
|
||
const FormComponent = forwardRef<HTMLFormElement, Omit<FormProps, 'form'>>( | ||
function FormComponent(props, ref) { | ||
return <Form {...props} form={form} ref={ref} /> | ||
}, | ||
) | ||
|
||
return { | ||
...form, | ||
Form: FormComponent, | ||
Field, | ||
} | ||
DisplayIf, | ||
ArrayField, | ||
ObjectField, | ||
} as UseFormReturn<TFieldValues, TContext> | ||
} | ||
|
||
export interface FormProps< | ||
TFieldValues extends FieldValues = FieldValues, | ||
TContext extends object = object, | ||
> extends HTMLChakraProps<'form'> { | ||
children: React.ReactNode | ||
form: ReturnType<typeof useHookForm<TFieldValues, TContext>> | ||
onSubmit: (data: any) => void | ||
onError?: (errors: any) => void | ||
} | ||
|
||
export const Form = forwardRef<HTMLFormElement, FormProps>( | ||
function Form(props, ref) { | ||
const { children, form, onSubmit, onError, ...rest } = props | ||
return ( | ||
<FormProvider {...form}> | ||
<chakra.form | ||
ref={ref} | ||
onSubmit={form.handleSubmit(onSubmit, onError)} | ||
{...rest} | ||
className={cx('sui-form', props.className)} | ||
> | ||
{props.children} | ||
</chakra.form> | ||
</FormProvider> | ||
) | ||
}, | ||
) as <TFieldValues extends FieldValues, TContext extends object>( | ||
props: FormProps<TFieldValues, TContext> & { | ||
ref?: React.Ref<HTMLFormElement> | ||
}, | ||
) => React.ReactElement | ||
|
||
export interface UseZodFormProps< | ||
TSchema extends | ||
| z.AnyZodObject | ||
| z.ZodEffects<z.AnyZodObject> = z.AnyZodObject, | ||
TFieldValues extends InferObjectSchema<TSchema> = InferObjectSchema<TSchema>, | ||
TContext extends object = object, | ||
> extends Omit<UseHookFormProps<TFieldValues, TContext>, 'defaultValues'> { | ||
schema: TSchema | ||
defaultValues?: | ||
| DefaultValues<InferObjectSchema<TSchema>> | ||
| AsyncDefaultValues<InferObjectSchema<TSchema>> | ||
} | ||
|
||
export function useZodForm< | ||
TSchema extends | ||
| z.AnyZodObject | ||
| z.ZodEffects<z.AnyZodObject> = z.AnyZodObject, | ||
TFieldValues extends InferObjectSchema<TSchema> = InferObjectSchema<TSchema>, | ||
TContext extends object = object, | ||
>(props: UseZodFormProps<TSchema, TFieldValues, TContext>) { | ||
const { schema, ...rest } = props | ||
|
||
return useForm({ | ||
resolver: zodResolver(schema) as any, | ||
...rest, | ||
}) | ||
} | ||
|
||
type InferObjectSchema<T extends z.ZodTypeAny | z.ZodEffects<z.ZodTypeAny>> = | ||
T extends z.ZodEffects<infer TSchema> ? z.infer<TSchema> : z.infer<T> | ||
|
||
type AsyncDefaultValues<TFieldValues> = ( | ||
payload?: unknown, | ||
) => Promise<TFieldValues> |
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 |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import React from 'react' | ||
|
||
import { z } from 'zod' | ||
|
||
import { FormLayout } from '../src/form-layout' | ||
import { SubmitButton } from '../src/submit-button' | ||
import { useForm, useZodForm } from '../src/use-form' | ||
|
||
export default { | ||
title: 'Forms/useForm', | ||
} | ||
|
||
const onSubmit = (data: any) => { | ||
return new Promise((resolve) => { | ||
setTimeout(() => { | ||
resolve(data) | ||
}, 500) | ||
}) | ||
} | ||
|
||
export const Basic = () => { | ||
const form = useForm({ | ||
defaultValues: { | ||
name: '', | ||
}, | ||
}) | ||
|
||
return ( | ||
<form.Form onSubmit={onSubmit}> | ||
<FormLayout> | ||
<form.Field type="text" name="name" label="Name" /> | ||
|
||
<SubmitButton /> | ||
</FormLayout> | ||
</form.Form> | ||
) | ||
} | ||
|
||
export const ConditionalFields = () => { | ||
const form = useForm({ | ||
defaultValues: { | ||
name: '', | ||
description: '', | ||
}, | ||
}) | ||
|
||
return ( | ||
<form.Form onSubmit={onSubmit}> | ||
<FormLayout> | ||
<form.Field type="text" name="name" label="Name" /> | ||
|
||
<form.DisplayIf name="name" condition={(value) => !!value}> | ||
<form.Field type="text" name="description" label="Description" /> | ||
</form.DisplayIf> | ||
|
||
<SubmitButton /> | ||
</FormLayout> | ||
</form.Form> | ||
) | ||
} | ||
|
||
export const ZodForm = () => { | ||
const form = useZodForm({ | ||
defaultValues: { | ||
name: '', | ||
}, | ||
schema: z.object({ | ||
name: z.string().min(1, { message: 'Name is required' }), | ||
}), | ||
}) | ||
|
||
return ( | ||
<form.Form onSubmit={onSubmit}> | ||
<FormLayout> | ||
<form.Field type="text" name="name" label="Name" /> | ||
|
||
<SubmitButton /> | ||
</FormLayout> | ||
</form.Form> | ||
) | ||
} |
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
{ | ||
"extends": "../../tsconfig.base.json", | ||
"compilerOptions": { | ||
"baseUrl": "." | ||
"baseUrl": ".", | ||
"paths": { | ||
"compositions/*": ["../../apps/compositions/src/*"] | ||
} | ||
}, | ||
"include": [".storybook/*.ts"] | ||
} |
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
Oops, something went wrong.