diff --git a/client/src/components/ProjectForm.js b/client/src/components/ProjectForm.js index dc0c5491..b24537c0 100644 --- a/client/src/components/ProjectForm.js +++ b/client/src/components/ProjectForm.js @@ -1,17 +1,11 @@ import React, { useState } from 'react'; import { Link, useHistory } from 'react-router-dom'; import { useForm } from 'react-hook-form'; -import ProjectApiService from '../api/ProjectApiService'; - -import { ReactComponent as EditIcon } from '../svg/Icon_Edit.svg'; -import { ReactComponent as PlusIcon } from '../svg/PlusIcon.svg'; import { Redirect } from 'react-router-dom'; import { Typography, Box, Divider, - TextField, - InputLabel, Button, Grid, Radio, @@ -20,7 +14,12 @@ import { RadioGroup, } from '@mui/material'; import { styled } from '@mui/material/styles'; + import useAuth from '../hooks/useAuth'; +import ProjectApiService from '../api/ProjectApiService'; +import { ReactComponent as EditIcon } from '../svg/Icon_Edit.svg'; +import { ReactComponent as PlusIcon } from '../svg/PlusIcon.svg'; +import ValidatedTextField from './parts/form/ValidatedTextField'; /** STYLES * -most TextField and InputLabel styles are controlled by the theme @@ -236,73 +235,16 @@ export default function ProjectForm({ })} > {arr.map((input) => ( - - - - - {input.label} - - - {input.name === 'location' && locationRadios} - - {/* Sets text field and data (if needed) based on the whether it is as add or edit form page. */} - {isEdit ? ( - /** - * Edit textfield. - * Includes - * - handleChange - to update the input fields based on users input. - * - value - formData that is passed from the DB to fill the input fields. - * */ - - ) : ( - // Add new project textfield. - - )} - + ))} diff --git a/client/src/components/parts/form/ValidatedTextField.js b/client/src/components/parts/form/ValidatedTextField.js new file mode 100644 index 00000000..b6fc6c5b --- /dev/null +++ b/client/src/components/parts/form/ValidatedTextField.js @@ -0,0 +1,70 @@ +import React from 'react'; +import { Box, Grid, InputLabel, TextField } from "@mui/material"; + +/** + * A validated text field component for forms. + * + * @component + * @param {Object} props - The component props. + * @param {Function} props.register - Function used for registering the input field with react-hook-form. + * @param {Object} props.errors - Object containing form validation errors. + * @param {boolean} props.isEdit - Boolean indicating if the form is in edit mode. + * @param {boolean} props.editMode - Boolean indicating if the component is in edit mode. + * @param {string} props.locationType - The type of location. + * @param {ReactNode} props.locationRadios - Radio Buttons for selecting location type. + * @param {Object} props.input - The input configuration for the text field. + * @returns {ReactElement} The rendered component. + */ +function ValidatedTextField({ + register, + errors, + isEdit, + editMode, + locationType, + locationRadios, + input, +}) { + const registerObj = { + ...register(input.name, { + required: `${input.name} is required`, + pattern: + input.name === 'location' + ? locationType === 'remote' + ? { + value: input.value, + message: input.errorMessage, + } + : { + value: input.addressValue, + message: input.addressError, + } + : { value: input.value, message: input.errorMessage }, + } + )} + + return ( + + + + + {input.label} + + + {input.name === 'location' && locationRadios} + + + + ); +}; + +export default ValidatedTextField; \ No newline at end of file