mobigrid-module 1.0.0
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.
- package/dist/components/CustomTable/CustomTable.d.ts +8 -0
- package/dist/components/CustomTable/Pagination.d.ts +8 -0
- package/dist/components/Icon.d.ts +6 -0
- package/dist/components/Layout/PageHeader.d.ts +13 -0
- package/dist/components/ui/alert.d.ts +8 -0
- package/dist/components/ui/button.d.ts +11 -0
- package/dist/components/ui/calendar.d.ts +8 -0
- package/dist/components/ui/date-picker-with-range.d.ts +7 -0
- package/dist/components/ui/dialog.d.ts +19 -0
- package/dist/components/ui/input.d.ts +3 -0
- package/dist/components/ui/pagination.d.ts +28 -0
- package/dist/components/ui/popover.d.ts +7 -0
- package/dist/components/ui/select.d.ts +13 -0
- package/dist/components/ui/sonner.d.ts +5 -0
- package/dist/components/ui/table.d.ts +10 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.esm.js +70 -0
- package/dist/index.esm.js.map +1 -0
- package/dist/index.tsx +70 -0
- package/dist/index.tsx.map +1 -0
- package/dist/lib/utils.d.ts +2 -0
- package/package.json +67 -0
- package/rollup.config.js +35 -0
- package/src/components/CustomTable/CustomTable.tsx +182 -0
- package/src/components/CustomTable/Pagination.tsx +136 -0
- package/src/components/Icon.tsx +27 -0
- package/src/components/Layout/PageHeader.tsx +270 -0
- package/src/components/ui/alert.tsx +59 -0
- package/src/components/ui/button.tsx +57 -0
- package/src/components/ui/calendar.tsx +70 -0
- package/src/components/ui/date-picker-with-range.tsx +167 -0
- package/src/components/ui/dialog.tsx +120 -0
- package/src/components/ui/input.tsx +22 -0
- package/src/components/ui/pagination.tsx +117 -0
- package/src/components/ui/popover.tsx +31 -0
- package/src/components/ui/select.tsx +157 -0
- package/src/components/ui/sonner.tsx +30 -0
- package/src/components/ui/table.tsx +120 -0
- package/src/index.tsx +252 -0
- package/src/lib/utils.ts +6 -0
- package/tsconfig.json +27 -0
- package/vite.config.ts +6 -0
@@ -0,0 +1,270 @@
|
|
1
|
+
import * as React from "react";
|
2
|
+
import { DatePickerWithRange } from "../../components/ui/date-picker-with-range";
|
3
|
+
import { Button } from "../../components/ui/button";
|
4
|
+
import { DateRange } from "react-day-picker";
|
5
|
+
import { Input } from "../../components/ui/input";
|
6
|
+
import {
|
7
|
+
Select,
|
8
|
+
SelectItem,
|
9
|
+
SelectContent,
|
10
|
+
SelectValue,
|
11
|
+
SelectTrigger,
|
12
|
+
} from "../../components/ui/select";
|
13
|
+
import { useState, useMemo } from "react";
|
14
|
+
|
15
|
+
interface PageHeaderProps {
|
16
|
+
title: string;
|
17
|
+
filters: any;
|
18
|
+
configFilters: any;
|
19
|
+
setFilters: (filters: any) => void;
|
20
|
+
onSearch: () => void;
|
21
|
+
count: number;
|
22
|
+
isLoading: boolean;
|
23
|
+
setCurrentPage: (page: number) => void;
|
24
|
+
}
|
25
|
+
|
26
|
+
export function PageHeader({
|
27
|
+
title,
|
28
|
+
filters,
|
29
|
+
setFilters,
|
30
|
+
configFilters,
|
31
|
+
onSearch,
|
32
|
+
count,
|
33
|
+
isLoading,
|
34
|
+
setCurrentPage,
|
35
|
+
}: PageHeaderProps) {
|
36
|
+
const [initialFilters, setInitialFilters] = React.useState(filters);
|
37
|
+
|
38
|
+
const [exportDisabled, setExportDisabled] = useState(false);
|
39
|
+
const filtersChanged = React.useMemo(() => {
|
40
|
+
return JSON.stringify(initialFilters) !== JSON.stringify(filters);
|
41
|
+
}, [initialFilters, filters]);
|
42
|
+
|
43
|
+
const handleSearch = async () => {
|
44
|
+
setInitialFilters(filters);
|
45
|
+
if (filtersChanged) await setCurrentPage(1);
|
46
|
+
onSearch();
|
47
|
+
};
|
48
|
+
|
49
|
+
const handleDateChange = (date: DateRange | undefined) => {
|
50
|
+
setFilters({ ...filters, fromDate: date?.from, toDate: date?.to });
|
51
|
+
};
|
52
|
+
|
53
|
+
const handleExport = async (ctx: string) => {
|
54
|
+
try {
|
55
|
+
const response = await fetch(
|
56
|
+
(() => {
|
57
|
+
let url = `/cashplus/newfront/templates/extract-action.cfm?ctx=${ctx}`;
|
58
|
+
for (const [key, value] of Object.entries(filters)) {
|
59
|
+
if (value && typeof value !== "object") {
|
60
|
+
if (key === "fromDate" || key === "toDate") {
|
61
|
+
const date = new Date(value as string);
|
62
|
+
url =
|
63
|
+
url +
|
64
|
+
`&${key}=${date.getFullYear()}-${String(
|
65
|
+
date.getMonth() + 1
|
66
|
+
).padStart(2, "0")}-${String(date.getDate()).padStart(
|
67
|
+
2,
|
68
|
+
"0"
|
69
|
+
)}`;
|
70
|
+
continue;
|
71
|
+
}
|
72
|
+
|
73
|
+
url = url + `&${key}=${value}`;
|
74
|
+
}
|
75
|
+
}
|
76
|
+
return url;
|
77
|
+
})()
|
78
|
+
);
|
79
|
+
|
80
|
+
if (!response.ok) {
|
81
|
+
throw new Error("Export failed");
|
82
|
+
}
|
83
|
+
|
84
|
+
const blob = await response.blob();
|
85
|
+
const url = window.URL.createObjectURL(blob);
|
86
|
+
const a = document.createElement("a");
|
87
|
+
a.href = url;
|
88
|
+
const fileName = `${title.replace(/\s+/g, "_")}_${filters.fromDate}_${
|
89
|
+
filters.toDate
|
90
|
+
}_.csv`;
|
91
|
+
a.download = fileName;
|
92
|
+
document.body.appendChild(a);
|
93
|
+
a.click();
|
94
|
+
window.URL.revokeObjectURL(url);
|
95
|
+
document.body.removeChild(a);
|
96
|
+
} catch (error) {
|
97
|
+
console.error("Export error:", error);
|
98
|
+
}
|
99
|
+
};
|
100
|
+
|
101
|
+
return (
|
102
|
+
<div className="flex flex-col gap-2">
|
103
|
+
<h1 className="text-2xl font-bold text-[rgb(0,137,149)]">
|
104
|
+
<svg
|
105
|
+
xmlns="http://www.w3.org/2000/svg"
|
106
|
+
width="24"
|
107
|
+
height="24"
|
108
|
+
viewBox="0 0 24 24"
|
109
|
+
fill="none"
|
110
|
+
stroke="rgb(0, 137, 149)"
|
111
|
+
strokeWidth="2"
|
112
|
+
strokeLinecap="round"
|
113
|
+
strokeLinejoin="round"
|
114
|
+
className="inline-block mr-2"
|
115
|
+
>
|
116
|
+
<line x1="8" y1="6" x2="21" y2="6" />
|
117
|
+
<line x1="8" y1="12" x2="21" y2="12" />
|
118
|
+
<line x1="8" y1="18" x2="21" y2="18" />
|
119
|
+
<line x1="3" y1="6" x2="3.01" y2="6" />
|
120
|
+
<line x1="3" y1="12" x2="3.01" y2="12" />
|
121
|
+
<line x1="3" y1="18" x2="3.01" y2="18" />
|
122
|
+
</svg>
|
123
|
+
|
124
|
+
{title}
|
125
|
+
<span className="ml-2 px-2 py-1 text-sm bg-gray-100 text-[rgb(0,137,149)] rounded-full">
|
126
|
+
{count}
|
127
|
+
</span>
|
128
|
+
</h1>
|
129
|
+
<div className="border-b border-gray-100 mb-4"></div>
|
130
|
+
<div className="flex gap-4 flex-wrap justify-between">
|
131
|
+
<DatePickerWithRange
|
132
|
+
dateFrom={filters.fromDate}
|
133
|
+
dateTo={filters.toDate}
|
134
|
+
handleDateChange={handleDateChange}
|
135
|
+
/>
|
136
|
+
{configFilters &&
|
137
|
+
configFilters.map((filter: any, index: number) => (
|
138
|
+
<div key={`${filter.name}-${index}`}>
|
139
|
+
{filter.type === "Text" && (
|
140
|
+
<Input
|
141
|
+
type="text"
|
142
|
+
placeholder={filter.placeholder}
|
143
|
+
value={filters[filter.name]}
|
144
|
+
onChange={(e: React.ChangeEvent<HTMLInputElement>) =>
|
145
|
+
setFilters({ ...filters, [filter.name]: e.target.value })
|
146
|
+
}
|
147
|
+
onKeyDown={(e: React.KeyboardEvent<HTMLInputElement>) => {
|
148
|
+
if (e.key === "Enter") {
|
149
|
+
handleSearch();
|
150
|
+
}
|
151
|
+
}}
|
152
|
+
/>
|
153
|
+
)}
|
154
|
+
{filter.type === "Select" && (
|
155
|
+
<Select
|
156
|
+
disabled={filter.disabled || filter.options.length === 0}
|
157
|
+
value={filters[filter.name]}
|
158
|
+
onValueChange={(value: string) => {
|
159
|
+
setFilters({
|
160
|
+
...filters,
|
161
|
+
[filter.name]: value === "_empty" ? undefined : value,
|
162
|
+
});
|
163
|
+
}}
|
164
|
+
>
|
165
|
+
<SelectTrigger>
|
166
|
+
<SelectValue placeholder={filter.placeholder} />
|
167
|
+
</SelectTrigger>
|
168
|
+
<SelectContent>
|
169
|
+
{useMemo(
|
170
|
+
() =>
|
171
|
+
filter.options.map(
|
172
|
+
(
|
173
|
+
option: { VALUE: string; NAME: string },
|
174
|
+
optionIndex: number
|
175
|
+
) => (
|
176
|
+
<SelectItem
|
177
|
+
key={`${option.VALUE}-${optionIndex}`}
|
178
|
+
value={option.VALUE || "_empty"}
|
179
|
+
>
|
180
|
+
{option.NAME}
|
181
|
+
</SelectItem>
|
182
|
+
)
|
183
|
+
),
|
184
|
+
[filter.options]
|
185
|
+
)}
|
186
|
+
</SelectContent>
|
187
|
+
</Select>
|
188
|
+
)}
|
189
|
+
</div>
|
190
|
+
))}
|
191
|
+
<div className="flex gap-2 ml-auto">
|
192
|
+
{configFilters.some((filter: any) => filter.type === "Export") && (
|
193
|
+
<Button
|
194
|
+
disabled={exportDisabled}
|
195
|
+
onClick={async () => {
|
196
|
+
setExportDisabled(true);
|
197
|
+
const exportFilter = configFilters.find(
|
198
|
+
(filter: any) => filter.type === "Export"
|
199
|
+
);
|
200
|
+
await handleExport(exportFilter?.ctx);
|
201
|
+
const timer = setInterval(() => {
|
202
|
+
setExportDisabled(false);
|
203
|
+
clearInterval(timer);
|
204
|
+
}, 30000);
|
205
|
+
}}
|
206
|
+
size="sm"
|
207
|
+
>
|
208
|
+
<svg
|
209
|
+
xmlns="http://www.w3.org/2000/svg"
|
210
|
+
width="16"
|
211
|
+
height="16"
|
212
|
+
viewBox="0 0 24 24"
|
213
|
+
fill="none"
|
214
|
+
stroke="currentColor"
|
215
|
+
strokeWidth="2"
|
216
|
+
strokeLinecap="round"
|
217
|
+
strokeLinejoin="round"
|
218
|
+
>
|
219
|
+
<path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4" />
|
220
|
+
<polyline points="7 10 12 15 17 10" />
|
221
|
+
<line x1="12" y1="15" x2="12" y2="3" />
|
222
|
+
</svg>
|
223
|
+
CSV
|
224
|
+
</Button>
|
225
|
+
)}
|
226
|
+
<Button size="sm" onClick={handleSearch} disabled={isLoading}>
|
227
|
+
{filtersChanged ? (
|
228
|
+
<>
|
229
|
+
<svg
|
230
|
+
xmlns="http://www.w3.org/2000/svg"
|
231
|
+
width="16"
|
232
|
+
height="16"
|
233
|
+
viewBox="0 0 24 24"
|
234
|
+
fill="none"
|
235
|
+
stroke="currentColor"
|
236
|
+
strokeWidth="2"
|
237
|
+
strokeLinecap="round"
|
238
|
+
strokeLinejoin="round"
|
239
|
+
>
|
240
|
+
<circle cx="11" cy="11" r="8" />
|
241
|
+
<path d="m21 21-4.3-4.3" />
|
242
|
+
</svg>
|
243
|
+
Recherche
|
244
|
+
</>
|
245
|
+
) : (
|
246
|
+
<>
|
247
|
+
<svg
|
248
|
+
width="16"
|
249
|
+
height="16"
|
250
|
+
viewBox="0 0 24 24"
|
251
|
+
fill="none"
|
252
|
+
stroke={"currentColor"}
|
253
|
+
strokeWidth="2"
|
254
|
+
strokeLinecap="round"
|
255
|
+
strokeLinejoin="round"
|
256
|
+
className="reload-icon"
|
257
|
+
aria-hidden="true"
|
258
|
+
>
|
259
|
+
<path d="M21 12a9 9 0 11-3-6.74" />
|
260
|
+
<path d="M21 3v6h-6" />
|
261
|
+
</svg>
|
262
|
+
Actualiser
|
263
|
+
</>
|
264
|
+
)}
|
265
|
+
</Button>
|
266
|
+
</div>
|
267
|
+
</div>
|
268
|
+
</div>
|
269
|
+
);
|
270
|
+
}
|
@@ -0,0 +1,59 @@
|
|
1
|
+
import * as React from "react"
|
2
|
+
import { cva, type VariantProps } from "class-variance-authority"
|
3
|
+
|
4
|
+
import { cn } from "../../lib/utils"
|
5
|
+
|
6
|
+
const alertVariants = cva(
|
7
|
+
"relative w-full rounded-lg border px-4 py-3 text-sm [&>svg+div]:translate-y-[-3px] [&>svg]:absolute [&>svg]:left-4 [&>svg]:top-4 [&>svg]:text-foreground [&>svg~*]:pl-7",
|
8
|
+
{
|
9
|
+
variants: {
|
10
|
+
variant: {
|
11
|
+
default: "bg-background text-foreground",
|
12
|
+
destructive:
|
13
|
+
"border-destructive/50 text-destructive dark:border-destructive [&>svg]:text-destructive",
|
14
|
+
},
|
15
|
+
},
|
16
|
+
defaultVariants: {
|
17
|
+
variant: "default",
|
18
|
+
},
|
19
|
+
}
|
20
|
+
)
|
21
|
+
|
22
|
+
const Alert = React.forwardRef<
|
23
|
+
HTMLDivElement,
|
24
|
+
React.HTMLAttributes<HTMLDivElement> & VariantProps<typeof alertVariants>
|
25
|
+
>(({ className, variant, ...props }, ref) => (
|
26
|
+
<div
|
27
|
+
ref={ref}
|
28
|
+
role="alert"
|
29
|
+
className={cn(alertVariants({ variant }), className)}
|
30
|
+
{...props}
|
31
|
+
/>
|
32
|
+
))
|
33
|
+
Alert.displayName = "Alert"
|
34
|
+
|
35
|
+
const AlertTitle = React.forwardRef<
|
36
|
+
HTMLParagraphElement,
|
37
|
+
React.HTMLAttributes<HTMLHeadingElement>
|
38
|
+
>(({ className, ...props }, ref) => (
|
39
|
+
<h5
|
40
|
+
ref={ref}
|
41
|
+
className={cn("mb-1 font-medium leading-none tracking-tight", className)}
|
42
|
+
{...props}
|
43
|
+
/>
|
44
|
+
))
|
45
|
+
AlertTitle.displayName = "AlertTitle"
|
46
|
+
|
47
|
+
const AlertDescription = React.forwardRef<
|
48
|
+
HTMLParagraphElement,
|
49
|
+
React.HTMLAttributes<HTMLParagraphElement>
|
50
|
+
>(({ className, ...props }, ref) => (
|
51
|
+
<div
|
52
|
+
ref={ref}
|
53
|
+
className={cn("text-sm [&_p]:leading-relaxed", className)}
|
54
|
+
{...props}
|
55
|
+
/>
|
56
|
+
))
|
57
|
+
AlertDescription.displayName = "AlertDescription"
|
58
|
+
|
59
|
+
export { Alert, AlertTitle, AlertDescription }
|
@@ -0,0 +1,57 @@
|
|
1
|
+
import * as React from "react"
|
2
|
+
import { Slot } from "@radix-ui/react-slot"
|
3
|
+
import { cva, type VariantProps } from "class-variance-authority"
|
4
|
+
|
5
|
+
import { cn } from "../../lib/utils"
|
6
|
+
|
7
|
+
const buttonVariants = cva(
|
8
|
+
"inline-flex items-center justify-center gap-2 whitespace-nowrap rounded-md text-sm font-medium transition-colors focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:pointer-events-none disabled:opacity-50 [&_svg]:pointer-events-none [&_svg]:size-4 [&_svg]:shrink-0",
|
9
|
+
{
|
10
|
+
variants: {
|
11
|
+
variant: {
|
12
|
+
default:
|
13
|
+
"bg-primary text-primary-foreground shadow hover:bg-primary/90",
|
14
|
+
destructive:
|
15
|
+
"bg-destructive text-destructive-foreground shadow-sm hover:bg-destructive/90",
|
16
|
+
outline:
|
17
|
+
"border border-input bg-background shadow-sm hover:bg-accent hover:text-accent-foreground",
|
18
|
+
secondary:
|
19
|
+
"bg-secondary text-secondary-foreground shadow-sm hover:bg-secondary/80",
|
20
|
+
ghost: "hover:bg-accent hover:text-accent-foreground",
|
21
|
+
link: "text-primary underline-offset-4 hover:underline",
|
22
|
+
},
|
23
|
+
size: {
|
24
|
+
default: "h-9 px-4 py-2",
|
25
|
+
sm: "h-8 rounded-md px-3 text-xs",
|
26
|
+
lg: "h-10 rounded-md px-8",
|
27
|
+
icon: "h-9 w-9",
|
28
|
+
},
|
29
|
+
},
|
30
|
+
defaultVariants: {
|
31
|
+
variant: "default",
|
32
|
+
size: "default",
|
33
|
+
},
|
34
|
+
}
|
35
|
+
)
|
36
|
+
|
37
|
+
export interface ButtonProps
|
38
|
+
extends React.ButtonHTMLAttributes<HTMLButtonElement>,
|
39
|
+
VariantProps<typeof buttonVariants> {
|
40
|
+
asChild?: boolean
|
41
|
+
}
|
42
|
+
|
43
|
+
const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(
|
44
|
+
({ className, variant, size, asChild = false, ...props }, ref) => {
|
45
|
+
const Comp = asChild ? Slot : "button"
|
46
|
+
return (
|
47
|
+
<Comp
|
48
|
+
className={cn(buttonVariants({ variant, size, className }))}
|
49
|
+
ref={ref}
|
50
|
+
{...props}
|
51
|
+
/>
|
52
|
+
)
|
53
|
+
}
|
54
|
+
)
|
55
|
+
Button.displayName = "Button"
|
56
|
+
|
57
|
+
export { Button, buttonVariants }
|
@@ -0,0 +1,70 @@
|
|
1
|
+
import * as React from "react"
|
2
|
+
import { ChevronLeft, ChevronRight } from "lucide-react"
|
3
|
+
import { DayPicker } from "react-day-picker"
|
4
|
+
|
5
|
+
import { cn } from "../../lib/utils"
|
6
|
+
import { buttonVariants } from "./button"
|
7
|
+
|
8
|
+
export type CalendarProps = React.ComponentProps<typeof DayPicker>
|
9
|
+
|
10
|
+
function Calendar({
|
11
|
+
className,
|
12
|
+
classNames,
|
13
|
+
showOutsideDays = true,
|
14
|
+
...props
|
15
|
+
}: CalendarProps) {
|
16
|
+
return (
|
17
|
+
<DayPicker
|
18
|
+
showOutsideDays={showOutsideDays}
|
19
|
+
className={cn("p-3", className)}
|
20
|
+
classNames={{
|
21
|
+
months: "flex flex-col sm:flex-row space-y-4 sm:space-x-4 sm:space-y-0",
|
22
|
+
month: "space-y-4",
|
23
|
+
caption: "flex justify-center pt-1 relative items-center",
|
24
|
+
caption_label: "text-sm font-medium",
|
25
|
+
nav: "space-x-1 flex items-center",
|
26
|
+
nav_button: cn(
|
27
|
+
buttonVariants({ variant: "outline" }),
|
28
|
+
"h-7 w-7 bg-transparent p-0 opacity-50 hover:opacity-100"
|
29
|
+
),
|
30
|
+
nav_button_previous: "absolute left-1",
|
31
|
+
nav_button_next: "absolute right-1",
|
32
|
+
table: "w-full border-collapse space-y-1",
|
33
|
+
head_row: "flex",
|
34
|
+
head_cell:
|
35
|
+
"text-muted-foreground rounded-md w-8 font-normal text-[0.8rem]",
|
36
|
+
row: "flex w-full mt-2",
|
37
|
+
cell: cn(
|
38
|
+
"relative p-0 text-center text-sm focus-within:relative focus-within:z-20 [&:has([aria-selected])]:bg-accent [&:has([aria-selected].day-outside)]:bg-accent/50 [&:has([aria-selected].day-range-end)]:rounded-r-md",
|
39
|
+
props.mode === "range"
|
40
|
+
? "[&:has(>.day-range-end)]:rounded-r-md [&:has(>.day-range-start)]:rounded-l-md first:[&:has([aria-selected])]:rounded-l-md last:[&:has([aria-selected])]:rounded-r-md"
|
41
|
+
: "[&:has([aria-selected])]:rounded-md"
|
42
|
+
),
|
43
|
+
day: cn(
|
44
|
+
buttonVariants({ variant: "ghost" }),
|
45
|
+
"h-8 w-8 p-0 font-normal aria-selected:opacity-100"
|
46
|
+
),
|
47
|
+
day_range_start: "day-range-start",
|
48
|
+
day_range_end: "day-range-end",
|
49
|
+
day_selected:
|
50
|
+
"bg-primary text-primary-foreground hover:bg-primary hover:text-primary-foreground focus:bg-primary focus:text-primary-foreground",
|
51
|
+
day_today: "bg-accent text-accent-foreground",
|
52
|
+
day_outside:
|
53
|
+
"day-outside text-muted-foreground aria-selected:bg-accent/50 aria-selected:text-muted-foreground",
|
54
|
+
day_disabled: "text-muted-foreground opacity-50",
|
55
|
+
day_range_middle:
|
56
|
+
"aria-selected:bg-accent aria-selected:text-accent-foreground",
|
57
|
+
day_hidden: "invisible",
|
58
|
+
...classNames,
|
59
|
+
}}
|
60
|
+
components={{
|
61
|
+
IconLeft: () => <ChevronLeft className="h-4 w-4" />,
|
62
|
+
IconRight: () => <ChevronRight className="h-4 w-4" />,
|
63
|
+
}}
|
64
|
+
{...props}
|
65
|
+
/>
|
66
|
+
)
|
67
|
+
}
|
68
|
+
Calendar.displayName = "Calendar"
|
69
|
+
|
70
|
+
export { Calendar }
|
@@ -0,0 +1,167 @@
|
|
1
|
+
import * as React from "react";
|
2
|
+
import {
|
3
|
+
addDays,
|
4
|
+
startOfWeek,
|
5
|
+
endOfWeek,
|
6
|
+
startOfMonth,
|
7
|
+
endOfMonth,
|
8
|
+
subMonths,
|
9
|
+
format,
|
10
|
+
subYears,
|
11
|
+
startOfYear,
|
12
|
+
endOfYear,
|
13
|
+
} from "date-fns";
|
14
|
+
import { CalendarIcon } from "lucide-react";
|
15
|
+
import { DateRange } from "react-day-picker";
|
16
|
+
|
17
|
+
import { cn } from "../../lib/utils";
|
18
|
+
import { Button } from "../../components/ui/button";
|
19
|
+
import { Calendar } from "../../components/ui/calendar";
|
20
|
+
import {
|
21
|
+
Popover,
|
22
|
+
PopoverContent,
|
23
|
+
PopoverTrigger,
|
24
|
+
} from "../../components/ui/popover";
|
25
|
+
import {
|
26
|
+
Select,
|
27
|
+
SelectContent,
|
28
|
+
SelectItem,
|
29
|
+
SelectTrigger,
|
30
|
+
SelectValue,
|
31
|
+
} from "../../components/ui/select";
|
32
|
+
|
33
|
+
export function DatePickerWithRange({
|
34
|
+
className,
|
35
|
+
dateFrom,
|
36
|
+
dateTo,
|
37
|
+
handleDateChange,
|
38
|
+
}: React.HTMLAttributes<HTMLDivElement> & {
|
39
|
+
dateFrom?: Date;
|
40
|
+
dateTo?: Date;
|
41
|
+
handleDateChange?: (range: DateRange | undefined) => void;
|
42
|
+
}) {
|
43
|
+
const [date, setDate] = React.useState<DateRange | undefined>({
|
44
|
+
from: dateFrom || new Date(2022, 0, 20),
|
45
|
+
to: dateTo || addDays(new Date(2022, 0, 20), 20),
|
46
|
+
});
|
47
|
+
|
48
|
+
const presets = [
|
49
|
+
{
|
50
|
+
label: "Hier",
|
51
|
+
getValue: () => {
|
52
|
+
const yesterday = new Date();
|
53
|
+
yesterday.setDate(yesterday.getDate() - 1);
|
54
|
+
return { from: yesterday, to: yesterday };
|
55
|
+
},
|
56
|
+
},
|
57
|
+
{
|
58
|
+
label: "Cette semaine",
|
59
|
+
getValue: () => {
|
60
|
+
const today = new Date();
|
61
|
+
return { from: startOfWeek(today), to: endOfWeek(today) };
|
62
|
+
},
|
63
|
+
},
|
64
|
+
{
|
65
|
+
label: "Ce mois-ci",
|
66
|
+
getValue: () => {
|
67
|
+
const today = new Date();
|
68
|
+
return { from: startOfMonth(today), to: endOfMonth(today) };
|
69
|
+
},
|
70
|
+
},
|
71
|
+
{
|
72
|
+
label: "Le mois dernier",
|
73
|
+
getValue: () => {
|
74
|
+
const today = new Date();
|
75
|
+
const lastMonth = subMonths(today, 1);
|
76
|
+
return { from: startOfMonth(lastMonth), to: endOfMonth(lastMonth) };
|
77
|
+
},
|
78
|
+
},
|
79
|
+
{
|
80
|
+
label: "Cette année",
|
81
|
+
getValue: () => {
|
82
|
+
const today = new Date();
|
83
|
+
return { from: startOfYear(today), to: endOfYear(today) };
|
84
|
+
},
|
85
|
+
},
|
86
|
+
{
|
87
|
+
label: "L'année dernière",
|
88
|
+
getValue: () => {
|
89
|
+
const today = new Date();
|
90
|
+
const lastYear = subYears(today, 1);
|
91
|
+
return { from: startOfYear(lastYear), to: endOfYear(lastYear) };
|
92
|
+
},
|
93
|
+
},
|
94
|
+
];
|
95
|
+
|
96
|
+
const handleSelect = (newDate: DateRange | undefined) => {
|
97
|
+
setDate(newDate);
|
98
|
+
handleDateChange?.(newDate);
|
99
|
+
};
|
100
|
+
|
101
|
+
return (
|
102
|
+
<div className={cn("grid gap-2", className)}>
|
103
|
+
<Popover>
|
104
|
+
<PopoverTrigger asChild>
|
105
|
+
<Button
|
106
|
+
id="date"
|
107
|
+
variant={"outline"}
|
108
|
+
className={cn(
|
109
|
+
"w-[300px] justify-start text-left font-normal",
|
110
|
+
!date && "text-muted-foreground"
|
111
|
+
)}
|
112
|
+
>
|
113
|
+
<CalendarIcon className="mr-2 h-4 w-4" />
|
114
|
+
{date?.from ? (
|
115
|
+
date.to ? (
|
116
|
+
<>
|
117
|
+
{format(date.from, "LLL dd, y")} -{" "}
|
118
|
+
{format(date.to, "LLL dd, y")}
|
119
|
+
</>
|
120
|
+
) : (
|
121
|
+
format(date.from, "LLL dd, y")
|
122
|
+
)
|
123
|
+
) : (
|
124
|
+
<span>Choisissez une date</span>
|
125
|
+
)}
|
126
|
+
</Button>
|
127
|
+
</PopoverTrigger>
|
128
|
+
<PopoverContent
|
129
|
+
className="flex w-auto flex-col space-y-4 p-4"
|
130
|
+
align="start"
|
131
|
+
>
|
132
|
+
<Select
|
133
|
+
onValueChange={(value) => {
|
134
|
+
const preset = presets.find((preset) => preset.label === value);
|
135
|
+
if (preset) {
|
136
|
+
const newDate = preset.getValue();
|
137
|
+
setDate(newDate);
|
138
|
+
handleDateChange?.(newDate);
|
139
|
+
}
|
140
|
+
}}
|
141
|
+
>
|
142
|
+
<SelectTrigger className="w-[300px]">
|
143
|
+
<SelectValue placeholder="Sélectionnez un préréglage" />
|
144
|
+
</SelectTrigger>
|
145
|
+
<SelectContent position="popper">
|
146
|
+
{presets.map((preset) => (
|
147
|
+
<SelectItem key={preset.label} value={preset.label}>
|
148
|
+
{preset.label}
|
149
|
+
</SelectItem>
|
150
|
+
))}
|
151
|
+
</SelectContent>
|
152
|
+
</Select>
|
153
|
+
<div className="rounded-md border">
|
154
|
+
<Calendar
|
155
|
+
initialFocus
|
156
|
+
mode="range"
|
157
|
+
defaultMonth={date?.from}
|
158
|
+
selected={date}
|
159
|
+
onSelect={handleSelect}
|
160
|
+
numberOfMonths={2}
|
161
|
+
/>
|
162
|
+
</div>
|
163
|
+
</PopoverContent>
|
164
|
+
</Popover>
|
165
|
+
</div>
|
166
|
+
);
|
167
|
+
}
|