"use client"; import { Card, CardHeader, CardContent, SxProps, Theme, Typography, Grid, TextField, FormControl, InputLabel, Select, MenuItem, Checkbox, FormControlLabel, Button, } from "@mui/material"; import { DataGrid, GridColDef, GridRowSelectionModel } from "@mui/x-data-grid"; import { darken, lighten, styled } from "@mui/material/styles"; import { Controller, useForm, useFormContext } from "react-hook-form"; import Stack from "@mui/material/Stack"; import { useTranslation } from "react-i18next"; import Box from "@mui/material/Box"; import { LocalizationProvider } from "@mui/x-date-pickers/LocalizationProvider"; import { AdapterDayjs } from "@mui/x-date-pickers/AdapterDayjs"; import { DemoItem } from "@mui/x-date-pickers/internals/demo"; import { DatePicker } from "@mui/x-date-pickers/DatePicker"; import dayjs from "dayjs"; import { useCallback, useEffect, useState } from "react"; import { Check, Close } from "@mui/icons-material"; import { NumericFormat, NumericFormatProps } from "react-number-format"; import * as React from "react"; // interface Option { // // Define properties of each option object // // based on your specific requirements // id: any; // value: any; // label: string; // } interface Field { // subtitle: string; id: string; label: string; type: string; value?: any; required?: boolean; pattern?: string; message?: string; options?: any[]; readOnly?: boolean; size?: number; setValue?: any[]; } interface CustomProps { onChange: (event: { target: { name: string; value: string } }) => void; name: string; } interface CustomInputFormProps { onSubmit: (data: any) => void; onSubmitError?: (data: any) => void; onCancel: () => void; // resetForm: () => void; Title?: string[]; isActive: boolean; fieldLists: Field[][]; } // interface SubComponents { // Loading: typeof CustomerSearchLoading; // } const CustomInputForm: React.FC = ({ Title, isActive, fieldLists, onSubmit, onSubmitError, onCancel, // resetForm, }) => { const { t } = useTranslation(); const { reset, register, handleSubmit, control, formState: { errors }, } = useForm(); const [dateObj, setDateObj] = useState(null); const [value, setValue] = useState({}); const [checkboxValue, setCheckboxValue] = useState({}); interface DateObj { [key: string]: string; } const handleFormSubmit = (data: any) => { // if (date != null || date != undefined ) { // data.date = dayjs(date).format('YYYY-MM-DD'); // } for (const key in data) { if (!isNaN(data[key])) { if (data[key] !== "") { if (Number.isInteger(parseFloat(data[key]))) { data[key] = parseInt(data[key]); } else { data[key] = parseFloat(data[key]); } } } } if (checkboxValue !== null) { data = { ...data, ...checkboxValue }; } const finalData = { ...value, ...data, ...dateObj, }; console.log(finalData); onSubmit(finalData); }; const handleDateChange = (id: string, newValue: dayjs.Dayjs | null): void => { console.log(dayjs(newValue).format("YYYY-MM-DD")); setDateObj((prevValues: DateObj) => ({ ...prevValues, [id]: newValue ? dayjs(newValue).format("YYYY-MM-DD") : "", })); }; const handleCheckboxChange = (id: string, newValue: boolean): void => { setCheckboxValue((prevValues: { [key: string]: boolean }) => ({ ...prevValues, [id]: newValue, })); }; const handleAutocompleteChange = (id: any, newValue: any) => { setValue((prevValues: any) => ({ ...prevValues, [id]: newValue, })); }; const handleCancel = () => { reset(); // resetForm(); // setFromDate(null); setDateObj(null); setValue({}); if (onCancel) { onCancel(); } // if fields include setValue func // fieldLists.map((list) => { // list.map((field) => { // if (typeof field.setValue === 'function') { // field.setValue(typeof field.value === 'boolean' ? false : null); // } else if (typeof field.setValue === 'object') { // field.setValue.map((setFunc: any) => { // setFunc(null); // }); // } // }) // }); // setToDate(null); }; fieldLists.forEach((list) => { list.forEach((obj) => { if ( obj.id === "created" || obj.id === "createdBy" || obj.id === "modified" || obj.id === "deleted" || obj.id === "modifiedBy" ) { obj.readOnly = true; } }); }); const NumericFormatCustom = React.forwardRef( function NumericFormatCustom(props, ref) { const { onChange, ...other } = props; return ( { onChange({ target: { name: props.name, value: values.value, }, }); }} thousandSeparator valueIsNumericString prefix="$" /> ); } ); const [values, setValues] = React.useState({ hourlyRate: "", }); const handleChange = (event: React.ChangeEvent) => { setValues({ ...values, [event.target.name]: event.target.value, }); }; return (
<> {fieldLists.map((fieldList, fieldListIndex) => ( {Title ? ( {t(`${Title[fieldListIndex]}`)} ) : null} {fieldList.map((field: Field) => { if (field.type === "text") { return ( ); } else if (field.type === "email") { return ( ); } else if (field.type === "multiDate") { return ( { handleDateChange(field.id, newValue); }} // required = {field.required ?? false}, /> ); } else if (field.type === "combo-Obj") { return ( {field.label} ( )} /> ); } else if (field.type === "numeric") { return ( ); } else if (field.type === "numeric-testing") { return ( { console.log(field); return ( ); }} /> ); } else if (field.type === "numeric-positive") { return ( ); } else if (field.type === "checkbox") { return ( { handleCheckboxChange( field.id, event.target.checked ); }} color="primary" /> } label={ {field.label} } /> ); } else if (field.type === "remarks") { return ( ); } else { return ( ); } })} ))}
); }; // CustomInputForm.Loading = CustomerSearchLoading; export default CustomInputForm;