diff --git a/frontend/src/components/Form.tsx b/frontend/src/components/Form.tsx index 90350d9a..57dbb362 100644 --- a/frontend/src/components/Form.tsx +++ b/frontend/src/components/Form.tsx @@ -1,6 +1,6 @@ import React, { useEffect } from "react"; import Grid from "@mui/joy/Grid"; -import { useForm } from "react-hook-form"; +import { useForm, FormProvider } from "react-hook-form"; import axios from "axios"; import { yupResolver } from "@hookform/resolvers/yup"; import Button from "@mui/joy/Button"; @@ -65,17 +65,20 @@ export const Form: React.FunctionComponent = ({ setDisableDownloadButton, } = useFromStore(); - //TODO - we need to make it global (zuatand) + //TODO - we need to make it global + + const methods = useForm({ + defaultValues: { species: [{ molecule: "CO", mole_fraction: 0.1 }] }, + resolver: yupResolver(formSchema), + }); + const { control, handleSubmit, setValue, watch, formState: { dirtyFields }, - } = useForm({ - defaultValues: { species: [{ molecule: "CO", mole_fraction: 0.1 }] }, - resolver: yupResolver(formSchema), - }); + } = methods; const databaseWatch = watch("database"); React.useEffect(() => { @@ -283,131 +286,133 @@ export const Form: React.FunctionComponent = ({ }, [setValue, isNonEquilibrium]); return ( -
onSubmit(data, `calculate-spectrum`))} - > - - - - - - - - - - - - {isNonEquilibrium ? ( - - + + onSubmit(data, `calculate-spectrum`))} + > + + + - ) : ( - - + + + + + - )} - {isNonEquilibrium ? ( - <> + {isNonEquilibrium ? ( - + - - + ) : ( + + - - ) : null} + )} - {isNonEquilibrium ? ( - - - - ) : ( - - - - )} + {isNonEquilibrium ? ( + <> + + + + + + + + ) : null} - {isNonEquilibrium ? ( - <> + {isNonEquilibrium ? ( - + - - ) : ( - <> + ) : ( - + - - )} + )} - - - + {isNonEquilibrium ? ( + <> + + + + + ) : ( + <> + + + + + )} - {useSimulateSlitFunction ? ( - + - ) : null} - {useSimulateSlitFunction ? ( - useSlit ? ( + {useSimulateSlitFunction ? ( + + + + ) : null} + + {useSimulateSlitFunction ? ( + useSlit ? ( + + + + ) : null + ) : null} + {showNonEquilibriumSwitch && ( - + - ) : null - ) : null} - {showNonEquilibriumSwitch && ( + )} + + + + + + - + { + onSubmit(data, `download-spectrum`); + })} + /> + + + { + onSubmit(data, `download-txt`); + })} + /> - )} - - - - - - - - { - onSubmit(data, `download-spectrum`); - })} - /> - - - { - onSubmit(data, `download-txt`); - })} - /> - - + + ); }; diff --git a/frontend/src/components/fields/TGas.tsx b/frontend/src/components/fields/TGas.tsx index f2c1ee7d..6e992d13 100644 --- a/frontend/src/components/fields/TGas.tsx +++ b/frontend/src/components/fields/TGas.tsx @@ -2,47 +2,46 @@ import Input from "@mui/joy/Input"; import FormControl from "@mui/joy/FormControl"; import FormLabel from "@mui/joy/FormLabel"; import FormHelperText from "@mui/joy/FormHelperText"; -import React, { ReactNode } from "react"; import InputAdornment from "@mui/material/InputAdornment"; -import { Control, Controller } from "react-hook-form"; -import { FormValues } from "../types"; +import { Controller, useFormContext } from "react-hook-form"; -interface TGasProps { - control: Control; -} -export const TGas: React.FC = ({ control }) => ( - ( - - TGas - K} - onKeyPress={(event: any) => { - if (event?.key === "-" || event?.key === "+") { - event.preventDefault(); - } - }} - /> - {formState.errors?.tgas ? ( - { + const { control } = useFormContext(); + return ( + ( + + TGas + K} + onKeyPress={(event: any) => { + if (event?.key === "-" || event?.key === "+") { + event.preventDefault(); + } }} - > - {formState.errors?.tgas?.message} - - ) : null} - - )} - name="tgas" - control={control} - defaultValue={300} - /> -); + /> + {fieldState.error ? ( + + {fieldState.error.message} + + ) : null} + + )} + name="tgas" + control={control} + defaultValue={300} + /> + ); +};