Refactored CabinetMaintenance.jsx to use FormField and FormInput
This commit is contained in:
parent
d4fd7601f1
commit
2afefc7442
@ -3,9 +3,10 @@ import { useNavigate } from "react-router-dom";
|
|||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import FormBox from "../components/FormBox";
|
import FormBox from "../components/FormBox";
|
||||||
import Button from "../components/Button";
|
import Button from "../components/Button";
|
||||||
import { motion } from "motion/react";
|
import FormInput from "../components/FormInput";
|
||||||
import clsx from "clsx";
|
import FormSelect from "../components/FormSelect";
|
||||||
import { AnimatePresence } from "motion/react";
|
import FieldsWrapper from "../components/FieldsWrapper";
|
||||||
|
import FormField from "../components/FormField";
|
||||||
|
|
||||||
function CabinetMaintenace() {
|
function CabinetMaintenace() {
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
@ -20,47 +21,41 @@ function CabinetMaintenace() {
|
|||||||
navigate(operation.value);
|
navigate(operation.value);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const formFields = [
|
||||||
|
{
|
||||||
|
name: "value",
|
||||||
|
label: t("operation"),
|
||||||
|
options: [{ label: t("create"), value: "create" }],
|
||||||
|
type: "select",
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
const renderField = (field) => {
|
||||||
|
const commonProps = {
|
||||||
|
value: operation[field.name],
|
||||||
|
onChange: (e) => {
|
||||||
|
const newValues = { ...operation };
|
||||||
|
newValues[field.name] = e.target.value;
|
||||||
|
newValues[`${field.name}Valid`] = true;
|
||||||
|
setOperation(newValues);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const options = field.options;
|
||||||
|
return (
|
||||||
|
<FormField label={field.label}>
|
||||||
|
{field.type === "input" ? (
|
||||||
|
<FormInput props={commonProps} />
|
||||||
|
) : (
|
||||||
|
<FormSelect props={commonProps} options={options} />
|
||||||
|
)}
|
||||||
|
</FormField>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<FormBox title={t("cabinetMaintenance")}>
|
<FormBox title={t("cabinetMaintenance")}>
|
||||||
<div className="p-2 pt-7">
|
<FieldsWrapper>{formFields.map(renderField)}</FieldsWrapper>
|
||||||
<div className="flex">
|
|
||||||
<label className="mr-4 text-lg text-black dark:text-primary-dark w-[10%]">
|
|
||||||
{t("operation")}
|
|
||||||
</label>
|
|
||||||
<div className="w-full">
|
|
||||||
<select
|
|
||||||
className={clsx(
|
|
||||||
"w-1/5 h-10 px-2 rounded-full dark:bg-grey dark:text-primary-dark border-2 focus:outline-grey border-grey",
|
|
||||||
!operation.valid && "border-error"
|
|
||||||
)}
|
|
||||||
onChange={(e) =>
|
|
||||||
setOperation({ value: e.target.value, valid: true })
|
|
||||||
}
|
|
||||||
defaultValue={operation.value}
|
|
||||||
value={operation.value}
|
|
||||||
>
|
|
||||||
<option value="" disabled>
|
|
||||||
{t("select")}
|
|
||||||
</option>
|
|
||||||
<option value="create">{t("create")}</option>
|
|
||||||
</select>
|
|
||||||
<AnimatePresence>
|
|
||||||
{!operation.valid && (
|
|
||||||
<motion.div
|
|
||||||
className="w-1/5 text-sm text-error ml-3 pt-1"
|
|
||||||
initial={{ y: 15, opacity: 0 }}
|
|
||||||
animate={{ y: 0, opacity: 1 }}
|
|
||||||
exit={{ y: 15, opacity: 0 }}
|
|
||||||
key="cabinetIdError"
|
|
||||||
>
|
|
||||||
Invalid Operation
|
|
||||||
</motion.div>
|
|
||||||
)}
|
|
||||||
</AnimatePresence>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<Button text={t("next")} onClick={(e) => handleNext(e)} />
|
<Button text={t("next")} onClick={(e) => handleNext(e)} />
|
||||||
</FormBox>
|
</FormBox>
|
||||||
</div>
|
</div>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user