"use client"; import React, { useState, FC } from 'react'; import { Stack, Typography, FormControlLabel, Checkbox, Autocomplete, Button, Grid, TextField, CardHeader, Card, FormControl, InputLabel, Select, MenuItem, ThemeProvider } from '@mui/material'; import { useForm, Controller } from 'react-hook-form'; import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider'; import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs'; import dayjs from 'dayjs'; import SearchIcon from '@mui/icons-material/Search'; import RefreshIcon from '@mui/icons-material/Refresh'; import { DemoItem } from '@mui/x-date-pickers/internals/demo'; import { DatePicker } from '@mui/x-date-pickers/DatePicker'; interface Field { id: any; label: any; type: string; options?: Array<{ id: string; label: string }>; setValue: ((value: any) => void) | Array<(value: any) => void>; value?: any; required?: boolean; } interface FormComponentProps { fields: Field[]; onSubmit: (data: any) => void; resetForm: () => void; sx?: any; } interface SearchFormProps { applySearch: (data: any) => void; fields: Field[]; title?: string; sx?: any; } const FormComponent: FC = ({ fields, onSubmit, resetForm, sx }) => { const { reset, register, handleSubmit, control } = useForm(); const [fromDate, setFromDate] = useState(null); const [dayRangeFromDate, setDayRangeFromDate] = useState(null); const [dayRangeToDate, setDayRangeToDate] = useState(null); const [value, setValue] = useState<{ [key: string]: any }>({}); const [checkbox1, setCheckbox1] = useState(false); const handleFormSubmit = (data: any) => { if (fromDate != null || fromDate != undefined) { data.fromDate = dayjs(fromDate).format('YYYY-MM-DD'); } if (value !== null) { data.dropdownCombo = value } if (value !== null) { data.checkbox = checkbox1; } if (dayRangeFromDate != null || dayRangeFromDate != undefined) { data.dayRangeFromDate = dayjs(dayRangeFromDate).format('YYYY-MM-DD'); } if (dayRangeToDate != null || dayRangeToDate != undefined) { data.dayRangeToDate = dayjs(dayRangeToDate).format('YYYY-MM-DD'); } onSubmit(data); }; const handleFormReset = () => { reset(); resetForm(); setFromDate(null); fields.forEach((field) => { if (typeof(field.setValue) === "function") { field.setValue(typeof (field.value) === "boolean" ? false : null); } else if (Array.isArray(field.setValue)) { field.setValue.forEach((setFunc) => { setFunc(null); }); } }); }; return (
{fields.map((field) => { if (field.type === 'dropdown') { return ( {field.label} ( )} /> ); } else if (field.type === 'date') { return ( { setFromDate(newValue); }} /> ); } else if (field.type === 'checkbox') { return ( { if (typeof field.setValue === 'function') { field.setValue(event.target.checked); setCheckbox1(event.target.checked); } }} color="primary" /> } label={{field.label}} /> ) } else if (field.type === 'dateRange') { return ( setDayRangeFromDate(newValue)} /> To setDayRangeToDate(newValue)} /> ) } return ( ); })}
); }; const CustomSearchForm: FC = ({ applySearch, fields, title, sx }) => { const Title = title || "Searching Criteria"; const handleSubmit = (data: any) => { if (applySearch) { applySearch(data); } else { console.log('applySearch function is null'); } }; const handleFormReset = () => { console.log('Form Reset'); }; return ( ); }; export default CustomSearchForm;