SearchDateRangePicker.jsx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. "use client";
  2. import * as React from "react";
  3. import dynamic from "next/dynamic";
  4. import { Calendar as CalendarIcon, X } from "lucide-react";
  5. import { cn } from "@/lib/utils";
  6. import { Badge } from "@/components/ui/badge";
  7. import { Button } from "@/components/ui/button";
  8. import { Input } from "@/components/ui/input";
  9. import { Label } from "@/components/ui/label";
  10. import {
  11. Popover,
  12. PopoverContent,
  13. PopoverTrigger,
  14. } from "@/components/ui/popover";
  15. import { Skeleton } from "@/components/ui/skeleton";
  16. import { useSearchDateRangePicker } from "@/lib/frontend/search/useSearchDateRangePicker";
  17. function CalendarLoading() {
  18. return (
  19. <div className="space-y-3">
  20. <div className="flex gap-4">
  21. <Skeleton className="h-72 w-72" />
  22. <Skeleton className="h-72 w-72" />
  23. </div>
  24. <p className="text-xs text-muted-foreground text-center">
  25. Kalender lädt…
  26. </p>
  27. </div>
  28. );
  29. }
  30. const Calendar = dynamic(
  31. () => import("@/components/ui/calendar").then((m) => m.Calendar),
  32. {
  33. ssr: false,
  34. loading: CalendarLoading,
  35. },
  36. );
  37. // Preset chips:
  38. // - Light mode: black background + white text (high contrast)
  39. // - Dark mode: keep white pills (reads well on dark surfaces)
  40. const PRESET_BADGE_CLASS = [
  41. "bg-black text-white border-black",
  42. "hover:bg-black/90",
  43. "dark:bg-white dark:text-black dark:border-border",
  44. "dark:hover:bg-white/90",
  45. ].join(" ");
  46. export default function SearchDateRangePicker({
  47. from,
  48. to,
  49. onDateRangeChange,
  50. isSubmitting,
  51. className,
  52. }) {
  53. const {
  54. disabled,
  55. open,
  56. setOpen,
  57. activeField,
  58. setActiveField,
  59. fromRef,
  60. toRef,
  61. month,
  62. setMonth,
  63. summary,
  64. fromDisplay,
  65. toDisplay,
  66. presetsRow1,
  67. presetsRow2,
  68. calendarKey,
  69. calendarSelected,
  70. calendarModifiers,
  71. calendarModifiersClassNames,
  72. isRangeInvalid,
  73. handlePickDay,
  74. handleClearFrom,
  75. handleClearTo,
  76. handleReset,
  77. applyPreset,
  78. } = useSearchDateRangePicker({
  79. from,
  80. to,
  81. onDateRangeChange,
  82. isSubmitting,
  83. });
  84. const fromInputId = React.useId();
  85. const toInputId = React.useId();
  86. const activeInputClass =
  87. "border-blue-600 bg-blue-50 dark:border-blue-900 dark:bg-blue-950";
  88. return (
  89. <div className={cn("grid gap-2", className)}>
  90. <Label>Zeitraum</Label>
  91. <Popover open={open} onOpenChange={setOpen}>
  92. <PopoverTrigger asChild>
  93. <Button
  94. type="button"
  95. variant="outline"
  96. disabled={disabled}
  97. className={cn(
  98. "w-60 justify-between font-normal",
  99. !from && !to ? "text-muted-foreground" : "",
  100. )}
  101. title="Zeitraum auswählen"
  102. >
  103. <span className="truncate">{summary}</span>
  104. <CalendarIcon className="ml-2 h-4 w-4 opacity-70" />
  105. </Button>
  106. </PopoverTrigger>
  107. <PopoverContent align="start" className="w-fit p-0">
  108. <div className="w-fit space-y-4 p-4">
  109. <div className="grid grid-cols-2 gap-4">
  110. <div className="space-y-1">
  111. <Label htmlFor={fromInputId}>Von</Label>
  112. <div className="relative">
  113. <Input
  114. id={fromInputId}
  115. ref={fromRef}
  116. readOnly
  117. disabled={disabled}
  118. value={fromDisplay}
  119. placeholder="TT.MM.JJJJ"
  120. className={cn(
  121. "pr-8",
  122. activeField === "from" ? activeInputClass : "",
  123. )}
  124. onFocus={() => setActiveField("from")}
  125. onClick={() => setActiveField("from")}
  126. />
  127. {from ? (
  128. <button
  129. type="button"
  130. className="absolute right-2 top-1/2 -translate-y-1/2 rounded-sm p-1 opacity-70 hover:opacity-100"
  131. onClick={handleClearFrom}
  132. aria-label="Startdatum löschen"
  133. title="Startdatum löschen"
  134. >
  135. <X className="h-4 w-4" />
  136. </button>
  137. ) : null}
  138. </div>
  139. </div>
  140. <div className="space-y-1">
  141. <Label htmlFor={toInputId}>Bis</Label>
  142. <div className="relative">
  143. <Input
  144. id={toInputId}
  145. ref={toRef}
  146. readOnly
  147. disabled={disabled}
  148. value={toDisplay}
  149. placeholder="TT.MM.JJJJ"
  150. className={cn(
  151. "pr-8",
  152. activeField === "to" ? activeInputClass : "",
  153. )}
  154. onFocus={() => setActiveField("to")}
  155. onClick={() => setActiveField("to")}
  156. />
  157. {to ? (
  158. <button
  159. type="button"
  160. className="absolute right-2 top-1/2 -translate-y-1/2 rounded-sm p-1 opacity-70 hover:opacity-100"
  161. onClick={handleClearTo}
  162. aria-label="Enddatum löschen"
  163. title="Enddatum löschen"
  164. >
  165. <X className="h-4 w-4" />
  166. </button>
  167. ) : null}
  168. </div>
  169. </div>
  170. </div>
  171. <Calendar
  172. key={calendarKey}
  173. mode="range"
  174. numberOfMonths={2}
  175. captionLayout="dropdown"
  176. month={month}
  177. onMonthChange={setMonth}
  178. selected={calendarSelected}
  179. modifiers={calendarModifiers}
  180. modifiersClassNames={calendarModifiersClassNames}
  181. onDayClick={handlePickDay}
  182. />
  183. {isRangeInvalid ? (
  184. <p className="text-xs text-destructive text-center">
  185. Das Startdatum darf nicht nach dem Enddatum liegen.
  186. </p>
  187. ) : null}
  188. <div className="space-y-2">
  189. <div className="text-sm text-muted-foreground">Schnellwahl</div>
  190. <div className="flex flex-wrap gap-2">
  191. {presetsRow1.map((p) => (
  192. <Badge key={p.key} asChild className={PRESET_BADGE_CLASS}>
  193. <button
  194. type="button"
  195. className="cursor-pointer select-none disabled:opacity-60"
  196. disabled={disabled}
  197. onClick={() => applyPreset(p)}
  198. title={p.label}
  199. >
  200. {p.label}
  201. </button>
  202. </Badge>
  203. ))}
  204. </div>
  205. <div className="flex flex-wrap gap-2">
  206. {presetsRow2.map((p) => (
  207. <Badge key={p.key} asChild className={PRESET_BADGE_CLASS}>
  208. <button
  209. type="button"
  210. className="cursor-pointer select-none disabled:opacity-60"
  211. disabled={disabled}
  212. onClick={() => applyPreset(p)}
  213. title={p.label}
  214. >
  215. {p.label}
  216. </button>
  217. </Badge>
  218. ))}
  219. </div>
  220. </div>
  221. <div className="flex justify-end">
  222. <Button
  223. type="button"
  224. variant="outline"
  225. disabled={disabled}
  226. onClick={handleReset}
  227. >
  228. Zurücksetzen
  229. </Button>
  230. </div>
  231. <p className="text-xs text-muted-foreground text-center">
  232. Tipp: Für einen einzelnen Tag setzen Sie <b>Von</b> und <b>Bis</b>{" "}
  233. auf dasselbe Datum.
  234. </p>
  235. </div>
  236. </PopoverContent>
  237. </Popover>
  238. </div>
  239. );
  240. }