-
-
Notifications
You must be signed in to change notification settings - Fork 0
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
10c19a5
commit 00538bd
Showing
10 changed files
with
253 additions
and
67 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
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 |
---|---|---|
@@ -0,0 +1,94 @@ | ||
"use client" | ||
|
||
import { | ||
ComponentPropsWithoutRef, | ||
ReactNode, | ||
RefObject, | ||
useImperativeHandle, | ||
useRef, | ||
} from "react" | ||
import { | ||
FieldValues, | ||
FormProvider as FormPrimitiveProvider, | ||
UseFormProps, | ||
UseFormReturn, | ||
useForm, | ||
} from "react-hook-form" | ||
|
||
export interface FormProps< | ||
TFieldValues extends FieldValues = FieldValues, | ||
TContext = any, | ||
TTransformedValues extends FieldValues | undefined = undefined, | ||
> extends Omit<ComponentPropsWithoutRef<"form">, "onSubmit">, | ||
UseFormProps<TFieldValues, TContext> { | ||
ref?: RefObject<HTMLFormElement> | ||
formRef?: RefObject<UseFormReturn<TFieldValues, TContext, TTransformedValues>> | ||
children: ReactNode | ||
onSubmit?: (data: TFieldValues) => void | ||
} | ||
|
||
export const Form = < | ||
TFieldValues extends FieldValues = FieldValues, | ||
TContext = any, | ||
TTransformedValues extends FieldValues | undefined = undefined, | ||
>({ | ||
children, | ||
mode, | ||
disabled, | ||
reValidateMode, | ||
defaultValues, | ||
values, | ||
errors, | ||
resetOptions, | ||
context, | ||
shouldFocusError, | ||
shouldUnregister, | ||
shouldUseNativeValidation, | ||
progressive, | ||
criteriaMode, | ||
delayError, | ||
formRef, | ||
ref, | ||
resolver, | ||
onSubmit, | ||
...props | ||
}: FormProps<TFieldValues, TContext, TTransformedValues>) => { | ||
const methods = useForm<TFieldValues, TContext, TTransformedValues>({ | ||
resolver, | ||
mode, | ||
disabled, | ||
reValidateMode, | ||
defaultValues, | ||
values, | ||
errors, | ||
resetOptions, | ||
context, | ||
shouldFocusError, | ||
shouldUnregister, | ||
shouldUseNativeValidation, | ||
progressive, | ||
criteriaMode, | ||
delayError, | ||
}) | ||
|
||
const innerFormElementRef = useRef<HTMLFormElement>(null) | ||
|
||
useImperativeHandle(formRef, () => methods as any, [methods]) | ||
useImperativeHandle(ref, () => innerFormElementRef.current as any, [ | ||
innerFormElementRef.current, | ||
]) | ||
|
||
return ( | ||
<FormPrimitiveProvider {...methods}> | ||
<form | ||
{...props} | ||
onSubmit={onSubmit ? methods.handleSubmit(onSubmit as any) : undefined} | ||
ref={innerFormElementRef} | ||
> | ||
{children} | ||
</form> | ||
</FormPrimitiveProvider> | ||
) | ||
} | ||
|
||
Form.displayName = "Form" |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
||
exports[`renders correctly 1`] = ` | ||
<form> | ||
<div | ||
className="field-item" | ||
> | ||
<label | ||
className="field-label" | ||
data-name="a" | ||
data-state="idle" | ||
htmlFor=":r0:-form-item" | ||
> | ||
Input | ||
</label> | ||
<input | ||
type="text" | ||
/> | ||
</div> | ||
<div | ||
className="field-item" | ||
> | ||
<input | ||
type="number" | ||
/> | ||
</div> | ||
<div | ||
className="field-item" | ||
> | ||
<label | ||
className="field-label" | ||
data-name="c" | ||
data-state="idle" | ||
htmlFor=":r2:-form-item" | ||
> | ||
File | ||
</label> | ||
<input | ||
type="file" | ||
/> | ||
</div> | ||
<div | ||
className="field-item" | ||
> | ||
<select> | ||
<option | ||
value="test" | ||
> | ||
Test | ||
</option> | ||
</select> | ||
</div> | ||
</form> | ||
`; |
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,62 @@ | ||
import React from "react" | ||
import renderer from "react-test-renderer" | ||
import { expect, it } from "vitest" | ||
import { FormLabel } from "../src/form-label" | ||
import { Form, FormField, createField } from "../src" | ||
|
||
interface InputProps {} | ||
|
||
interface NumberProps {} | ||
|
||
interface FileProps {} | ||
|
||
interface SelectProps { | ||
options: any[] | ||
} | ||
|
||
const Field = createField({ | ||
text: (props: InputProps) => <input type="text" />, | ||
number: (props: NumberProps) => <input type="number" />, | ||
file: (props: FileProps) => <input type="file" />, | ||
select: (props: SelectProps) => ( | ||
<select> | ||
<option value="test">Test</option> | ||
</select> | ||
), | ||
}) | ||
|
||
const MyForm = () => { | ||
return ( | ||
<Form> | ||
<Field label="Input" component="text" name="a" /> | ||
<Field component="number" name="b" /> | ||
<Field label="File" component="file" name="c" /> | ||
<Field component="select" name="d" options={[]} /> | ||
</Form> | ||
) | ||
} | ||
|
||
it("renders correctly", () => { | ||
const form = renderer.create(<MyForm />) | ||
const formInstance = form.root | ||
|
||
expect(formInstance).toBeDefined() | ||
|
||
expect( | ||
formInstance.findByProps({ component: "text", name: "a" }) | ||
).toBeDefined() | ||
|
||
expect( | ||
formInstance.findByProps({ component: "number", name: "b" }) | ||
).toBeDefined() | ||
|
||
expect( | ||
formInstance.findByProps({ component: "file", name: "c" }) | ||
).toBeDefined() | ||
|
||
expect( | ||
formInstance.findByProps({ component: "select", name: "d" }) | ||
).toBeDefined() | ||
|
||
expect(form.toJSON()).toMatchSnapshot() | ||
}) |