@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,70 @@
1
+ import { type ColorType, type CssProp, type TestProps } from "@react-fabric/core/dist/types/types";
2
+ export type DateLike = Date | string | number;
3
+ export type DateParsed = Date | undefined;
4
+ export declare enum PageType {
5
+ DATE = 0,
6
+ MONTH = 1,
7
+ YEAR = 2,
8
+ DECADE = 3
9
+ }
10
+ export declare enum DatePart {
11
+ NOW = "$now",
12
+ MINUTE = "$minute",
13
+ HOUR = "$hour",
14
+ DAY = "$day",
15
+ WEEK = "$week",
16
+ MONTH = "$month",
17
+ QUARTER = "$quarter",
18
+ YEAR = "$year",
19
+ DECADE = "$decade"
20
+ }
21
+ export interface BaseProps {
22
+ min?: DateLike;
23
+ max?: DateLike;
24
+ withTime?: boolean;
25
+ showHijriToggle?: boolean;
26
+ dateDisabled?: DateLike[] | ((date: Date) => boolean);
27
+ }
28
+ export interface DateProps extends BaseProps, CssProp, TestProps {
29
+ value?: DateLike;
30
+ onChange?: (date: string) => void;
31
+ }
32
+ export interface RangeProps extends BaseProps, CssProp, TestProps {
33
+ value?: [start: DateLike, end: DateLike];
34
+ onChange?: (dates: [start: string, end: string]) => void;
35
+ presets?: Record<string, [DateLike, DateLike]>;
36
+ }
37
+ export interface EventType<D = DateLike> {
38
+ label: string;
39
+ icon?: string;
40
+ start: D;
41
+ end: D;
42
+ hijri?: boolean;
43
+ }
44
+ export interface PresetType {
45
+ value: string;
46
+ label: string;
47
+ }
48
+ export interface SuperDateProps {
49
+ as?: "button" | "chip";
50
+ color?: ColorType;
51
+ variant?: "solid" | "outline" | "link";
52
+ /**
53
+ * relative date value
54
+ */
55
+ value?: string;
56
+ /**
57
+ * change handler
58
+ */
59
+ onChange?: (value: string, dates: [string, string]) => void;
60
+ /**
61
+ * list of calendar events
62
+ */
63
+ events?: EventType[];
64
+ /**
65
+ * list of relative presets
66
+ *
67
+ * {value:'$year-5|$year', label:'last 5 years'}
68
+ */
69
+ presets?: PresetType[];
70
+ }
@@ -0,0 +1,30 @@
1
+ import { PageType, type DateLike } from "../types";
2
+ export declare namespace DateUtil {
3
+ const format: (date?: DateLike, fmt?: string, locale?: string, isHijri?: boolean) => string;
4
+ const isRelative: (date: DateLike) => date is string;
5
+ const isRelativeRange: (date: DateLike) => date is string;
6
+ const startOfDay: (date?: Date, isHijri?: boolean) => Date;
7
+ const startOfWeek: (date: Date, isHijri?: boolean) => Date;
8
+ const startOfMonth: (date: Date, isHijri?: boolean) => Date;
9
+ const startOfYear: (date: Date, isHijri?: boolean) => Date;
10
+ const startOfDecade: (date: Date, isHijri?: boolean) => Date;
11
+ const startOfCentury: (date: Date, isHijri?: boolean) => Date;
12
+ const endOfDay: (date?: Date, isHijri?: boolean) => Date;
13
+ const endOfWeek: (date: Date, isHijri?: boolean) => Date;
14
+ const endOfMonth: (date: Date, isHijri?: boolean) => Date;
15
+ const endOfYear: (date: Date, isHijri?: boolean) => Date;
16
+ const endOfDecade: (date: Date, isHijri?: boolean) => Date;
17
+ const endOfCentury: (date: Date, isHijri?: boolean) => Date;
18
+ const addDays: (date: Date, d: number, isHijri?: boolean) => Date;
19
+ const addWeeks: (date: Date, d: number, isHijri?: boolean) => Date;
20
+ const addMonths: (date: Date, d: number, isHijri?: boolean) => Date;
21
+ const addYears: (date: Date, d: number, isHijri?: boolean) => Date;
22
+ const getWeek: (date: Date, locale: string, isHijri?: boolean) => string;
23
+ const isSameMonth: (date: Date, compare: Date, isHijri?: boolean) => boolean;
24
+ const isSameYear: (date: Date, compare: Date, isHijri?: boolean) => boolean;
25
+ const isDisabled: (date: Date, page: PageType, min?: DateLike, max?: DateLike) => boolean;
26
+ const parseDate: (datelike: string) => Date;
27
+ const parseRange: (datelike: string) => [Date, Date];
28
+ const relativeTooltip: (datelike?: string, isHijri?: boolean) => string;
29
+ const relativeLabel: (datelike?: string, isHijri?: boolean) => string | undefined;
30
+ }
package/package.json ADDED
@@ -0,0 +1,58 @@
1
+ {
2
+ "name": "@react-fabric/date",
3
+ "version": "1.0.0-beta.0",
4
+ "description": "RUF: Date package",
5
+ "author": "Adarsh Pastakia <adarshpastakia@outlook.com>",
6
+ "license": "MIT",
7
+ "repository": "https://github.com/adarshpastakia/react-ui-framework/tree/main/packages/date",
8
+ "source": "src/index.ts",
9
+ "main": "dist/cjs/index.js",
10
+ "module": "dist/esm/index.js",
11
+ "types": "dist/types/index.d.ts",
12
+ "directories": {
13
+ "src": "src",
14
+ "test": "tests"
15
+ },
16
+ "files": [
17
+ "src",
18
+ "dist"
19
+ ],
20
+ "publishConfig": {
21
+ "access": "public",
22
+ "registry": "https://registry.npmjs.org/"
23
+ },
24
+ "scripts": {
25
+ "makelink": "yarn link",
26
+ "removelink": "yarn unlink",
27
+ "prebuild": "rm -rf ./dist",
28
+ "build:dts": "tsc -p ./tsconfig.build.json",
29
+ "build:rollup": "rollup -c ../../rollup.config.mjs",
30
+ "build": "npm-run-all -p build:rollup build:dts",
31
+ "watch:dts": "tsc -w -p ./tsconfig.build.json",
32
+ "watch:rollup": "rollup -w -c ../../rollup.config.mjs",
33
+ "watch": "npm-run-all -p watch:rollup watch:dts"
34
+ },
35
+ "dependencies": {
36
+ "@floating-ui/react": "^0.26.24",
37
+ "@react-fabric/core": "^1.0.0-beta.0",
38
+ "@react-fabric/form": "^1.0.0-beta.0",
39
+ "@react-fabric/utilities": "^1.0.0-beta.0",
40
+ "classnames": "^2.5.1",
41
+ "date-fns": "^4.0.0",
42
+ "resize-observer-polyfill": "^1.5.1"
43
+ },
44
+ "peerDependencies": {
45
+ "i18next": "^23.15.1",
46
+ "react": ">=18.0.0 <20.0.0",
47
+ "react-dom": ">=18.0.0 <20.0.0",
48
+ "react-i18next": "^15.0.2"
49
+ },
50
+ "devDependencies": {
51
+ "@types/react-color": "^3.0.12"
52
+ },
53
+ "engines": {
54
+ "node": ">=18.12.0",
55
+ "npm": ">=8.19.0"
56
+ },
57
+ "gitHead": "83fa79064b1d57e71149005432192d506810bbda"
58
+ }
@@ -0,0 +1,75 @@
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
+ @layer comp {
25
+ .dateHeader {
26
+ box-shadow: 0 -1px 0 theme(colors.tint.200/50%) inset;
27
+ }
28
+
29
+ .timeSlider {
30
+ height: 11.25rem;
31
+
32
+ &::before,
33
+ &::after {
34
+ content: "";
35
+ z-index: 10;
36
+ display: block;
37
+ box-shadow: 0 0 3rem 0.25rem theme(colors.tint.600);
38
+ }
39
+
40
+ &::before {
41
+ position: sticky;
42
+ top: 0;
43
+ }
44
+
45
+ &::after {
46
+ position: sticky;
47
+ bottom: 0;
48
+ }
49
+
50
+ & > :first-child,
51
+ & > :last-child {
52
+ height: calc(50% - 0.75rem);
53
+ }
54
+
55
+ & [data-skew="1"] > div {
56
+ transform: scaleY(0.9);
57
+ opacity: 0.9;
58
+ }
59
+
60
+ & [data-skew="2"] > div {
61
+ transform: scaleY(0.75);
62
+ opacity: 0.75;
63
+ }
64
+
65
+ & [data-skew="3"] > div {
66
+ transform: scaleY(0.65);
67
+ opacity: 0.65;
68
+ }
69
+
70
+ & [data-skew="4"] > div {
71
+ transform: scaleY(0.5);
72
+ opacity: 0.5;
73
+ }
74
+ }
75
+ }
@@ -0,0 +1,181 @@
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 { isArray } from "@react-fabric/utilities";
25
+ import classNames from "classnames";
26
+ import {
27
+ isAfter,
28
+ isBefore,
29
+ isSameDay,
30
+ isSameMonth,
31
+ isSameYear,
32
+ startOfDecade,
33
+ } from "date-fns";
34
+ import i18next from "i18next";
35
+ import { useMemo } from "react";
36
+ import { PageType } from "../types";
37
+ import { DateUtil } from "../utils/dateUtil";
38
+
39
+ export interface DateCellProps {
40
+ date: Date;
41
+ page: PageType;
42
+ selected?: Date | [Date, Date];
43
+ hilight?: [Date, Date];
44
+ isHijri?: boolean;
45
+ isMuted?: boolean;
46
+ className?: string;
47
+ selectDate?: (date: Date) => void;
48
+ changePageDate?: (date: Date) => void;
49
+ mouseOver?: (date: Date) => void;
50
+ dateDisabled?: (date: Date) => boolean;
51
+ }
52
+
53
+ export const DateCell = ({
54
+ date = new Date(),
55
+ isMuted = false,
56
+ ...props
57
+ }: DateCellProps) => {
58
+ const {
59
+ page,
60
+ selected,
61
+ hilight,
62
+ isHijri,
63
+ className,
64
+ selectDate,
65
+ changePageDate,
66
+ mouseOver,
67
+ dateDisabled,
68
+ } = props;
69
+
70
+ const isToday = useMemo(() => isSameDay(date, new Date()), [date]);
71
+ const isSelected = useMemo(
72
+ () => selected && !isArray(selected) && isSameDay(date, selected),
73
+ [date, page, selected],
74
+ );
75
+
76
+ const isMarked = useMemo(() => {
77
+ switch (page) {
78
+ case PageType.MONTH:
79
+ return selected && !isArray(selected) && isSameMonth(date, selected);
80
+ case PageType.YEAR:
81
+ return selected && !isArray(selected) && isSameYear(date, selected);
82
+ case PageType.DECADE:
83
+ return (
84
+ selected &&
85
+ !isArray(selected) &&
86
+ isSameYear(startOfDecade(date), startOfDecade(selected))
87
+ );
88
+ }
89
+ }, [date, page, selected]);
90
+
91
+ const isHilightEdge = useMemo(() => {
92
+ if (!isMuted) {
93
+ if (hilight) {
94
+ return isSameDay(date, hilight[0]);
95
+ } else if (Array.isArray(selected)) {
96
+ const [start, end] = selected;
97
+ let hilight: string | false = false;
98
+ if (isSameDay(date, start) && isSameDay(date, end))
99
+ hilight = "startend";
100
+ else if (isSameDay(date, start)) hilight = "start";
101
+ else if (isSameDay(date, end)) hilight = "end";
102
+ return hilight;
103
+ }
104
+ }
105
+ }, [date, hilight, selected, isMuted]);
106
+
107
+ const isHilight = useMemo(() => {
108
+ if (!isMuted) {
109
+ if (hilight) {
110
+ const [start, end] = hilight;
111
+ if (isBefore(start, end)) {
112
+ return (
113
+ isAfter(date, start) &&
114
+ isBefore(date, DateUtil.addDays(end, 1, isHijri))
115
+ );
116
+ } else {
117
+ return (
118
+ isBefore(date, start) &&
119
+ isAfter(date, DateUtil.addDays(end, -1, isHijri))
120
+ );
121
+ }
122
+ } else if (Array.isArray(selected)) {
123
+ const [start, end] = selected;
124
+ return (
125
+ isAfter(date, DateUtil.startOfDay(start, isHijri)) &&
126
+ isBefore(date, DateUtil.startOfDay(end, isHijri))
127
+ );
128
+ }
129
+ }
130
+ }, [isMuted, hilight, selected, date, isHijri]);
131
+
132
+ const isDisabled = useMemo(
133
+ () => !!isSelected || dateDisabled?.(date),
134
+ [date, dateDisabled, isSelected],
135
+ );
136
+
137
+ return (
138
+ <div
139
+ role="none"
140
+ className={classNames(
141
+ "p-1 m-px text-sm text-center rounded",
142
+ className,
143
+ !isDisabled && isMuted && "text-tint-500",
144
+ (isToday || isMarked) &&
145
+ "underline underline-offset-4 text-primary-700",
146
+ (!!isSelected || isHilightEdge) && "bg-primary-400 text-white",
147
+ isHilightEdge === "start" && "rounded-e-none",
148
+ isHilightEdge === "end" && "rounded-s-none",
149
+ isHilight && "bg-primary-50 rounded-none",
150
+ !isDisabled &&
151
+ "cursor-pointer hover:bg-primary-100 hover:text-primary-700",
152
+ isDisabled && "text-tint-100 cursor-not-allowed",
153
+ )}
154
+ onFocus={() => hilight && page === PageType.DATE && mouseOver?.(date)}
155
+ onMouseOver={() => hilight && page === PageType.DATE && mouseOver?.(date)}
156
+ onClick={() =>
157
+ !isDisabled &&
158
+ (page === PageType.DATE ? selectDate?.(date) : changePageDate?.(date))
159
+ }
160
+ >
161
+ {page === PageType.DATE &&
162
+ DateUtil.format(date, "d", i18next.language, isHijri)}
163
+ {page === PageType.MONTH &&
164
+ DateUtil.format(date, "MMM", i18next.language, isHijri)}
165
+ {page === PageType.YEAR &&
166
+ DateUtil.format(date, "yyyy", i18next.language, isHijri)}
167
+ {page === PageType.DECADE &&
168
+ `${DateUtil.format(
169
+ DateUtil.startOfDecade(date, isHijri),
170
+ "yyyy",
171
+ i18next.language,
172
+ isHijri,
173
+ )}-${DateUtil.format(
174
+ DateUtil.endOfDecade(date, isHijri),
175
+ "yy",
176
+ i18next.language,
177
+ isHijri,
178
+ )}`}
179
+ </div>
180
+ );
181
+ };
@@ -0,0 +1,223 @@
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, CoreIcons } from "@react-fabric/core";
25
+ import classNames from "classnames";
26
+ import { addDays, isAfter, isBefore } from "date-fns";
27
+ import i18next from "i18next";
28
+ import { useMemo } from "react";
29
+ import { type DateLike, PageType } from "../types";
30
+ import { DateUtil } from "../utils/dateUtil";
31
+
32
+ interface Props {
33
+ min?: DateLike;
34
+ max?: DateLike;
35
+ pageDate: Date;
36
+ page: PageType;
37
+ isHijri?: boolean;
38
+ hidePrevious?: boolean;
39
+ hideNext?: boolean;
40
+ changePage: (page: PageType) => void;
41
+ changePageDate: (date: Date) => void;
42
+ }
43
+
44
+ export const DateHeader = (props: Props) => {
45
+ const {
46
+ page,
47
+ pageDate = new Date(),
48
+ changePage,
49
+ changePageDate,
50
+ hideNext,
51
+ hidePrevious,
52
+ min,
53
+ max,
54
+ isHijri = false,
55
+ } = props;
56
+
57
+ const headLabel = useMemo(() => {
58
+ switch (page) {
59
+ case PageType.DATE:
60
+ return DateUtil.format(pageDate, "MMM yyyy", i18next.language, isHijri);
61
+ case PageType.MONTH:
62
+ return DateUtil.format(pageDate, "yyyy", i18next.language, isHijri);
63
+ case PageType.YEAR:
64
+ return `${DateUtil.format(
65
+ DateUtil.startOfDecade(pageDate, isHijri),
66
+ "yyyy",
67
+ i18next.language,
68
+ isHijri,
69
+ )} - ${DateUtil.format(
70
+ DateUtil.endOfDecade(pageDate, isHijri),
71
+ "yyyy",
72
+ i18next.language,
73
+ isHijri,
74
+ )}`;
75
+ case PageType.DECADE:
76
+ return `${DateUtil.format(
77
+ DateUtil.startOfCentury(pageDate, isHijri),
78
+ "yyyy",
79
+ i18next.language,
80
+ isHijri,
81
+ )} - ${DateUtil.format(
82
+ DateUtil.endOfCentury(pageDate, isHijri),
83
+ "yyyy",
84
+ i18next.language,
85
+ isHijri,
86
+ )}`;
87
+ }
88
+ }, [i18next.language, isHijri, page, pageDate]);
89
+
90
+ const [startPage, endPage] = useMemo(() => {
91
+ if (page === PageType.DATE) {
92
+ const dateBefore = addDays(DateUtil.startOfYear(pageDate, isHijri), -1);
93
+ const dateAfter = addDays(DateUtil.endOfYear(pageDate, isHijri), 1);
94
+
95
+ return [
96
+ min && isBefore(dateBefore, min) ? null : dateBefore,
97
+ max && isAfter(dateAfter, max) ? null : dateAfter,
98
+ ];
99
+ }
100
+ if (page === PageType.MONTH) {
101
+ const dateBefore = addDays(DateUtil.startOfDecade(pageDate, isHijri), -1);
102
+ const dateAfter = addDays(DateUtil.endOfDecade(pageDate, isHijri), 1);
103
+
104
+ return [
105
+ min && isBefore(dateBefore, min) ? null : dateBefore,
106
+ max && isAfter(dateAfter, max) ? null : dateAfter,
107
+ ];
108
+ }
109
+ if (page === PageType.YEAR) {
110
+ const dateBefore = addDays(
111
+ DateUtil.startOfCentury(pageDate, isHijri),
112
+ -1,
113
+ );
114
+ const dateAfter = addDays(DateUtil.endOfCentury(pageDate, isHijri), 1);
115
+
116
+ return [
117
+ min && isBefore(dateBefore, min) ? null : dateBefore,
118
+ max && isAfter(dateAfter, max) ? null : dateAfter,
119
+ ];
120
+ }
121
+ return [];
122
+ }, [page, min, max, pageDate, isHijri]);
123
+
124
+ const [previousPage, nextPage] = useMemo(() => {
125
+ if (page === PageType.DATE) {
126
+ const dateBefore = addDays(DateUtil.startOfMonth(pageDate, isHijri), -1);
127
+ const dateAfter = addDays(DateUtil.endOfMonth(pageDate, isHijri), 1);
128
+ return [
129
+ min && isBefore(dateBefore, min) ? null : dateBefore,
130
+ max && isAfter(dateAfter, max) ? null : dateAfter,
131
+ ];
132
+ }
133
+ if (page === PageType.MONTH) {
134
+ const dateBefore = addDays(DateUtil.startOfYear(pageDate, isHijri), -1);
135
+ const dateAfter = addDays(DateUtil.endOfYear(pageDate, isHijri), 1);
136
+ return [
137
+ min && isBefore(dateBefore, min) ? null : dateBefore,
138
+ max && isAfter(dateAfter, max) ? null : dateAfter,
139
+ ];
140
+ }
141
+ if (page === PageType.YEAR) {
142
+ const dateBefore = addDays(DateUtil.startOfDecade(pageDate, isHijri), -1);
143
+ const dateAfter = addDays(DateUtil.endOfDecade(pageDate, isHijri), 1);
144
+
145
+ return [
146
+ min && isBefore(dateBefore, min) ? null : dateBefore,
147
+ max && isAfter(dateAfter, max) ? null : dateAfter,
148
+ ];
149
+ }
150
+ if (page === PageType.DECADE) {
151
+ const dateBefore = addDays(
152
+ DateUtil.startOfCentury(pageDate, isHijri),
153
+ -1,
154
+ );
155
+ const dateAfter = addDays(DateUtil.endOfCentury(pageDate, isHijri), 1);
156
+
157
+ return [
158
+ min && isBefore(dateBefore, min) ? null : dateBefore,
159
+ max && isAfter(dateAfter, max) ? null : dateAfter,
160
+ ];
161
+ }
162
+ return [];
163
+ }, [page, min, max, pageDate, isHijri]);
164
+
165
+ return (
166
+ <div className="flex flex-nowrap items-center area-head" data-page={page}>
167
+ {startPage !== undefined && (
168
+ <Button
169
+ icon={CoreIcons.chevronsLeft}
170
+ rtlFlip
171
+ size="sm"
172
+ variant="link"
173
+ aria-label="start"
174
+ disabled={!startPage}
175
+ className={classNames(hidePrevious && "invisible")}
176
+ onClick={() => startPage && changePageDate?.(startPage)}
177
+ />
178
+ )}
179
+ <Button
180
+ icon={CoreIcons.chevronLeft}
181
+ rtlFlip
182
+ size="sm"
183
+ variant="link"
184
+ aria-label="prev"
185
+ disabled={!previousPage}
186
+ className={classNames(hidePrevious && "invisible")}
187
+ onClick={() => previousPage && changePageDate?.(previousPage)}
188
+ />
189
+ <div
190
+ role="none"
191
+ onClick={() => changePage(page + 1)}
192
+ className={classNames(
193
+ "flex-1 px-2 py-1 text-primary-600 text-center text-sm font-medium truncate",
194
+ page === PageType.DECADE ? "pointer-events-none" : "cursor-pointer",
195
+ )}
196
+ >
197
+ {headLabel}
198
+ </div>
199
+ <Button
200
+ icon={CoreIcons.chevronRight}
201
+ rtlFlip
202
+ size="sm"
203
+ variant="link"
204
+ aria-label="next"
205
+ disabled={!nextPage}
206
+ className={classNames(hideNext && "invisible")}
207
+ onClick={() => nextPage && changePageDate?.(nextPage)}
208
+ />
209
+ {endPage !== undefined && (
210
+ <Button
211
+ icon={CoreIcons.chevronsRight}
212
+ rtlFlip
213
+ size="sm"
214
+ variant="link"
215
+ aria-label="end"
216
+ disabled={!endPage}
217
+ className={classNames(hideNext && "invisible")}
218
+ onClick={() => endPage && changePageDate?.(endPage)}
219
+ />
220
+ )}
221
+ </div>
222
+ );
223
+ };