complete up to request for storing azmayesh_sample and complete design of...
This commit is contained in:
35
src/core/components/FormMaker/DateType.jsx
Normal file
35
src/core/components/FormMaker/DateType.jsx
Normal file
@@ -0,0 +1,35 @@
|
||||
import { FormControl } from "@mui/material";
|
||||
import CustomDatePicker from "@/core/components/CustomDatePicker";
|
||||
import moment from "jalali-moment";
|
||||
import { useEffect, useState } from "react";
|
||||
|
||||
const DateType = ({ itemInfo, defaultValues, setDefaultValues }) => {
|
||||
const [dateValue, setDateValue] = useState(null);
|
||||
const handleDateChange = (newValue) => {
|
||||
const date = new Date(newValue);
|
||||
const formattedDate = moment(date).locale("fa").format("YYYY/MM/DD");
|
||||
setDefaultValues((prev) => ({
|
||||
...prev,
|
||||
[itemInfo.id]: newValue ? formattedDate : "",
|
||||
}));
|
||||
setDateValue(moment(date).format("YYYY/MM/DD"));
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
if (defaultValues[itemInfo.id]) {
|
||||
setDateValue(moment.from(defaultValues[itemInfo.id], "fa", "YYYY/MM/DD"));
|
||||
}
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<FormControl fullWidth size="small" variant="outlined">
|
||||
<CustomDatePicker
|
||||
dateValue={dateValue ? new Date(dateValue) : null}
|
||||
setDateValue={handleDateChange}
|
||||
placeholder={itemInfo.name}
|
||||
/>
|
||||
</FormControl>
|
||||
);
|
||||
};
|
||||
|
||||
export default DateType;
|
||||
30
src/core/components/FormMaker/InputType.jsx
Normal file
30
src/core/components/FormMaker/InputType.jsx
Normal file
@@ -0,0 +1,30 @@
|
||||
import { FormControl, InputLabel, OutlinedInput } from "@mui/material";
|
||||
import React from "react";
|
||||
|
||||
const InputType = ({ itemInfo, defaultValues, setDefaultValues }) => {
|
||||
const handleChange = (event) => {
|
||||
const { value } = event.target;
|
||||
setDefaultValues((prevValues) => ({
|
||||
...prevValues,
|
||||
[itemInfo.id]: value,
|
||||
}));
|
||||
};
|
||||
|
||||
return (
|
||||
<FormControl size="small" fullWidth variant="outlined">
|
||||
<InputLabel htmlFor={itemInfo.id}>{itemInfo.name}</InputLabel>
|
||||
<OutlinedInput
|
||||
id={itemInfo.id}
|
||||
label={itemInfo.name}
|
||||
autoComplete="off"
|
||||
size="small"
|
||||
fullWidth
|
||||
type="text"
|
||||
onChange={handleChange}
|
||||
value={defaultValues[itemInfo.id] || ""}
|
||||
/>
|
||||
</FormControl>
|
||||
);
|
||||
};
|
||||
|
||||
export default InputType;
|
||||
34
src/core/components/FormMaker/SelectType.jsx
Normal file
34
src/core/components/FormMaker/SelectType.jsx
Normal file
@@ -0,0 +1,34 @@
|
||||
import { FormControl, InputLabel, MenuItem, Select } from "@mui/material";
|
||||
import React from "react";
|
||||
|
||||
const SelectType = ({ itemInfo, defaultValues, setDefaultValues }) => {
|
||||
const handleChange = (event) => {
|
||||
const newValue = event.target.value;
|
||||
setDefaultValues((prev) => ({
|
||||
...prev,
|
||||
[itemInfo.id]: newValue,
|
||||
}));
|
||||
};
|
||||
|
||||
return (
|
||||
<FormControl size="small" fullWidth variant="outlined">
|
||||
<InputLabel id={itemInfo.id}>{itemInfo.name}</InputLabel>
|
||||
<Select
|
||||
labelId={itemInfo.id}
|
||||
id={itemInfo.id}
|
||||
value={defaultValues[itemInfo.id] || ""}
|
||||
label={itemInfo.name}
|
||||
onChange={handleChange}
|
||||
>
|
||||
<MenuItem value="">{itemInfo.name}</MenuItem>
|
||||
{itemInfo.option?.map((item, index) => (
|
||||
<MenuItem key={index} value={item}>
|
||||
{item}
|
||||
</MenuItem>
|
||||
))}
|
||||
</Select>
|
||||
</FormControl>
|
||||
);
|
||||
};
|
||||
|
||||
export default SelectType;
|
||||
33
src/core/components/FormMaker/index.jsx
Normal file
33
src/core/components/FormMaker/index.jsx
Normal file
@@ -0,0 +1,33 @@
|
||||
import InputType from "./InputType";
|
||||
import SelectType from "./SelectType";
|
||||
import DateType from "./DateType";
|
||||
import { Grid } from "@mui/material";
|
||||
|
||||
const componentMap = {
|
||||
input: InputType,
|
||||
select: SelectType,
|
||||
date: DateType,
|
||||
};
|
||||
|
||||
const FormMaker = ({ formInfo, defaultValues, setDefaultValues }) => {
|
||||
return (
|
||||
<Grid container spacing={1}>
|
||||
{formInfo?.map((itemInfo) => {
|
||||
const Component = componentMap[itemInfo.type] || null;
|
||||
return (
|
||||
<Grid item xs={12} sm={6} md={4} key={itemInfo.id}>
|
||||
{Component ? (
|
||||
<Component
|
||||
itemInfo={itemInfo}
|
||||
defaultValues={defaultValues}
|
||||
setDefaultValues={setDefaultValues}
|
||||
/>
|
||||
) : null}
|
||||
</Grid>
|
||||
);
|
||||
})}
|
||||
</Grid>
|
||||
);
|
||||
};
|
||||
|
||||
export default FormMaker;
|
||||
Reference in New Issue
Block a user