@react-fabric/date 1.0.0-beta.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.
Files changed (105) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +10 -0
  3. package/dist/cjs/components/Date.module.css +1 -0
  4. package/dist/cjs/components/DateCell.js +98 -0
  5. package/dist/cjs/components/DateHeader.js +142 -0
  6. package/dist/cjs/components/DatePage.js +59 -0
  7. package/dist/cjs/components/DecadePage.js +34 -0
  8. package/dist/cjs/components/MonthPage.js +33 -0
  9. package/dist/cjs/components/TimeSliders.js +131 -0
  10. package/dist/cjs/components/YearPage.js +34 -0
  11. package/dist/cjs/display/DateDisplay.js +35 -0
  12. package/dist/cjs/hijri/index.js +1029 -0
  13. package/dist/cjs/i18n/ar.json.js +25 -0
  14. package/dist/cjs/i18n/en.json.js +21 -0
  15. package/dist/cjs/i18n/index.js +39 -0
  16. package/dist/cjs/i18n/superdate/ar.json.js +86 -0
  17. package/dist/cjs/i18n/superdate/en.json.js +93 -0
  18. package/dist/cjs/index.js +20 -0
  19. package/dist/cjs/panels/BasePanel.js +73 -0
  20. package/dist/cjs/panels/DatePanel.js +205 -0
  21. package/dist/cjs/panels/RangePanel.js +340 -0
  22. package/dist/cjs/superdate/EventPanel.js +173 -0
  23. package/dist/cjs/superdate/QuickSelect.js +134 -0
  24. package/dist/cjs/superdate/RelativeInput.js +79 -0
  25. package/dist/cjs/superdate/RelativePanel.js +91 -0
  26. package/dist/cjs/superdate/SuperDate.module.css +1 -0
  27. package/dist/cjs/superdate/Superdate.js +169 -0
  28. package/dist/cjs/types/index.js +61 -0
  29. package/dist/cjs/utils/dateUtil.js +436 -0
  30. package/dist/esm/components/Date.module.css +1 -0
  31. package/dist/esm/components/DateCell.js +98 -0
  32. package/dist/esm/components/DateHeader.js +142 -0
  33. package/dist/esm/components/DatePage.js +59 -0
  34. package/dist/esm/components/DecadePage.js +34 -0
  35. package/dist/esm/components/MonthPage.js +33 -0
  36. package/dist/esm/components/TimeSliders.js +131 -0
  37. package/dist/esm/components/YearPage.js +34 -0
  38. package/dist/esm/display/DateDisplay.js +35 -0
  39. package/dist/esm/hijri/index.js +1029 -0
  40. package/dist/esm/i18n/ar.json.js +21 -0
  41. package/dist/esm/i18n/en.json.js +18 -0
  42. package/dist/esm/i18n/index.js +39 -0
  43. package/dist/esm/i18n/superdate/ar.json.js +79 -0
  44. package/dist/esm/i18n/superdate/en.json.js +85 -0
  45. package/dist/esm/index.js +9 -0
  46. package/dist/esm/panels/BasePanel.js +73 -0
  47. package/dist/esm/panels/DatePanel.js +205 -0
  48. package/dist/esm/panels/RangePanel.js +340 -0
  49. package/dist/esm/superdate/EventPanel.js +173 -0
  50. package/dist/esm/superdate/QuickSelect.js +134 -0
  51. package/dist/esm/superdate/RelativeInput.js +79 -0
  52. package/dist/esm/superdate/RelativePanel.js +91 -0
  53. package/dist/esm/superdate/SuperDate.module.css +1 -0
  54. package/dist/esm/superdate/Superdate.js +168 -0
  55. package/dist/esm/types/index.js +60 -0
  56. package/dist/esm/utils/dateUtil.js +438 -0
  57. package/dist/types/components/DateCell.d.ts +15 -0
  58. package/dist/types/components/DateHeader.d.ts +14 -0
  59. package/dist/types/components/DatePage.d.ts +6 -0
  60. package/dist/types/components/DecadePage.d.ts +6 -0
  61. package/dist/types/components/MonthPage.d.ts +6 -0
  62. package/dist/types/components/TimeSliders.d.ts +10 -0
  63. package/dist/types/components/YearPage.d.ts +6 -0
  64. package/dist/types/display/DateDisplay.d.ts +15 -0
  65. package/dist/types/hijri/index.d.ts +2 -0
  66. package/dist/types/i18n/index.d.ts +1 -0
  67. package/dist/types/index.d.ts +7 -0
  68. package/dist/types/panels/BasePanel.d.ts +25 -0
  69. package/dist/types/panels/DatePanel.d.ts +2 -0
  70. package/dist/types/panels/RangePanel.d.ts +2 -0
  71. package/dist/types/superdate/EventPanel.d.ts +4 -0
  72. package/dist/types/superdate/QuickSelect.d.ts +1 -0
  73. package/dist/types/superdate/RelativeInput.d.ts +1 -0
  74. package/dist/types/superdate/RelativePanel.d.ts +1 -0
  75. package/dist/types/superdate/Superdate.d.ts +17 -0
  76. package/dist/types/types/index.d.ts +70 -0
  77. package/dist/types/utils/dateUtil.d.ts +30 -0
  78. package/package.json +58 -0
  79. package/src/components/Date.module.css +75 -0
  80. package/src/components/DateCell.tsx +181 -0
  81. package/src/components/DateHeader.tsx +223 -0
  82. package/src/components/DatePage.tsx +107 -0
  83. package/src/components/DecadePage.tsx +53 -0
  84. package/src/components/MonthPage.tsx +52 -0
  85. package/src/components/TimeSliders.tsx +196 -0
  86. package/src/components/YearPage.tsx +53 -0
  87. package/src/display/DateDisplay.tsx +88 -0
  88. package/src/hijri/index.js +1540 -0
  89. package/src/i18n/ar.json +14 -0
  90. package/src/i18n/en.json +13 -0
  91. package/src/i18n/index.ts +31 -0
  92. package/src/i18n/superdate/ar.json +70 -0
  93. package/src/i18n/superdate/en.json +75 -0
  94. package/src/index.ts +32 -0
  95. package/src/panels/BasePanel.tsx +130 -0
  96. package/src/panels/DatePanel.tsx +235 -0
  97. package/src/panels/RangePanel.tsx +400 -0
  98. package/src/superdate/EventPanel.tsx +200 -0
  99. package/src/superdate/QuickSelect.tsx +121 -0
  100. package/src/superdate/RelativeInput.tsx +64 -0
  101. package/src/superdate/RelativePanel.tsx +103 -0
  102. package/src/superdate/SuperDate.module.css +30 -0
  103. package/src/superdate/Superdate.tsx +186 -0
  104. package/src/types/index.ts +121 -0
  105. package/src/utils/dateUtil.ts +551 -0
@@ -0,0 +1,14 @@
1
+ {
2
+ "label": {
3
+ "prevMonth": "الشهر الماضى",
4
+ "prevYear": "السنة الماضية",
5
+ "prevDecade": "العقد السابق",
6
+ "nextMonth": "الشهر القادم",
7
+ "nextYear": "العام القادم",
8
+ "nextDecade": "العقد القادم",
9
+ "today": "اليوم",
10
+ "hijri": "هجري",
11
+ "gregorian": "ميلادي"
12
+ },
13
+ "number": {}
14
+ }
@@ -0,0 +1,13 @@
1
+ {
2
+ "label": {
3
+ "prevMonth": "Previous Month",
4
+ "prevYear": "Previous Year",
5
+ "prevDecade": "Previous Decade",
6
+ "nextMonth": "Next Month",
7
+ "nextYear": "Next Year",
8
+ "nextDecade": "Next Decade",
9
+ "today": "Today",
10
+ "hijri": "Hijri",
11
+ "gregorian": "Gregorian"
12
+ }
13
+ }
@@ -0,0 +1,31 @@
1
+ /*
2
+ * React Fabric
3
+ * @version: 1.0.0
4
+ *
5
+ *
6
+ * The MIT License (MIT)
7
+ * Copyright (c) 2024 Adarsh Pastakia
8
+ *
9
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
10
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
11
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
12
+ * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
13
+ * subject to the following conditions:
14
+ *
15
+ * The above copyright notice and this permission notice shall be included in all copies or substantial
16
+ * portions of the Software.
17
+ *
18
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
19
+ * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ */
23
+
24
+ import { addTranslationBundle } from "@react-fabric/core";
25
+ import ar from "./ar.json";
26
+ import en from "./en.json";
27
+ import arSuper from "./superdate/ar.json";
28
+ import enSuper from "./superdate/en.json";
29
+
30
+ addTranslationBundle("date", { en, ar });
31
+ addTranslationBundle("superdate", { en: enSuper, ar: arSuper });
@@ -0,0 +1,70 @@
1
+ {
2
+ "separator": " ⇽ ",
3
+ "label": {
4
+ "quick": "حدد مسرعا",
5
+ "relative": "نسبيا",
6
+ "events": "الأحداث",
7
+ "absolute": "مطلق",
8
+ "preset": "المسبقة",
9
+ "current": "تيار",
10
+ "previous": "السابق",
11
+ "from": "من عند",
12
+ "to": "إلى",
13
+ "+": "من الان",
14
+ "-": "منذ",
15
+ "apply": "يتقدم",
16
+ "$now": "الآن",
17
+ "$minute": "دقيقة",
18
+ "$hour": "ساعة",
19
+ "$day": "يوم",
20
+ "$week": "أسبوع",
21
+ "$month": "شهر",
22
+ "$quarter": "ربع",
23
+ "$year": "عام",
24
+ "$decade": "عقد"
25
+ },
26
+ "now": {
27
+ "$day": "اليوم",
28
+ "$week": "هذا الاسبوع",
29
+ "$month": "هذا الشهر",
30
+ "$quarter": "هذا الربع",
31
+ "$year": "هذه السنة",
32
+ "$decade": "هذا العقد"
33
+ },
34
+ "next": {
35
+ "$day": "غدا",
36
+ "$week": "الاسبوع القادم",
37
+ "$month": "الشهر القادم",
38
+ "$quarter": "الربع القادم",
39
+ "$year": "العام القادم",
40
+ "$decade": "العقد القادم",
41
+ "$minute": "بعد دقيقة",
42
+ "$hour": "بعد ساعة",
43
+ "$minute_plural": "بعد {{count}} دقائق",
44
+ "$hour_plural": "بعد {{count}} ساعات",
45
+ "$day_plural": "بعد {{count}} أيام",
46
+ "$week_plural": "بعد {{count}} أسابيع",
47
+ "$month_plural": "بعد {{count}} أشهر",
48
+ "$quarter_plural": "بعد {{count}} أرباع",
49
+ "$year_plural": "بعد {{count}} سنوات",
50
+ "$decade_plural": "بعد {{count}} عقود"
51
+ },
52
+ "prev": {
53
+ "$day": "في الامس",
54
+ "$week": "الاسبوع الماضي",
55
+ "$month": "الشهر الماضي",
56
+ "$quarter": "الربع الأخير",
57
+ "$year": "العام الماضي",
58
+ "$decade": "العقد الماضي",
59
+ "$minute": "منذ دقيقة",
60
+ "$hour": "منذ ساعة",
61
+ "$minute_plural": "منذ {{count}} دقيقة",
62
+ "$hour_plural": "منذ {{count}} ساعات",
63
+ "$day_plural": "منذ {{count}} أيام",
64
+ "$week_plural": "من {{count}} اسابيع",
65
+ "$month_plural": "قبل {{count}} أشهر",
66
+ "$quarter_plural": "منذ {{count}} أرباع",
67
+ "$year_plural": "منذ {{count}} سنوات",
68
+ "$decade_plural": "منذ {{count}} عقود"
69
+ }
70
+ }
@@ -0,0 +1,75 @@
1
+ {
2
+ "separator": " ⇾ ",
3
+ "event": {
4
+ "ramadan": "Ramadan",
5
+ "christmas": "Christmas",
6
+ "newYear": "New Year"
7
+ },
8
+ "label": {
9
+ "quick": "Quick Select",
10
+ "relative": "Relative",
11
+ "absolute": "Absolute",
12
+ "events": "Events",
13
+ "preset": "Presets",
14
+ "current": "Current",
15
+ "previous": "Previous",
16
+ "from": "From",
17
+ "to": "To",
18
+ "+": "From now",
19
+ "-": "Ago",
20
+ "apply": "Apply",
21
+ "$now": "Now",
22
+ "$minute": "Minute",
23
+ "$hour": "Hour",
24
+ "$day": "Day",
25
+ "$week": "Week",
26
+ "$month": "Month",
27
+ "$quarter": "Quarter",
28
+ "$year": "Year",
29
+ "$decade": "Decade"
30
+ },
31
+ "now": {
32
+ "$day": "Today",
33
+ "$week": "This week",
34
+ "$month": "This month",
35
+ "$quarter": "This quarter",
36
+ "$year": "This year",
37
+ "$decade": "This decade"
38
+ },
39
+ "next": {
40
+ "$day": "Tomorrow",
41
+ "$week": "Next week",
42
+ "$month": "Next month",
43
+ "$quarter": "Next quarter",
44
+ "$year": "Next year",
45
+ "$decade": "Next decade",
46
+ "$minute": "A minute later",
47
+ "$hour": "An hour later",
48
+ "$minute_other": "{{count}} minutes later",
49
+ "$hour_other": "{{count}} hours later",
50
+ "$day_other": "{{count}} days later",
51
+ "$week_other": "{{count}} weeks later",
52
+ "$month_other": "{{count}} months later",
53
+ "$quarter_other": "{{count}} quarters later",
54
+ "$year_other": "{{count}} years later",
55
+ "$decade_other": "{{count}} decades later"
56
+ },
57
+ "prev": {
58
+ "$day": "Yesterday",
59
+ "$week": "Last week",
60
+ "$month": "Last month",
61
+ "$quarter": "Last quarter",
62
+ "$year": "Last year",
63
+ "$decade": "Last decade",
64
+ "$minute": "A minute ago",
65
+ "$hour": "An hour ago",
66
+ "$minute_other": "{{count}} minutes ago",
67
+ "$hour_other": "{{count}} hours ago",
68
+ "$day_other": "{{count}} days ago",
69
+ "$week_other": "{{count}} weeks ago",
70
+ "$month_other": "{{count}} months ago",
71
+ "$quarter_other": "{{count}} quarters ago",
72
+ "$year_other": "{{count}} years ago",
73
+ "$decade_other": "{{count}} decades ago"
74
+ }
75
+ }
package/src/index.ts ADDED
@@ -0,0 +1,32 @@
1
+ /*
2
+ * React Fabric
3
+ * @version: 1.0.0
4
+ *
5
+ *
6
+ * The MIT License (MIT)
7
+ * Copyright (c) 2024 Adarsh Pastakia
8
+ *
9
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
10
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
11
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
12
+ * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
13
+ * subject to the following conditions:
14
+ *
15
+ * The above copyright notice and this permission notice shall be included in all copies or substantial
16
+ * portions of the Software.
17
+ *
18
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
19
+ * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ */
23
+
24
+ import "./hijri";
25
+ import "./i18n";
26
+
27
+ export { DateDisplay } from "./display/DateDisplay";
28
+ export { DatePanel } from "./panels/DatePanel";
29
+ export { RangePanel } from "./panels/RangePanel";
30
+ export { SuperDate } from "./superdate/Superdate";
31
+
32
+ export { DateUtil } from "./utils/dateUtil";
@@ -0,0 +1,130 @@
1
+ /*
2
+ * React Fabric
3
+ * @version: 1.0.0
4
+ *
5
+ *
6
+ * The MIT License (MIT)
7
+ * Copyright (c) 2024 Adarsh Pastakia
8
+ *
9
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
10
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
11
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
12
+ * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
13
+ * subject to the following conditions:
14
+ *
15
+ * The above copyright notice and this permission notice shall be included in all copies or substantial
16
+ * portions of the Software.
17
+ *
18
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
19
+ * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ */
23
+
24
+ import { DateHeader } from "../components/DateHeader";
25
+ import { DatePage } from "../components/DatePage";
26
+ import { DecadePage } from "../components/DecadePage";
27
+ import { MonthPage } from "../components/MonthPage";
28
+ import { TimeSlider } from "../components/TimeSliders";
29
+ import { YearPage } from "../components/YearPage";
30
+ import { PageType, type DateLike } from "../types";
31
+
32
+ export interface PanelProps {
33
+ page: PageType;
34
+ pageDate: Date;
35
+ selected?: Date | [Date, Date];
36
+ hilight?: [Date, Date];
37
+ hour: number;
38
+ minute: number;
39
+ min?: DateLike;
40
+ max?: DateLike;
41
+ isHijri: boolean;
42
+ withTime?: boolean;
43
+
44
+ hidePrevious?: boolean;
45
+ hideNext?: boolean;
46
+
47
+ changePage: (page: PageType) => void;
48
+ changePageDate: (date: Date, pageChange?: boolean) => void;
49
+ changeTime: (time: { hour: number; minute: number }) => void;
50
+ mouseOver?: (date: Date) => void;
51
+
52
+ selectDate: (date: Date) => void;
53
+ isDateDisabled: (date: Date) => boolean;
54
+ }
55
+
56
+ export const BasePanel = ({
57
+ min,
58
+ max,
59
+ isHijri,
60
+ withTime,
61
+ hideNext,
62
+ hidePrevious,
63
+ changePage,
64
+ changePageDate,
65
+ changeTime,
66
+ selectDate,
67
+ isDateDisabled,
68
+ ...state
69
+ }: PanelProps) => {
70
+ return (
71
+ <div
72
+ className="grid overflow-hidden select-none"
73
+ style={{
74
+ gridTemplate: `"head head" auto "content time" 1fr / 1fr auto`,
75
+ }}
76
+ >
77
+ <DateHeader
78
+ {...state}
79
+ min={min}
80
+ max={max}
81
+ hideNext={hideNext}
82
+ hidePrevious={hidePrevious}
83
+ isHijri={isHijri}
84
+ changePage={changePage}
85
+ changePageDate={(dt) => changePageDate(dt, false)}
86
+ />
87
+ <DatePage
88
+ {...state}
89
+ isHijri={isHijri}
90
+ selectDate={selectDate}
91
+ dateDisabled={isDateDisabled}
92
+ changePageDate={changePageDate}
93
+ />
94
+ {state.page === PageType.MONTH && (
95
+ <MonthPage
96
+ {...state}
97
+ isHijri={isHijri}
98
+ selectDate={selectDate}
99
+ dateDisabled={isDateDisabled}
100
+ changePageDate={changePageDate}
101
+ />
102
+ )}
103
+ {state.page === PageType.YEAR && (
104
+ <YearPage
105
+ {...state}
106
+ isHijri={isHijri}
107
+ selectDate={selectDate}
108
+ dateDisabled={isDateDisabled}
109
+ changePageDate={changePageDate}
110
+ />
111
+ )}
112
+ {state.page === PageType.DECADE && (
113
+ <DecadePage
114
+ {...state}
115
+ isHijri={isHijri}
116
+ selectDate={selectDate}
117
+ dateDisabled={isDateDisabled}
118
+ changePageDate={changePageDate}
119
+ />
120
+ )}
121
+ {withTime && state.page === PageType.DATE && (
122
+ <TimeSlider
123
+ hour={state.hour}
124
+ minute={state.minute}
125
+ onTimeChange={changeTime}
126
+ />
127
+ )}
128
+ </div>
129
+ );
130
+ };
@@ -0,0 +1,235 @@
1
+ /*
2
+ * React Fabric
3
+ * @version: 1.0.0
4
+ *
5
+ *
6
+ * The MIT License (MIT)
7
+ * Copyright (c) 2024 Adarsh Pastakia
8
+ *
9
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
10
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
11
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
12
+ * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
13
+ * subject to the following conditions:
14
+ *
15
+ * The above copyright notice and this permission notice shall be included in all copies or substantial
16
+ * portions of the Software.
17
+ *
18
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
19
+ * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ */
23
+
24
+ import { Button, useDebounce, useGlobals } from "@react-fabric/core";
25
+ import { isArray, isEmpty } from "@react-fabric/utilities";
26
+ import classNames from "classnames";
27
+ import { isAfter, isBefore, isSameDay } from "date-fns";
28
+ import { useCallback, useEffect, useReducer, useState } from "react";
29
+ import { useTranslation } from "react-i18next";
30
+ import { PageType, type DateLike, type DateProps } from "../types";
31
+ import { DateUtil } from "../utils/dateUtil";
32
+ import { BasePanel } from "./BasePanel";
33
+
34
+ interface PanelState {
35
+ page: PageType;
36
+ pageDate: Date;
37
+ selected?: Date;
38
+ hour: number;
39
+ minute: number;
40
+ }
41
+
42
+ type PanelActions =
43
+ | { type: "reset"; date?: DateLike }
44
+ | { type: "changeDate"; date: Date }
45
+ | { type: "changeTime"; hour: number; minute: number }
46
+ | { type: "changePage"; page: PageType }
47
+ | { type: "changePageDate"; date: Date; pageChange: boolean };
48
+
49
+ export const DatePanel = ({
50
+ className,
51
+ max,
52
+ min,
53
+ dateDisabled,
54
+ onChange,
55
+ showHijriToggle,
56
+ value,
57
+ withTime,
58
+ ...rest
59
+ }: DateProps) => {
60
+ const { t } = useTranslation("date");
61
+ const { currentCalendar } = useGlobals();
62
+ const [isHijri, setHijri] = useState(currentCalendar === "hijri");
63
+ const [canSelectToday, setCanSelectToday] = useState(true);
64
+
65
+ const fireChange = useDebounce(onChange, [onChange]);
66
+
67
+ useEffect(() => {
68
+ setHijri(currentCalendar === "hijri");
69
+ }, [currentCalendar]);
70
+
71
+ const [state, dispatch] = useReducer(
72
+ (state: PanelState, action: PanelActions) => {
73
+ if (action.type === "reset") {
74
+ if (!isEmpty(action.date)) {
75
+ const date = new Date(action.date);
76
+ state.selected = date;
77
+ state.pageDate = date;
78
+ state.hour = date.getHours();
79
+ state.minute = date.getMinutes();
80
+ } else {
81
+ state.selected = undefined;
82
+ state.pageDate = new Date();
83
+ state.hour = 0;
84
+ state.minute = 0;
85
+ }
86
+ }
87
+ if (action.type === "changeDate") {
88
+ action.date.setHours(state.hour);
89
+ action.date.setMinutes(state.minute);
90
+ state.selected = action.date;
91
+ state.pageDate = action.date;
92
+ }
93
+ if (action.type === "changeTime") {
94
+ state.selected?.setHours(action.hour);
95
+ state.selected?.setMinutes(action.minute);
96
+ state.hour = action.hour;
97
+ state.minute = action.minute;
98
+ }
99
+ if (action.type === "changePage") {
100
+ state.page = action.page;
101
+ }
102
+ if (action.type === "changePageDate") {
103
+ state.pageDate = action.date;
104
+ action.pageChange && (state.page = Math.max(state.page - 1, 0));
105
+ }
106
+ if (action.type === "changeDate" || action.type === "changeTime") {
107
+ if (min && state.selected && isBefore(state.selected, new Date(min))) {
108
+ state.selected = new Date(min);
109
+ state.hour = state.selected.getHours();
110
+ state.minute = state.selected.getMinutes();
111
+ }
112
+ if (max && state.selected && isAfter(state.selected, new Date(max))) {
113
+ state.selected = new Date(max);
114
+ state.hour = state.selected.getHours();
115
+ state.minute = state.selected.getMinutes();
116
+ }
117
+ state.selected && fireChange?.(state.selected.toISOString());
118
+ }
119
+ return { ...state };
120
+ },
121
+ {
122
+ page: PageType.DATE,
123
+ pageDate: new Date(),
124
+ hour: 0,
125
+ minute: 0,
126
+ } as PanelState,
127
+ );
128
+
129
+ useEffect(() => {
130
+ let canSelect = true;
131
+ if (min && canSelect) {
132
+ canSelect = isAfter(new Date(), min);
133
+ }
134
+ if (max && canSelect) {
135
+ canSelect = isBefore(new Date(), max);
136
+ }
137
+ setCanSelectToday(canSelect);
138
+ }, [min, max]);
139
+
140
+ useEffect(() => {
141
+ dispatch({ type: "reset", date: value });
142
+ }, [value]);
143
+
144
+ const isDateDisabled = useCallback(
145
+ (dt: Date) => {
146
+ if (state.page === PageType.DATE) {
147
+ if (isArray(dateDisabled)) {
148
+ return !!dateDisabled.find((dd) => isSameDay(new Date(dd), dt));
149
+ }
150
+ }
151
+
152
+ return DateUtil.isDisabled(dt, state.page, min, max);
153
+ },
154
+ [state.page, min, max, dateDisabled],
155
+ );
156
+
157
+ const changePageDate = useCallback((date: Date, pageChange = true) => {
158
+ dispatch({
159
+ type: "changePageDate",
160
+ pageChange,
161
+ date,
162
+ });
163
+ }, []);
164
+
165
+ const selectDate = useCallback((date: Date) => {
166
+ dispatch({
167
+ type: "changeDate",
168
+ date,
169
+ });
170
+ }, []);
171
+
172
+ return (
173
+ <div
174
+ className={classNames(
175
+ "block overflow-hidden bg-base rounded-capped",
176
+ className,
177
+ )}
178
+ {...rest}
179
+ >
180
+ <BasePanel
181
+ {...state}
182
+ max={max}
183
+ min={min}
184
+ withTime={withTime}
185
+ isHijri={isHijri}
186
+ selectDate={selectDate}
187
+ isDateDisabled={isDateDisabled}
188
+ changeTime={(time) => dispatch({ type: "changeTime", ...time })}
189
+ changePage={(page) => dispatch({ type: "changePage", page })}
190
+ changePageDate={changePageDate}
191
+ />
192
+ <div className="border-t flex flex-nowrap justify-between px-2">
193
+ <span>
194
+ {showHijriToggle && (
195
+ <Button
196
+ size="sm"
197
+ variant="link"
198
+ color="primary"
199
+ className="my-1"
200
+ onClick={() => {
201
+ setHijri(!isHijri);
202
+ }}
203
+ >
204
+ {t(isHijri ? "label.gregorian" : "label.hijri")}
205
+ </Button>
206
+ )}
207
+ </span>
208
+ {state.page === PageType.DATE && canSelectToday && (
209
+ <Button
210
+ size="sm"
211
+ variant="link"
212
+ color="primary"
213
+ className="my-1"
214
+ onClick={() => selectDate(new Date())}
215
+ >
216
+ {t("label.today")}
217
+ </Button>
218
+ )}
219
+ {state.page !== PageType.DATE && (
220
+ <Button
221
+ size="sm"
222
+ variant="link"
223
+ color="primary"
224
+ className="my-1"
225
+ onClick={() =>
226
+ dispatch({ type: "changePage", page: PageType.DATE })
227
+ }
228
+ >
229
+ {t("core:action.cancel")}
230
+ </Button>
231
+ )}
232
+ </div>
233
+ </div>
234
+ );
235
+ };