diff --git a/frontend/src/components/Checkbox.tsx b/frontend/src/components/Checkbox.tsx index b4905a32..47663286 100644 --- a/frontend/src/components/Checkbox.tsx +++ b/frontend/src/components/Checkbox.tsx @@ -1,28 +1,27 @@ "use client"; -import { UseFormRegister } from "react-hook-form"; +import { FieldValues, Path, UseFormRegister } from "react-hook-form"; import { cn } from "../lib/utils"; import OtherCheckbox from "./OtherCheckbox"; -import { FormData } from "./StudentForm/types"; -type CheckboxProps = { +type CheckboxProps = { options: string[]; className?: string; - name: keyof FormData; + name: Path; defaultValue?: string[]; defaultOtherValue?: string; - register: UseFormRegister; + register: UseFormRegister; }; -export function Checkbox({ +export function Checkbox({ options, className, name, register, defaultValue, defaultOtherValue, -}: CheckboxProps) { +}: CheckboxProps) { return (
{options.map((item, index) => { diff --git a/frontend/src/components/OtherCheckbox.tsx b/frontend/src/components/OtherCheckbox.tsx index 84533275..89cf7163 100644 --- a/frontend/src/components/OtherCheckbox.tsx +++ b/frontend/src/components/OtherCheckbox.tsx @@ -1,15 +1,17 @@ import { useEffect, useState } from "react"; -import { UseFormRegister } from "react-hook-form"; +import { FieldValues, Path, UseFormRegister } from "react-hook-form"; -import { FormData } from "./StudentForm/types"; import { Textfield } from "./Textfield"; -type OtherCheckboxProps = { - register: UseFormRegister; +type OtherCheckboxProps = { + register: UseFormRegister; defaultOtherValue?: string; }; -export default function OtherCheckbox({ register, defaultOtherValue = "" }: OtherCheckboxProps) { +export default function OtherCheckbox({ + register, + defaultOtherValue = "", +}: OtherCheckboxProps) { const [checked, setChecked] = useState(defaultOtherValue !== ""); //Revert other checkbox when clicked outside @@ -32,7 +34,7 @@ export default function OtherCheckbox({ register, defaultOtherValue = "" }: Othe } label="Other" placeholder="Type Here..." defaultValue={defaultOtherValue} diff --git a/frontend/src/components/Radio.tsx b/frontend/src/components/Radio.tsx index bb4a8f4a..c7fffc89 100644 --- a/frontend/src/components/Radio.tsx +++ b/frontend/src/components/Radio.tsx @@ -1,15 +1,20 @@ -import { FieldValues, UseFormRegister } from "react-hook-form"; +import { FieldValues, Path, UseFormRegister } from "react-hook-form"; import { cn } from "../lib/utils"; -type RadioProps = { +type RadioProps = { options: string[]; className?: string; - name: string; - register: UseFormRegister; + name: Path; + register: UseFormRegister; }; -export default function Radio({ options, register, name, className }: RadioProps) { +export default function Radio({ + options, + register, + name, + className, +}: RadioProps) { return (
{options.map((option, index) => { diff --git a/frontend/src/components/StudentForm/ContactInfo.tsx b/frontend/src/components/StudentForm/ContactInfo.tsx index e24dbe2d..30defb6d 100644 --- a/frontend/src/components/StudentForm/ContactInfo.tsx +++ b/frontend/src/components/StudentForm/ContactInfo.tsx @@ -3,17 +3,17 @@ import { UseFormRegister } from "react-hook-form"; import { cn } from "../../lib/utils"; import { Textfield } from "../Textfield"; -import { FormData, StudentFormData } from "./types"; +import { StudentData, StudentFormData } from "./types"; type ContactRole = "student" | "emergency" | "serviceCoordinator"; type PersonalInfoField = "firstName" | "lastName" | "email" | "phoneNumber"; type ContactInfoProps = { - register: UseFormRegister; + register: UseFormRegister; classname?: string; type: "add" | "edit"; - data: StudentFormData | null; + data: StudentData | null; }; type FieldProps = { @@ -25,7 +25,7 @@ type FieldProps = { }; type FinalFieldProps = { - name: keyof FormData; + name: keyof StudentFormData; label: string; placeholder: string; type?: string; diff --git a/frontend/src/components/StudentForm/StudentBackground.tsx b/frontend/src/components/StudentForm/StudentBackground.tsx index f937ba8e..59d1bcea 100644 --- a/frontend/src/components/StudentForm/StudentBackground.tsx +++ b/frontend/src/components/StudentForm/StudentBackground.tsx @@ -4,13 +4,13 @@ import { cn } from "../../lib/utils"; import { Checkbox } from "../Checkbox"; import { Textfield } from "../Textfield"; -import { FormData, StudentFormData } from "./types"; +import { StudentData, StudentFormData } from "./types"; type StudentBackgroundProps = { - register: UseFormRegister; + register: UseFormRegister; classname?: string; - setCalendarValue: UseFormSetValue; - data: StudentFormData | null; + setCalendarValue: UseFormSetValue; + data: StudentData | null; }; const dietaryList = ["Nuts", "Eggs", "Seafood", "Pollen", "Dairy", "Other"]; diff --git a/frontend/src/components/StudentForm/StudentInfo.tsx b/frontend/src/components/StudentForm/StudentInfo.tsx index 4a8dbbe2..bcec3220 100644 --- a/frontend/src/components/StudentForm/StudentInfo.tsx +++ b/frontend/src/components/StudentForm/StudentInfo.tsx @@ -4,13 +4,13 @@ import { cn } from "../../lib/utils"; import { Checkbox } from "../Checkbox"; import { Textfield } from "../Textfield"; -import { FormData, StudentFormData } from "./types"; +import { StudentData, StudentFormData } from "./types"; type StudentInfoProps = { - register: UseFormRegister; + register: UseFormRegister; classname?: string; - setCalendarValue: UseFormSetValue; - data: StudentFormData | null; + setCalendarValue: UseFormSetValue; + data: StudentData | null; }; const regularPrograms = ["Intro", "ENTR"]; diff --git a/frontend/src/components/StudentForm/types.ts b/frontend/src/components/StudentForm/types.ts index eda11fa7..ec93a05b 100644 --- a/frontend/src/components/StudentForm/types.ts +++ b/frontend/src/components/StudentForm/types.ts @@ -5,7 +5,7 @@ export type Contact = { phoneNumber: string; }; -export type StudentFormData = { +export type StudentData = { student: Contact; emergency: Contact; serviceCoordinator: Contact; @@ -20,7 +20,7 @@ export type StudentFormData = { otherString: string; }; -export type FormData = { +export type StudentFormData = { student_name: string; student_last: string; student_email: string; diff --git a/frontend/src/components/StudentFormButton.tsx b/frontend/src/components/StudentFormButton.tsx index ad671d73..bd19010b 100644 --- a/frontend/src/components/StudentFormButton.tsx +++ b/frontend/src/components/StudentFormButton.tsx @@ -7,7 +7,7 @@ import { Button } from "./Button"; import ContactInfo from "./StudentForm/ContactInfo"; import StudentBackground from "./StudentForm/StudentBackground"; import StudentInfo from "./StudentForm/StudentInfo"; -import { FormData, StudentFormData } from "./StudentForm/types"; +import { StudentData, StudentFormData } from "./StudentForm/types"; import { Dialog, DialogClose, DialogContent, DialogTrigger } from "./ui/dialog"; type BaseProps = { @@ -16,12 +16,12 @@ type BaseProps = { type EditProps = BaseProps & { type: "edit"; - data: StudentFormData | null; + data: StudentData | null; }; type AddProps = BaseProps & { type: "add"; - data?: StudentFormData | null; + data?: StudentData | null; }; type StudentFormProps = EditProps | AddProps; @@ -31,10 +31,10 @@ export default function StudentFormButton({ data = null, classname, }: StudentFormProps) { - const { register, setValue: setCalendarValue, reset, handleSubmit } = useForm(); + const { register, setValue: setCalendarValue, reset, handleSubmit } = useForm(); - const onSubmit: SubmitHandler = (formData: FormData) => { - const transformedData: StudentFormData = { + const onSubmit: SubmitHandler = (formData: StudentFormData) => { + const transformedData: StudentData = { student: { firstName: formData.student_name, lastName: formData.student_last, diff --git a/frontend/src/components/Textfield.tsx b/frontend/src/components/Textfield.tsx index cfbf2264..ac2584c6 100644 --- a/frontend/src/components/Textfield.tsx +++ b/frontend/src/components/Textfield.tsx @@ -1,17 +1,15 @@ "use client"; import { useEffect, useState } from "react"; -import { UseFormRegister, UseFormSetValue } from "react-hook-form"; +import { FieldValues, Path, PathValue, UseFormRegister, UseFormSetValue } from "react-hook-form"; import { Calendar } from "../components/ui/calendar"; import { Popover, PopoverContent, PopoverTrigger } from "../components/ui/popover"; import { cn } from "../lib/utils"; -import { FormData } from "./StudentForm/types"; - -type BaseProps = { - register: UseFormRegister; - name: keyof FormData; +type BaseProps = { + register: UseFormRegister; + name: Path; label?: string; type?: string; placeholder: string; @@ -19,29 +17,29 @@ type BaseProps = { className?: string; }; -type WithCalendarProps = BaseProps & { - calendar: true; // When calendar is true, setCalendarValue is required - setCalendarValue: UseFormSetValue; +type WithCalendarProps = BaseProps & { + calendar?: true; // When calendar is false or not provided, setCalendarValue is optional + setCalendarValue?: UseFormSetValue; }; -type WithoutCalendarProps = BaseProps & { +type WithoutCalendarProps = BaseProps & { calendar?: false; // When calendar is false or not provided, setCalendarValue is optional - setCalendarValue?: UseFormSetValue; + setCalendarValue?: UseFormSetValue; }; -type TextFieldProps = WithCalendarProps | WithoutCalendarProps; +type TextFieldProps = WithCalendarProps | WithoutCalendarProps; -export function Textfield({ +export function Textfield({ register, setCalendarValue, label, - name, + name, //Must be a key in form data type specified in useForm hook placeholder, calendar = false, className, type = "text", defaultValue = "", -}: TextFieldProps) { +}: TextFieldProps) { const [date, setDate] = useState(); useEffect(() => { @@ -51,7 +49,7 @@ export function Textfield({ month: "2-digit", day: "2-digit", }); - setCalendarValue(name, parsedDate); + setCalendarValue(name, parsedDate as PathValue>); } }, [date]); @@ -64,7 +62,7 @@ export function Textfield({ )} > )} className="focus-visible:out w-full appearance-none bg-inherit px-2 placeholder-pia_accent outline-none" id={label + placeholder} type={type} diff --git a/frontend/src/pages/index.tsx b/frontend/src/pages/index.tsx index 910d28bc..5416e497 100644 --- a/frontend/src/pages/index.tsx +++ b/frontend/src/pages/index.tsx @@ -1,11 +1,40 @@ +import { useForm } from "react-hook-form"; + +import { Button } from "../components/Button"; +import { Checkbox } from "../components/Checkbox"; import StudentFormButton from "../components/StudentFormButton"; +import { Textfield } from "../components/Textfield"; import sampleStudentData from "../sampleStudentData.json"; +type FruitData = { + fruits: string[]; + favoriteFruit: string; +}; + export default function Home() { + const { register, handleSubmit, reset } = useForm(); + + const onSubmit = (formData: FruitData) => { + console.log(formData); + reset(); + }; + return ( -
- - +
+
+ + +
+ + {/* Example */} +
+

Example

+
+ + +
); }