-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from Code-Forge-Net/32-formstateissubmitsucces…
…sful-does-not-behave-like-react-hook-form version 3.0.0
- Loading branch information
Showing
9 changed files
with
214 additions
and
113 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 |
---|---|---|
@@ -1,19 +1,55 @@ | ||
import { zodResolver } from "@hookform/resolvers/zod"; | ||
import { json, type ActionFunctionArgs } from "@remix-run/node"; | ||
import { useFetcher } from "@remix-run/react"; | ||
import { getValidatedFormData, useRemixForm } from "remix-hook-form"; | ||
import { | ||
json, | ||
type ActionFunctionArgs, | ||
unstable_parseMultipartFormData, | ||
LoaderFunctionArgs, | ||
} from "@remix-run/node"; | ||
import { Form, useFetcher } from "@remix-run/react"; | ||
import { | ||
getValidatedFormData, | ||
useRemixForm, | ||
parseFormData, | ||
getFormDataFromSearchParams, | ||
} from "remix-hook-form"; | ||
import { z } from "zod"; | ||
|
||
const MAX_FILE_SIZE = 500000; | ||
const ACCEPTED_IMAGE_TYPES = [ | ||
"image/jpeg", | ||
"image/jpg", | ||
"image/png", | ||
"image/webp", | ||
]; | ||
const schema = z.object({ | ||
content: z.string().nonempty("content is required"), | ||
array: z.array(z.string()).nonempty(), | ||
user: z.object({ | ||
name: z.string().nonempty(), | ||
email: z.string().email(), | ||
}), | ||
boolean: z.boolean(), | ||
number: z.number().min(5), | ||
|
||
// file: z | ||
// .any() | ||
// .refine((files) => files?.length == 1, "Image is required.") | ||
// .refine( | ||
// (files) => files?.[0]?.size <= MAX_FILE_SIZE, | ||
// `Max file size is 5MB.`, | ||
// ) | ||
// .refine( | ||
// (files) => ACCEPTED_IMAGE_TYPES.includes(files?.[0]?.type), | ||
// ".jpg, .jpeg, .png and .webp files are accepted.", | ||
// ) | ||
// .nullish(), | ||
}); | ||
|
||
type FormData = z.infer<typeof schema>; | ||
|
||
const resolver = zodResolver(schema); | ||
|
||
export const action = async ({ request }: ActionFunctionArgs) => { | ||
const { data } = await getValidatedFormData<FormData>(request, resolver); | ||
const data = await getValidatedFormData<FormData>(request, resolver); | ||
console.log("action", data); | ||
await new Promise((resolve) => setTimeout(resolve, 2000)); | ||
return null; | ||
// Make DB call or similar here. | ||
|
@@ -24,41 +60,50 @@ export const action = async ({ request }: ActionFunctionArgs) => { | |
}; | ||
}; | ||
|
||
export const loader = () => { | ||
export const loader = ({ request }: LoaderFunctionArgs) => { | ||
const data = getFormDataFromSearchParams(request); | ||
console.log("loader", data); | ||
return json({ result: "success" }); | ||
}; | ||
|
||
export default function Index() { | ||
const fetcher = useFetcher(); | ||
const { register, handleSubmit, formState } = useRemixForm<FormData>({ | ||
resolver, | ||
fetcher, | ||
submitConfig: { | ||
method: "GET", | ||
}, | ||
}); | ||
const { register, handleSubmit, formState, watch, reset } = | ||
useRemixForm<FormData>({ | ||
resolver, | ||
defaultValues: { | ||
array: ["a", "b"], | ||
boolean: true, | ||
number: 6, | ||
user: { | ||
name: "John Doe", | ||
email: "[email protected]", | ||
}, | ||
}, | ||
submitConfig: { | ||
method: "POST", | ||
}, | ||
}); | ||
|
||
return ( | ||
<div> | ||
<p>Add a thing...</p> | ||
<p>Current Errors: {JSON.stringify(formState.errors)}</p> | ||
<fetcher.Form method="post" onSubmit={handleSubmit}> | ||
<div> | ||
<label> | ||
Content:{" "} | ||
<input | ||
type="text" | ||
{...register("content", { disableProgressiveEnhancement: true })} | ||
/> | ||
Error: {formState.errors.content?.message} | ||
</label> | ||
|
||
<Form method="post" onSubmit={handleSubmit}> | ||
<div className="flex flex-col gap-2"> | ||
<input type="number" {...register("number")} /> | ||
<input type="boolean" {...register("boolean")} /> | ||
|
||
<input type="string" {...register("user.name")} /> | ||
<input type="string" {...register("user.email")} /> | ||
<input type="string" {...register("array.0")} /> | ||
<input type="string" {...register("array.1")} /> | ||
</div> | ||
<div> | ||
<button type="submit" className="button"> | ||
Add | ||
</button> | ||
</div> | ||
</fetcher.Form> | ||
</Form> | ||
</div> | ||
); | ||
} |
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.