|
| 1 | +import { zodResolver } from '@hookform/resolvers/zod'; |
| 2 | +import { uploadFileSchema } from '~/utils/schemas'; |
| 3 | +import { trpc } from '~/utils/trpc'; |
| 4 | +import { useRef, useState } from 'react'; |
| 5 | +import type { UseFormProps } from 'react-hook-form'; |
| 6 | +import { FormProvider, useForm } from 'react-hook-form'; |
| 7 | +import type { z } from 'zod'; |
| 8 | + |
| 9 | +/** |
| 10 | + * zod-form-data wraps zod in an effect where the original type is a `FormData` |
| 11 | + */ |
| 12 | +type UnwrapZodEffect<TType extends z.ZodType> = TType extends z.ZodEffects< |
| 13 | + infer U, |
| 14 | + any, |
| 15 | + any |
| 16 | +> |
| 17 | + ? U |
| 18 | + : TType; |
| 19 | + |
| 20 | +type GetInput<TType extends z.ZodType> = UnwrapZodEffect<TType>['_input']; |
| 21 | + |
| 22 | +function useZodFormData<TSchema extends z.ZodType>( |
| 23 | + props: Omit<UseFormProps<GetInput<TSchema>>, 'resolver'> & { |
| 24 | + schema: TSchema; |
| 25 | + }, |
| 26 | +) { |
| 27 | + const formRef = useRef<HTMLFormElement>(null); |
| 28 | + const _resolver = zodResolver(props.schema, undefined, { |
| 29 | + rawValues: true, |
| 30 | + }); |
| 31 | + |
| 32 | + const form = useForm<GetInput<TSchema>>({ |
| 33 | + ...props, |
| 34 | + resolver: (_, ctx, opts) => { |
| 35 | + if (!formRef.current) { |
| 36 | + return { |
| 37 | + values: {}, |
| 38 | + errors: { |
| 39 | + root: { |
| 40 | + message: 'Form not mounted', |
| 41 | + }, |
| 42 | + }, |
| 43 | + }; |
| 44 | + } |
| 45 | + const values = new FormData(formRef.current); |
| 46 | + return _resolver(values, ctx, opts); |
| 47 | + }, |
| 48 | + }); |
| 49 | + |
| 50 | + return { ...form, formRef }; |
| 51 | +} |
| 52 | + |
| 53 | +export default function Page() { |
| 54 | + const mutation = trpc.room.sendMessage.useMutation({ |
| 55 | + onError(err) { |
| 56 | + alert('Error from server: ' + err.message); |
| 57 | + }, |
| 58 | + }); |
| 59 | + |
| 60 | + const form = useZodFormData({ |
| 61 | + schema: uploadFileSchema, |
| 62 | + defaultValues: { |
| 63 | + name: 'whadaaaap', |
| 64 | + }, |
| 65 | + }); |
| 66 | + |
| 67 | + const [noJs, setNoJs] = useState(false); |
| 68 | + |
| 69 | + return ( |
| 70 | + <> |
| 71 | + <h2 className="text-3xl font-bold">Posts</h2> |
| 72 | + |
| 73 | + <FormProvider {...form}> |
| 74 | + <form |
| 75 | + method="post" |
| 76 | + action={`/api/trpc/${mutation.trpc.path}`} |
| 77 | + encType="multipart/form-data" |
| 78 | + onSubmit={(_event) => { |
| 79 | + if (noJs) { |
| 80 | + return; |
| 81 | + } |
| 82 | + void form.handleSubmit(async (values, event) => { |
| 83 | + await mutation.mutateAsync(new FormData(event?.target)); |
| 84 | + })(_event); |
| 85 | + }} |
| 86 | + style={{ display: 'flex', flexDirection: 'column', gap: 10 }} |
| 87 | + ref={form.formRef} |
| 88 | + > |
| 89 | + <fieldset> |
| 90 | + <legend>Form with file upload</legend> |
| 91 | + <div style={{}}> |
| 92 | + <label htmlFor="name">Enter your name</label> |
| 93 | + <input {...form.register('name')} /> |
| 94 | + {form.formState.errors.name && ( |
| 95 | + <div>{form.formState.errors.name.message}</div> |
| 96 | + )} |
| 97 | + </div> |
| 98 | + |
| 99 | + <div> |
| 100 | + <label>Required file, only images</label> |
| 101 | + <input type="file" {...form.register('image')} /> |
| 102 | + {form.formState.errors.image && ( |
| 103 | + <div>{form.formState.errors.image.message}</div> |
| 104 | + )} |
| 105 | + </div> |
| 106 | + |
| 107 | + <div> |
| 108 | + <label>Post without JS</label> |
| 109 | + <input |
| 110 | + type="checkbox" |
| 111 | + checked={noJs} |
| 112 | + onChange={(e) => { |
| 113 | + setNoJs(e.target.checked); |
| 114 | + }} |
| 115 | + /> |
| 116 | + </div> |
| 117 | + <div> |
| 118 | + <button type="submit" disabled={mutation.status === 'pending'}> |
| 119 | + submit |
| 120 | + </button> |
| 121 | + </div> |
| 122 | + </fieldset> |
| 123 | + </form> |
| 124 | + |
| 125 | + {mutation.data && ( |
| 126 | + <fieldset> |
| 127 | + <legend>Upload result</legend> |
| 128 | + <ul> |
| 129 | + <li> |
| 130 | + Image: <br /> |
| 131 | + <img |
| 132 | + src={mutation.data.image.url} |
| 133 | + alt={mutation.data.image.url} |
| 134 | + /> |
| 135 | + </li> |
| 136 | + </ul> |
| 137 | + </fieldset> |
| 138 | + )} |
| 139 | + </FormProvider> |
| 140 | + </> |
| 141 | + ); |
| 142 | +} |
0 commit comments