Skip to content

Commit

Permalink
improved-code-structure
Browse files Browse the repository at this point in the history
  • Loading branch information
ankman007 committed Sep 19, 2024
1 parent e7331d0 commit 3d59f60
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 140 deletions.
2 changes: 0 additions & 2 deletions src/components/DateField/DateFieldComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export const DateFieldComponent: React.FC<DateFieldComponentProps> = ({
name,
value,
onChange,
onBlur,
localError = null,
helperText,
required = false,
Expand Down Expand Up @@ -35,7 +34,6 @@ export const DateFieldComponent: React.FC<DateFieldComponentProps> = ({
name={name}
value={value}
onChange={handleChange}
onBlur={onBlur}
fullWidth
required={required}
variant="outlined"
Expand Down
2 changes: 1 addition & 1 deletion src/components/FileUpload/FileUploadComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const FileUploadComponent: React.FC<FileUploadComponentProps> = ({ label,
const handleFileChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const file = event.target.files?.[0];
if (file) {
if (file.size > 1000000) {
if (file.size > 10000000) {
setError("File size exceeds 1MB.");
return;
}
Expand Down
7 changes: 6 additions & 1 deletion src/components/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@ const Form: React.FC = () => {
const [formData, setFormData] = useState<FormData>({});
const [submitted, setSubmitted] = useState(false);

const handleChange = (name: string, value: string | boolean | string[]) => {
const handleChange = (name: string, value: any) => {
if (value && value.target) {
value = value.target.type === 'checkbox' ? value.target.checked : value.target.value;
}

setFormData((prevData) => ({
...prevData,
[name]: value,
}));
};


const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
Expand Down
214 changes: 87 additions & 127 deletions src/components/InputField.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import React, { useState } from "react";
import { SelectChangeEvent } from "@mui/material";
import React from "react";
import { InputFieldProps } from "../types/types";
import { validateUserInput } from "../utils/validation";
import { CheckboxComponent } from "./Checkbox/Checkbox";
import { RadioComponent } from "./Radio/RadioComponent";
import { SelectComponent } from "./Select/SelectComponent";
Expand All @@ -20,139 +18,101 @@ const InputField: React.FC<InputFieldProps> = ({
onChange,
value,
}) => {
const [localError, setLocalError] = useState<string | null>(null);

const handleBlur = () => {
const userValidation = validateUserInput(type, value, required, label);
setLocalError(userValidation);
};

const handleChange = (
event: React.ChangeEvent<
HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement
>
) => {
const target = event.target;
const renderField = () => {
switch (type) {
case "text":
case "email":
case "number":
case "password":
case "textarea":
return (
<TextFieldComponent
type={type === "textarea" ? "textarea" : type}
label={label}
name={name}
value={value as string}
onChange={onChange}
helperText={helperText}
required={required}
/>
);

if (target.type === "checkbox") {
handleCheckboxChange(event, target.checked);
} else if (target.type === "radio") {
onChange(target.value);
} else {
onChange(target.value);
}
};
case "select":
return (
<SelectComponent
label={label}
name={name}
value={value as string}
onChange={onChange}
options={options}
required={required}
/>
);

const handleSelectChange = (event: SelectChangeEvent<string>) => {
const selectedValue = event;
onChange(selectedValue);
};
case "multi-select":
return (
<MultiSelectComponent
label={label}
name={name}
value={value as string[]}
onChange={onChange}
options={options}
required={required}
/>
);

const handleCheckboxChange = (
event: React.ChangeEvent<HTMLInputElement>,
checked: boolean
) => {
onChange(checked);
};
case "radio":
return (
<RadioComponent
label={label}
name={name}
value={value as string}
onChange={onChange}
options={options}
required={required}
/>
);

return (
<div>
{["text", "email", "number", "password"].includes(type) ? (
<TextFieldComponent
type={type}
label={label}
name={name}
value={value as string}
onChange={handleChange}
onBlur={handleBlur}
localError={localError}
helperText={helperText}
required={required}
/>
) : null}
case "checkbox":
return (
<CheckboxComponent
name={name}
label={label}
value={value as boolean}
onChange={onChange}
required={required}
/>
);

{type === "textarea" ? (
<TextFieldComponent
type="textarea"
label={label}
name={name}
value={value as string}
onChange={handleChange}
onBlur={handleBlur}
localError={localError}
helperText={helperText}
required={required}
/>
) : null}
case "file":
case "image":
return (
<FileUploadComponent
label={label}
name={name}
onChange={onChange}
/>
);

{type === "select" ? (
<SelectComponent
label={label}
name={name}
value={value as string}
onChange={handleSelectChange}
onBlur={handleBlur}
options={options}
localError={localError}
required={required}
/>
) : null}
case "date":
return (
<DateFieldComponent
label={label}
name={name}
value={value as string}
onChange={onChange}
required={required}
helperText={helperText}
/>
);

{type === "multi-select" ? (
<MultiSelectComponent
label={label}
name={name}
value={value as string[]}
onChange={handleSelectChange}
options={options}
localError={localError}
required={required}
/>
) : null}

{type === "radio" ? (
<RadioComponent
label={label}
name={name}
value={value as string}
onChange={handleChange}
options={options}
required={required}
/>
) : null}

{["file", "image"].includes(type) ? (
<FileUploadComponent
label={label}
name={name}
onChange={handleChange}
/>
) : null}

{type === "date" ? (
<DateFieldComponent
label={label}
name={name}
value={value as string}
onChange={handleChange}
onBlur={handleBlur}
required={required}
localError={localError}
helperText={helperText}
/>
) : null}
default:
return null;
}
};

{type === "checkbox" ? (
<CheckboxComponent
name={name}
label={label}
value={value as boolean}
onChange={handleCheckboxChange}
required={false}
/>
) : null}
</div>
);
return <div>{renderField()}</div>;
};

export default InputField;
1 change: 0 additions & 1 deletion src/components/Select/SelectComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ export const SelectComponent: React.FC<SelectComponentProps> = ({
name={name}
value={value || ""}
onChange={handleChange}
onBlur={handleBlur}
required={required}
label={label}
>
Expand Down
2 changes: 1 addition & 1 deletion src/components/TextField/TextFieldComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const TextFieldComponent: React.FC<TextFieldComponentProps> = ({
setError(`${label} is required.`);
} else {
setError(null);
setActive(true);
setActive(true)
}
};

Expand Down
9 changes: 2 additions & 7 deletions src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ export type CheckboxComponentProps = {
onChange: (event: React.ChangeEvent<HTMLInputElement>, checked: boolean) => void;

required?: boolean;
localError?: string | null;
};

export type RadioComponentProps = {
Expand All @@ -63,17 +62,15 @@ export type RadioComponentProps = {
onChange: (event: React.ChangeEvent<HTMLInputElement>) => void;
options: Option[];
required?: boolean;
localError?: string | null;
};

export type SelectComponentProps = {
name: string;
label: string;
value: string;
value: unknown;
// onChange: (event: SelectChangeEvent<string>) => void;
onChange: (value: string) => void;
options: Option[];
localError?: string | null;
required?: boolean;
};

Expand All @@ -84,7 +81,6 @@ export type MultiSelectComponentProps = {
onChange: (event: SelectChangeEvent<string[]>) => void;
options: Option[];
required?: boolean;
localError?: string | null;
};

export type TextFieldComponentProps = {
Expand All @@ -94,7 +90,7 @@ export type TextFieldComponentProps = {
value: string;
onChange: (event: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => void;
onBlur?: () => void;
localError?: string | null;

helperText?: string;
required?: boolean;
};
Expand All @@ -111,7 +107,6 @@ export type DateFieldComponentProps = {
value: string;
onChange: (event: React.ChangeEvent<HTMLInputElement>) => void;
onBlur?: () => void;
localError?: string | null;
helperText?: string;
required?: boolean;
};

0 comments on commit 3d59f60

Please sign in to comment.