|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { zodResolver } from '@hookform/resolvers/zod'; |
| 4 | +import { Box, Button, FormHelperText } from '@mui/material'; |
| 5 | +import React from 'react'; |
| 6 | +import { Controller, SubmitHandler, useForm } from 'react-hook-form'; |
| 7 | +import { z } from 'zod'; |
| 8 | + |
| 9 | +import { consoleLog } from '@/util/shared/console-log'; |
| 10 | +import { getApiResponse } from '@/util/shared/get-api-response'; |
| 11 | + |
| 12 | +const zodSchema = z.object({ |
| 13 | + name: z.string().min(5).nonempty({ message: 'Name is required' }), |
| 14 | + email: z.string().min(10).email({ message: 'Invalid email address' }), |
| 15 | +}); |
| 16 | + |
| 17 | +type FormValues = z.infer<typeof zodSchema>; |
| 18 | + |
| 19 | +const ReactHookForm: React.FC = () => { |
| 20 | + const apiEndpoint = '/api/test'; |
| 21 | + const [apiResult, setApiResult] = React.useState<FormValues>(); |
| 22 | + |
| 23 | + const { |
| 24 | + handleSubmit, |
| 25 | + control, |
| 26 | + formState: { errors }, |
| 27 | + } = useForm<FormValues>({ |
| 28 | + resolver: zodResolver(zodSchema), |
| 29 | + }); |
| 30 | + |
| 31 | + const onSubmit: SubmitHandler<FormValues> = async (data) => { |
| 32 | + try { |
| 33 | + const result = await getApiResponse<{ |
| 34 | + reqData: FormValues; |
| 35 | + }>({ |
| 36 | + apiEndpoint, |
| 37 | + method: 'POST', |
| 38 | + requestData: JSON.stringify(data), |
| 39 | + }); |
| 40 | + setApiResult(result?.reqData); |
| 41 | + consoleLog('getApiResponse result', result, errors); |
| 42 | + } catch (error) { |
| 43 | + consoleLog('handleSubmit ERROR', error); |
| 44 | + } |
| 45 | + }; |
| 46 | + |
| 47 | + return ( |
| 48 | + <form onSubmit={handleSubmit(onSubmit)}> |
| 49 | + <Box sx={{ m: 2 }}> |
| 50 | + <label>Name:</label> |
| 51 | + <Controller |
| 52 | + name='name' |
| 53 | + control={control} |
| 54 | + defaultValue='' |
| 55 | + render={({ field }) => <input {...field} />} |
| 56 | + /> |
| 57 | + {errors.name && ( |
| 58 | + <FormHelperText sx={{ textAlign: 'center' }}> |
| 59 | + {errors.name.message} |
| 60 | + </FormHelperText> |
| 61 | + )} |
| 62 | + </Box> |
| 63 | + |
| 64 | + <Box sx={{ m: 2 }}> |
| 65 | + <label>Email:</label> |
| 66 | + <Controller |
| 67 | + name='email' |
| 68 | + control={control} |
| 69 | + defaultValue='' |
| 70 | + render={({ field }) => <input {...field} />} |
| 71 | + /> |
| 72 | + {errors.email && ( |
| 73 | + <FormHelperText sx={{ textAlign: 'center' }}> |
| 74 | + {errors.email.message} |
| 75 | + </FormHelperText> |
| 76 | + )} |
| 77 | + </Box> |
| 78 | + {apiResult && ( |
| 79 | + <Box sx={{ m: 2, color: 'green' }}> |
| 80 | + API result from {apiEndpoint}: {apiResult.name} & {apiResult.email} |
| 81 | + </Box> |
| 82 | + )} |
| 83 | + <Button variant='contained' type='submit'> |
| 84 | + Test react hook form with zod |
| 85 | + </Button> |
| 86 | + </form> |
| 87 | + ); |
| 88 | +}; |
| 89 | + |
| 90 | +export default ReactHookForm; |
0 commit comments