Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 

264 linhas
9.2 KiB

  1. "use client";
  2. import React, { useState, FC } from 'react';
  3. import {
  4. Stack,
  5. Typography,
  6. FormControlLabel,
  7. Checkbox,
  8. Autocomplete,
  9. Button,
  10. Grid,
  11. TextField,
  12. CardHeader,
  13. Card,
  14. FormControl,
  15. InputLabel,
  16. Select,
  17. MenuItem,
  18. ThemeProvider
  19. } from '@mui/material';
  20. import { useForm, Controller } from 'react-hook-form';
  21. import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
  22. import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';
  23. import dayjs from 'dayjs';
  24. import SearchIcon from '@mui/icons-material/Search';
  25. import RefreshIcon from '@mui/icons-material/Refresh';
  26. import { DemoItem } from '@mui/x-date-pickers/internals/demo';
  27. import { DatePicker } from '@mui/x-date-pickers/DatePicker';
  28. interface Field {
  29. id: any;
  30. label: any;
  31. type: string;
  32. options?: Array<{ id: string; label: string }>;
  33. setValue: ((value: any) => void) | Array<(value: any) => void>;
  34. value?: any;
  35. required?: boolean;
  36. }
  37. interface FormComponentProps {
  38. fields: Field[];
  39. onSubmit: (data: any) => void;
  40. resetForm: () => void;
  41. sx?: any;
  42. }
  43. interface SearchFormProps {
  44. applySearch: (data: any) => void;
  45. fields: Field[];
  46. title?: string;
  47. sx?: any;
  48. }
  49. const FormComponent: FC<FormComponentProps> = ({ fields, onSubmit, resetForm, sx }) => {
  50. const { reset, register, handleSubmit, control } = useForm();
  51. const [fromDate, setFromDate] = useState<dayjs.Dayjs | null>(null);
  52. const [dayRangeFromDate, setDayRangeFromDate] = useState<dayjs.Dayjs | null>(null);
  53. const [dayRangeToDate, setDayRangeToDate] = useState<dayjs.Dayjs | null>(null);
  54. const [value, setValue] = useState<{ [key: string]: any }>({});
  55. const [checkbox1, setCheckbox1] = useState(false);
  56. const handleFormSubmit = (data: any) => {
  57. if (fromDate != null || fromDate != undefined) {
  58. data.fromDate = dayjs(fromDate).format('YYYY-MM-DD');
  59. }
  60. if (value !== null) {
  61. data.dropdownCombo = value
  62. }
  63. if (value !== null) {
  64. data.checkbox = checkbox1;
  65. }
  66. if (dayRangeFromDate != null || dayRangeFromDate != undefined) {
  67. data.dayRangeFromDate = dayjs(dayRangeFromDate).format('YYYY-MM-DD');
  68. }
  69. if (dayRangeToDate != null || dayRangeToDate != undefined) {
  70. data.dayRangeToDate = dayjs(dayRangeToDate).format('YYYY-MM-DD');
  71. }
  72. onSubmit(data);
  73. };
  74. const handleFormReset = () => {
  75. reset();
  76. resetForm();
  77. setFromDate(null);
  78. fields.forEach((field) => {
  79. if (typeof(field.setValue) === "function") {
  80. field.setValue(typeof (field.value) === "boolean" ? false : null);
  81. } else if (Array.isArray(field.setValue)) {
  82. field.setValue.forEach((setFunc) => {
  83. setFunc(null);
  84. });
  85. }
  86. });
  87. };
  88. return (
  89. <form onSubmit={handleSubmit(handleFormSubmit)}>
  90. <Grid container alignItems="center">
  91. {fields.map((field) => {
  92. if (field.type === 'dropdown') {
  93. return (
  94. <Grid item xs={12} sm={5.5} md={5.5} lg={5.5} sx={{ ml: 3, mr: 3, mb: 3 }} key={field.id}>
  95. <FormControl fullWidth>
  96. <InputLabel id={`${field.id}-label`}>{field.label}</InputLabel>
  97. <Controller
  98. name={field.id}
  99. control={control}
  100. defaultValue=""
  101. render={({ field: { onChange, value } }) => (
  102. <Select
  103. labelId={`${field.id}-label`}
  104. id={field.id}
  105. value={value}
  106. onChange={(e) => {
  107. onChange(e.target.value);
  108. }}
  109. >
  110. {field.options?.map((option) => (
  111. <MenuItem
  112. value={option.id ?? JSON.stringify(option)}
  113. key={option.id ?? JSON.stringify(option)}
  114. >
  115. {option.id !== undefined ? option.label : JSON.stringify(option)}
  116. </MenuItem>
  117. ))}
  118. </Select>
  119. )}
  120. />
  121. </FormControl>
  122. </Grid>
  123. );
  124. } else if (field.type === 'date') {
  125. return (
  126. <Grid item xs={12} sm={5.5} md={5.5} lg={5.5} sx={{ ml: 3, mr: 3, mb: 3 }} key={field.id}>
  127. <LocalizationProvider dateAdapter={AdapterDayjs}>
  128. <DemoItem>
  129. <DatePicker
  130. slotProps={{
  131. textField: {
  132. id:field.id,
  133. },
  134. }}
  135. label={field.label}
  136. value={fromDate === null ? null : dayjs(fromDate)}
  137. onChange={(newValue) => {
  138. setFromDate(newValue);
  139. }}
  140. />
  141. </DemoItem>
  142. </LocalizationProvider>
  143. </Grid>
  144. );
  145. } else if (field.type === 'checkbox') {
  146. return (
  147. <Grid item xs={12} sm={5.5} md={5.5} lg={5.5} sx={{ ml: 3, mr: 3, mb: 3 }} key={field.id}>
  148. <FormControlLabel
  149. control={
  150. <Checkbox
  151. id={field.id}
  152. checked={field.value}
  153. onChange={(event) => {
  154. if (typeof field.setValue === 'function') {
  155. field.setValue(event.target.checked);
  156. setCheckbox1(event.target.checked);
  157. }
  158. }}
  159. color="primary"
  160. />
  161. }
  162. label={<Typography style={{fontSize:"1.15em"}}>{field.label}</Typography>}
  163. />
  164. </Grid>
  165. )
  166. } else if (field.type === 'dateRange') {
  167. return (
  168. <Grid container key={field.id[0]}>
  169. <Grid item xs={12} sm={7} md={7} lg={7} sx={{ ml: 3, mr: 3, mb: 3 }}>
  170. <Grid container>
  171. <Grid>
  172. <LocalizationProvider dateAdapter={AdapterDayjs}>
  173. <DatePicker
  174. label={field.label[0]}
  175. value={field.value[0]}
  176. onChange={(newValue) => setDayRangeFromDate(newValue)}
  177. />
  178. </LocalizationProvider>
  179. </Grid>
  180. <Grid item xs={1.5} sm={1.5} md={1.5} lg={1.5} sx={{ display: 'flex', justifyContent: "center", alignItems: 'center' }}>
  181. To
  182. </Grid>
  183. <Grid item xs={5.25} sm={5.25} md={5.25} lg={5.25}>
  184. <LocalizationProvider dateAdapter={AdapterDayjs}>
  185. <DatePicker
  186. label={field.label[1]}
  187. value={field.value[1]}
  188. onChange={(newValue) => setDayRangeToDate(newValue)}
  189. />
  190. </LocalizationProvider>
  191. </Grid>
  192. </Grid>
  193. </Grid>
  194. </Grid>
  195. )
  196. }
  197. return (
  198. <Grid item xs={12} sm={5.5} md={5.5} lg={5.5} sx={{ ml: 3, mr: 3, mb: 3 }} key={field.id}>
  199. <TextField
  200. fullWidth
  201. {...register(field.id)}
  202. id={field.id}
  203. label={field.label}
  204. defaultValue={field.value !== undefined && field.value !== null ? `${field.value}` : ''}
  205. required={field.required === true ? field.required : false}
  206. sx={{ ...sx }}
  207. />
  208. </Grid>
  209. );
  210. })}
  211. </Grid>
  212. <Grid container maxWidth="lg" justifyContent="space-between" style={{marginTop:-20}}>
  213. <Stack direction="row">
  214. <Grid item sx={{ ml: 3, mr: 3, mb: 3, mt: 3 }}>
  215. <Button className="h-12 w-32" style={{backgroundColor:"#92c1e9",color:"white",fontSize:"1.15em",fontWeight:100}} type="submit">
  216. <SearchIcon/>&nbsp;Search
  217. </Button>
  218. </Grid>
  219. <Grid item sx={{ ml: 3, mr: 3, mb: 3, mt: 3 }}>
  220. <Button className="h-12 w-32" style={{backgroundColor:"#f890a5",color:"white",fontSize:"1.15em",fontWeight:100}} onClick={handleFormReset}>
  221. <RefreshIcon/>&nbsp;Reset
  222. </Button>
  223. </Grid>
  224. </Stack>
  225. </Grid>
  226. </form>
  227. );
  228. };
  229. const CustomSearchForm: FC<SearchFormProps> = ({ applySearch, fields, title, sx }) => {
  230. const Title = title || "Searching Criteria";
  231. const handleSubmit = (data: any) => {
  232. if (applySearch) {
  233. applySearch(data);
  234. } else {
  235. console.log('applySearch function is null');
  236. }
  237. };
  238. const handleFormReset = () => {
  239. console.log('Form Reset');
  240. };
  241. return (
  242. <Card style={{marginRight:20}}>
  243. <CardHeader className="text-slate-500" style={{marginTop:-20}} titleTypographyProps={{ variant: 'h5' }} title={Title}></CardHeader>
  244. <FormComponent fields={fields} onSubmit={handleSubmit} resetForm={handleFormReset} sx={sx} />
  245. </Card>
  246. );
  247. };
  248. export default CustomSearchForm;