From 064c11060f7a8e1ec5e1a128a7beabd7635991ca Mon Sep 17 00:00:00 2001 From: Michael Hunteman Date: Sun, 25 Feb 2024 13:46:41 -0600 Subject: Use react-hook-form --- src/components/RsvpForm.tsx | 120 ++++++++++++++++++++++++++++---------------- 1 file changed, 77 insertions(+), 43 deletions(-) (limited to 'src/components/RsvpForm.tsx') diff --git a/src/components/RsvpForm.tsx b/src/components/RsvpForm.tsx index fe6d874..5e0fb31 100644 --- a/src/components/RsvpForm.tsx +++ b/src/components/RsvpForm.tsx @@ -6,12 +6,15 @@ import { FormControlLabel, FormLabel, Grid, - Paper, + MenuItem, + Select, Radio, RadioGroup, TextField, Typography } from '@mui/material'; +import { useForm, Controller } from 'react-hook-form'; +import { DevTool } from '@hookform/devtools'; import { useGetGuestsQuery, useUpdateGuestMutation } from '../apiSlice'; @@ -26,18 +29,24 @@ function RsvpForm() { const [updateGuest] = useUpdateGuestMutation(); - const handleSubmit = (e) => { - e.preventDefault(); - let guest = guests[0]; - if (guest.attendance === 'true') { - updateGuest({...guest, attendance: 'false'}); - } else { - updateGuest({...guest, attendance: 'true'}); + const { register, handleSubmit, control, + formState: { isDirty, isValid, errors } } = useForm({ + defaultValues: { + attendance: '', + mealPreference: '', + dietaryRestrictions: '', + plusOne: '', + marriageAdvice: '' } + }); + + const onSubmit = async (data) => { + console.log(data); + updateGuest({...data}); }; return ( - +
Date: April 14, 2025 @@ -48,57 +57,82 @@ function RsvpForm() { RSVP Deadline: - - - + { errors && {errors.attendance?.message} } Will you attend our wedding? - - } label="Yes" /> - } label="No" /> - + ( + + } + label="Yes" + /> + } + label="No" + /> + + )} + /> - - - + { errors && {errors.mealPreference?.message} } Meal Preference - } - label="Beef" - /> - } - label="Chicken" - /> - } - label="Fish" - /> - } - label="Vegetarian" - /> + ( + + } + label="Beef" + /> + } + label="Chicken" + /> + + )} + /> - + + + + - + - + - + ); } -- cgit v1.2.3