trithuc-mvc-react 3.0.3 → 3.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Accordion, AccordionSummary, Box, Slider, Typography } from "@mui/material";
|
|
1
|
+
import { Accordion, AccordionSummary, Box, IconButton, Slider, Typography } from "@mui/material";
|
|
2
2
|
import Grid from "@mui/material/Unstable_Grid2";
|
|
3
3
|
import { useFormContext } from "react-hook-form";
|
|
4
4
|
import { FilterElement } from "./FilterElement";
|
|
@@ -9,7 +9,10 @@ import AccordionDetails from "@mui/material/AccordionDetails";
|
|
|
9
9
|
import moment from "moment";
|
|
10
10
|
import { useEffect, useState } from "react";
|
|
11
11
|
import { DateRangePicker } from "../date";
|
|
12
|
-
|
|
12
|
+
import { ClearIcon, DatePicker } from "@mui/x-date-pickers";
|
|
13
|
+
import { LocalizationProvider } from "@mui/x-date-pickers/LocalizationProvider";
|
|
14
|
+
import "dayjs/locale/vi";
|
|
15
|
+
import { toast } from "react-toastify";
|
|
13
16
|
export const FilterGod = ({ tableName, filters, elementSize = "small", setPage = () => {} }) => {
|
|
14
17
|
const { handleSubmit } = useFormContext();
|
|
15
18
|
const onSubmit = (data) => console.log(data);
|
|
@@ -46,33 +49,102 @@ export const FilterGod = ({ tableName, filters, elementSize = "small", setPage =
|
|
|
46
49
|
<Grid container spacing={1}>
|
|
47
50
|
{filters.map(({ field, size, ...rest }) => {
|
|
48
51
|
if (rest.type === "date-range") {
|
|
49
|
-
|
|
52
|
+
const [label1, label2] = Array.isArray(field.label) ? field.label : ["Từ ngày", "Đến ngày"];
|
|
50
53
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
54
|
+
const handleClear = (key) => {
|
|
55
|
+
setDataSearch((prev) => ({
|
|
56
|
+
...prev,
|
|
57
|
+
[key]: null // Xóa giá trị của trường được chọn
|
|
58
|
+
}));
|
|
59
|
+
setPage(0); // Reset trang khi clear
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
const handleDateChange = (newValue, fieldKey, compareKey, compareType) => {
|
|
63
|
+
let formattedDate = null;
|
|
64
|
+
|
|
65
|
+
// Kiểm tra nếu newValue là ngày hợp lệ
|
|
66
|
+
if (!moment(newValue, "DD/MM/YYYY", true).isValid()) {
|
|
67
|
+
return; // Dừng nếu người dùng nhập chưa xong hoặc giá trị không hợp lệ
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (newValue) {
|
|
71
|
+
// Xác định thời gian dựa trên fieldKey
|
|
72
|
+
if (fieldKey.toLowerCase().includes("from")) {
|
|
73
|
+
formattedDate = moment(newValue, "DD/MM/YYYY").startOf("day").format("YYYY-MM-DD HH:mm");
|
|
74
|
+
} else if (fieldKey.toLowerCase().includes("to")) {
|
|
75
|
+
formattedDate = moment(newValue, "DD/MM/YYYY").endOf("day").format("YYYY-MM-DD HH:mm");
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const compareDate = dataSearch?.[compareKey] ? moment(dataSearch?.[compareKey]) : null;
|
|
62
80
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
81
|
+
if (formattedDate && compareDate) {
|
|
82
|
+
const isInvalid =
|
|
83
|
+
(compareType === "min" && moment(formattedDate).isBefore(compareDate)) ||
|
|
84
|
+
(compareType === "max" && moment(formattedDate).isAfter(compareDate));
|
|
85
|
+
|
|
86
|
+
if (isInvalid) {
|
|
87
|
+
toast.error(
|
|
88
|
+
compareType === "min" ? "Ngày đến không được nhỏ hơn ngày từ." : "Ngày từ không được lớn hơn ngày đến."
|
|
89
|
+
);
|
|
90
|
+
setDataSearch((prev) => ({
|
|
91
|
+
...prev,
|
|
92
|
+
[fieldKey]: null
|
|
93
|
+
}));
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
setDataSearch((prev) => ({
|
|
99
|
+
...prev,
|
|
100
|
+
[fieldKey]: formattedDate
|
|
101
|
+
}));
|
|
102
|
+
setPage(0); // Reset page
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
const renderDatePicker = (fieldKey, label, compareKey, compareType) => (
|
|
106
|
+
<LocalizationProvider adapterLocale="vi" localeText={{ clearButtonLabel: "Xóa" }}>
|
|
107
|
+
<DatePicker
|
|
108
|
+
label={label}
|
|
109
|
+
format="DD/MM/YYYY"
|
|
110
|
+
desktopModeMediaQuery="@media (min-width: 0px)"
|
|
111
|
+
views={["year", "month", "day"]}
|
|
112
|
+
mask="__/__/____"
|
|
113
|
+
minDate={
|
|
114
|
+
compareType === "min" ? (dataSearch?.[compareKey] ? moment(dataSearch?.[compareKey]) : null) : null
|
|
115
|
+
}
|
|
116
|
+
maxDate={
|
|
117
|
+
compareType === "max" ? (dataSearch?.[compareKey] ? moment(dataSearch?.[compareKey]) : null) : null
|
|
118
|
+
}
|
|
119
|
+
InputLabelProps={{ shrink: true }}
|
|
120
|
+
slotProps={{
|
|
121
|
+
textField: {
|
|
122
|
+
fullWidth: true,
|
|
123
|
+
InputProps: {
|
|
124
|
+
startAdornment: (
|
|
125
|
+
<IconButton onClick={() => handleClear(fieldKey)} aria-label="clear">
|
|
126
|
+
<ClearIcon />
|
|
127
|
+
</IconButton>
|
|
128
|
+
)
|
|
129
|
+
}
|
|
130
|
+
},
|
|
131
|
+
actionBar: { actions: ["clear"] }
|
|
72
132
|
}}
|
|
133
|
+
value={dataSearch?.[fieldKey] ? moment(dataSearch?.[fieldKey]) : null}
|
|
134
|
+
onChange={(newValue) => handleDateChange(newValue, fieldKey, compareKey, compareType)}
|
|
73
135
|
size={elementSize}
|
|
74
|
-
value={[dataSearch?.[field?.[0]] || null, dataSearch?.[field?.[1]] || null]}
|
|
75
136
|
/>
|
|
137
|
+
</LocalizationProvider>
|
|
138
|
+
);
|
|
139
|
+
|
|
140
|
+
return (
|
|
141
|
+
<Grid container key={field.toString()} md={size?.md} xs={size?.xs} sm={size?.sm}>
|
|
142
|
+
<Grid item xs={6}>
|
|
143
|
+
{renderDatePicker(field[0], label1, field[1], "max")}
|
|
144
|
+
</Grid>
|
|
145
|
+
<Grid item xs={6}>
|
|
146
|
+
{renderDatePicker(field[1], label2, field[0], "min")}
|
|
147
|
+
</Grid>
|
|
76
148
|
</Grid>
|
|
77
149
|
);
|
|
78
150
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "trithuc-mvc-react",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.5",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
@@ -45,6 +45,7 @@
|
|
|
45
45
|
"react-dom": ">=16",
|
|
46
46
|
"react-hook-form": "^7.45.2",
|
|
47
47
|
"react-query": "^3.39.3",
|
|
48
|
-
"react-toastify": "^10.0.6"
|
|
48
|
+
"react-toastify": "^10.0.6",
|
|
49
|
+
"dayjs": "^1.11.13"
|
|
49
50
|
}
|
|
50
51
|
}
|