@quillsql/react 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.
Files changed (272) hide show
  1. package/.eslintrc.json +19 -0
  2. package/.prettierrc +11 -0
  3. package/example/README.md +46 -0
  4. package/example/package-lock.json +12111 -0
  5. package/example/package.json +43 -0
  6. package/example/public/favicon.ico +0 -0
  7. package/example/public/index.html +43 -0
  8. package/example/public/logo192.png +0 -0
  9. package/example/public/logo512.png +0 -0
  10. package/example/public/manifest.json +25 -0
  11. package/example/public/robots.txt +3 -0
  12. package/example/src/App.css +38 -0
  13. package/example/src/App.test.tsx +9 -0
  14. package/example/src/App.tsx +46 -0
  15. package/example/src/index.css +13 -0
  16. package/example/src/index.tsx +19 -0
  17. package/example/src/logo.svg +1 -0
  18. package/example/src/react-app-env.d.ts +1 -0
  19. package/example/src/reportWebVitals.ts +15 -0
  20. package/example/src/setupTests.ts +5 -0
  21. package/example/tsconfig.json +26 -0
  22. package/lib/AppContext.d.ts +29 -0
  23. package/lib/AppContext.js +94 -0
  24. package/lib/AppContext.js.map +1 -0
  25. package/lib/BarList.d.ts +17 -0
  26. package/lib/BarList.js +81 -0
  27. package/lib/BarList.js.map +1 -0
  28. package/lib/Button.d.ts +26 -0
  29. package/lib/Button.js +151 -0
  30. package/lib/Button.js.map +1 -0
  31. package/lib/Chart.d.ts +26 -0
  32. package/lib/Chart.js +532 -0
  33. package/lib/Chart.js.map +1 -0
  34. package/lib/Context.d.ts +28 -0
  35. package/lib/Context.js +126 -0
  36. package/lib/Context.js.map +1 -0
  37. package/lib/ContextProvider.d.ts +28 -0
  38. package/lib/ContextProvider.js +93 -0
  39. package/lib/ContextProvider.js.map +1 -0
  40. package/lib/Dashboard.d.ts +9 -0
  41. package/lib/Dashboard.js +271 -0
  42. package/lib/Dashboard.js.map +1 -0
  43. package/lib/DateRangePicker/Calendar.d.ts +15 -0
  44. package/lib/DateRangePicker/Calendar.js +94 -0
  45. package/lib/DateRangePicker/Calendar.js.map +1 -0
  46. package/lib/DateRangePicker/DateRangePicker.d.ts +31 -0
  47. package/lib/DateRangePicker/DateRangePicker.js +105 -0
  48. package/lib/DateRangePicker/DateRangePicker.js.map +1 -0
  49. package/lib/DateRangePicker/DateRangePickerButton.d.ts +21 -0
  50. package/lib/DateRangePicker/DateRangePickerButton.js +39 -0
  51. package/lib/DateRangePicker/DateRangePickerButton.js.map +1 -0
  52. package/lib/DateRangePicker/dateRangePickerUtils.d.ts +13 -0
  53. package/lib/DateRangePicker/dateRangePickerUtils.js +313 -0
  54. package/lib/DateRangePicker/dateRangePickerUtils.js.map +1 -0
  55. package/lib/DateRangePicker/index.d.ts +2 -0
  56. package/lib/DateRangePicker/index.js +2 -0
  57. package/lib/DateRangePicker/index.js.map +1 -0
  58. package/lib/DateRangePicker.d.ts +32 -0
  59. package/lib/DateRangePicker.js +105 -0
  60. package/lib/DateRangePicker.js.map +1 -0
  61. package/lib/PieChart.d.ts +63 -0
  62. package/lib/PieChart.js +589 -0
  63. package/lib/PieChart.js.map +1 -0
  64. package/lib/QuillProvider.d.ts +13 -0
  65. package/lib/QuillProvider.js +19 -0
  66. package/lib/QuillProvider.js.map +1 -0
  67. package/lib/assets/ArrowDownHeadIcon.d.ts +5 -0
  68. package/lib/assets/ArrowDownHeadIcon.js +29 -0
  69. package/lib/assets/ArrowDownHeadIcon.js.map +1 -0
  70. package/lib/assets/ArrowDownIcon.d.ts +5 -0
  71. package/lib/assets/ArrowDownIcon.js +29 -0
  72. package/lib/assets/ArrowDownIcon.js.map +1 -0
  73. package/lib/assets/ArrowDownRightIcon.d.ts +5 -0
  74. package/lib/assets/ArrowDownRightIcon.js +29 -0
  75. package/lib/assets/ArrowDownRightIcon.js.map +1 -0
  76. package/lib/assets/ArrowLeftHeadIcon.d.ts +5 -0
  77. package/lib/assets/ArrowLeftHeadIcon.js +29 -0
  78. package/lib/assets/ArrowLeftHeadIcon.js.map +1 -0
  79. package/lib/assets/ArrowRightHeadIcon.d.ts +5 -0
  80. package/lib/assets/ArrowRightHeadIcon.js +29 -0
  81. package/lib/assets/ArrowRightHeadIcon.js.map +1 -0
  82. package/lib/assets/ArrowRightIcon.d.ts +5 -0
  83. package/lib/assets/ArrowRightIcon.js +29 -0
  84. package/lib/assets/ArrowRightIcon.js.map +1 -0
  85. package/lib/assets/ArrowUpHeadIcon.d.ts +5 -0
  86. package/lib/assets/ArrowUpHeadIcon.js +29 -0
  87. package/lib/assets/ArrowUpHeadIcon.js.map +1 -0
  88. package/lib/assets/ArrowUpIcon.d.ts +5 -0
  89. package/lib/assets/ArrowUpIcon.js +29 -0
  90. package/lib/assets/ArrowUpIcon.js.map +1 -0
  91. package/lib/assets/ArrowUpRightIcon.d.ts +5 -0
  92. package/lib/assets/ArrowUpRightIcon.js +29 -0
  93. package/lib/assets/ArrowUpRightIcon.js.map +1 -0
  94. package/lib/assets/CalendarIcon.d.ts +5 -0
  95. package/lib/assets/CalendarIcon.js +29 -0
  96. package/lib/assets/CalendarIcon.js.map +1 -0
  97. package/lib/assets/DoubleArrowLeftHeadIcon.d.ts +5 -0
  98. package/lib/assets/DoubleArrowLeftHeadIcon.js +29 -0
  99. package/lib/assets/DoubleArrowLeftHeadIcon.js.map +1 -0
  100. package/lib/assets/DoubleArrowRightHeadIcon.d.ts +5 -0
  101. package/lib/assets/DoubleArrowRightHeadIcon.js +29 -0
  102. package/lib/assets/DoubleArrowRightHeadIcon.js.map +1 -0
  103. package/lib/assets/ExclamationFilledIcon.d.ts +5 -0
  104. package/lib/assets/ExclamationFilledIcon.js +29 -0
  105. package/lib/assets/ExclamationFilledIcon.js.map +1 -0
  106. package/lib/assets/LoadingSpinner.d.ts +5 -0
  107. package/lib/assets/LoadingSpinner.js +29 -0
  108. package/lib/assets/LoadingSpinner.js.map +1 -0
  109. package/lib/assets/SearchIcon.d.ts +5 -0
  110. package/lib/assets/SearchIcon.js +29 -0
  111. package/lib/assets/SearchIcon.js.map +1 -0
  112. package/lib/assets/XCircleIcon.d.ts +5 -0
  113. package/lib/assets/XCircleIcon.js +29 -0
  114. package/lib/assets/XCircleIcon.js.map +1 -0
  115. package/lib/assets/index.d.ts +16 -0
  116. package/lib/assets/index.js +17 -0
  117. package/lib/assets/index.js.map +1 -0
  118. package/lib/components/Dropdown/Dropdown.d.ts +12 -0
  119. package/lib/components/Dropdown/Dropdown.js +60 -0
  120. package/lib/components/Dropdown/Dropdown.js.map +1 -0
  121. package/lib/components/Dropdown/DropdownItem.d.ts +8 -0
  122. package/lib/components/Dropdown/DropdownItem.js +54 -0
  123. package/lib/components/Dropdown/DropdownItem.js.map +1 -0
  124. package/lib/components/Dropdown/index.d.ts +2 -0
  125. package/lib/components/Dropdown/index.js +3 -0
  126. package/lib/components/Dropdown/index.js.map +1 -0
  127. package/lib/components/Modal/Dropdown/Dropdown.d.ts +12 -0
  128. package/lib/components/Modal/Dropdown/Dropdown.js +52 -0
  129. package/lib/components/Modal/Dropdown/Dropdown.js.map +1 -0
  130. package/lib/components/Modal/Dropdown/DropdownItem.d.ts +8 -0
  131. package/lib/components/Modal/Dropdown/DropdownItem.js +51 -0
  132. package/lib/components/Modal/Dropdown/DropdownItem.js.map +1 -0
  133. package/lib/components/Modal/Dropdown/index.d.ts +2 -0
  134. package/lib/components/Modal/Dropdown/index.js +3 -0
  135. package/lib/components/Modal/Dropdown/index.js.map +1 -0
  136. package/lib/components/Modal/Modal.d.ts +13 -0
  137. package/lib/components/Modal/Modal.js +71 -0
  138. package/lib/components/Modal/Modal.js.map +1 -0
  139. package/lib/components/Modal/index.d.ts +1 -0
  140. package/lib/components/Modal/index.js +2 -0
  141. package/lib/components/Modal/index.js.map +1 -0
  142. package/lib/components/selectUtils.d.ts +9 -0
  143. package/lib/components/selectUtils.js +37 -0
  144. package/lib/components/selectUtils.js.map +1 -0
  145. package/lib/contexts/BaseColorContext.d.ts +3 -0
  146. package/lib/contexts/BaseColorContext.js +5 -0
  147. package/lib/contexts/BaseColorContext.js.map +1 -0
  148. package/lib/contexts/HoveredValueContext.d.ts +7 -0
  149. package/lib/contexts/HoveredValueContext.js +6 -0
  150. package/lib/contexts/HoveredValueContext.js.map +1 -0
  151. package/lib/contexts/RootStylesContext.d.ts +3 -0
  152. package/lib/contexts/RootStylesContext.js +4 -0
  153. package/lib/contexts/RootStylesContext.js.map +1 -0
  154. package/lib/contexts/SelectedValueContext.d.ts +7 -0
  155. package/lib/contexts/SelectedValueContext.js +7 -0
  156. package/lib/contexts/SelectedValueContext.js.map +1 -0
  157. package/lib/contexts/index.d.ts +4 -0
  158. package/lib/contexts/index.js +5 -0
  159. package/lib/contexts/index.js.map +1 -0
  160. package/lib/hooks/index.d.ts +4 -0
  161. package/lib/hooks/index.js +5 -0
  162. package/lib/hooks/index.js.map +1 -0
  163. package/lib/hooks/useInternalState.d.ts +3 -0
  164. package/lib/hooks/useInternalState.js +15 -0
  165. package/lib/hooks/useInternalState.js.map +1 -0
  166. package/lib/hooks/useOnClickOutside.d.ts +2 -0
  167. package/lib/hooks/useOnClickOutside.js +19 -0
  168. package/lib/hooks/useOnClickOutside.js.map +1 -0
  169. package/lib/hooks/useOnWindowResize.d.ts +4 -0
  170. package/lib/hooks/useOnWindowResize.js +15 -0
  171. package/lib/hooks/useOnWindowResize.js.map +1 -0
  172. package/lib/hooks/useSelectOnKeyDown.d.ts +2 -0
  173. package/lib/hooks/useSelectOnKeyDown.js +64 -0
  174. package/lib/hooks/useSelectOnKeyDown.js.map +1 -0
  175. package/lib/index.d.ts +3 -0
  176. package/lib/index.js +5 -0
  177. package/lib/index.js.map +1 -0
  178. package/lib/lib/colorClassNames.d.ts +19 -0
  179. package/lib/lib/colorClassNames.js +3175 -0
  180. package/lib/lib/colorClassNames.js.map +1 -0
  181. package/lib/lib/constants.d.ts +16 -0
  182. package/lib/lib/constants.js +47 -0
  183. package/lib/lib/constants.js.map +1 -0
  184. package/lib/lib/font.d.ts +13 -0
  185. package/lib/lib/font.js +14 -0
  186. package/lib/lib/font.js.map +1 -0
  187. package/lib/lib/hexColors.d.ts +3 -0
  188. package/lib/lib/hexColors.js +29 -0
  189. package/lib/lib/hexColors.js.map +1 -0
  190. package/lib/lib/index.d.ts +10 -0
  191. package/lib/lib/index.js +11 -0
  192. package/lib/lib/index.js.map +1 -0
  193. package/lib/lib/inputTypes.d.ts +20 -0
  194. package/lib/lib/inputTypes.js +37 -0
  195. package/lib/lib/inputTypes.js.map +1 -0
  196. package/lib/lib/shape.d.ts +73 -0
  197. package/lib/lib/shape.js +74 -0
  198. package/lib/lib/shape.js.map +1 -0
  199. package/lib/lib/sizing.d.ts +46 -0
  200. package/lib/lib/sizing.js +43 -0
  201. package/lib/lib/sizing.js.map +1 -0
  202. package/lib/lib/spacing.d.ts +264 -0
  203. package/lib/lib/spacing.js +265 -0
  204. package/lib/lib/spacing.js.map +1 -0
  205. package/lib/lib/theme.d.ts +22 -0
  206. package/lib/lib/theme.js +46 -0
  207. package/lib/lib/theme.js.map +1 -0
  208. package/lib/lib/utils.d.ts +12 -0
  209. package/lib/lib/utils.js +69 -0
  210. package/lib/lib/utils.js.map +1 -0
  211. package/lib/styles.css +12019 -0
  212. package/package.json +48 -0
  213. package/postcss.config.js +6 -0
  214. package/src/BarList.tsx +236 -0
  215. package/src/Chart.tsx +934 -0
  216. package/src/Context.tsx +204 -0
  217. package/src/Dashboard.tsx +379 -0
  218. package/src/DateRangePicker/Calendar.tsx +425 -0
  219. package/src/DateRangePicker/DateRangePicker.tsx +251 -0
  220. package/src/DateRangePicker/DateRangePickerButton.tsx +176 -0
  221. package/src/DateRangePicker/dateRangePickerUtils.tsx +460 -0
  222. package/src/DateRangePicker/index.ts +3 -0
  223. package/src/PieChart.tsx +838 -0
  224. package/src/QuillProvider.tsx +28 -0
  225. package/src/assets/ArrowDownHeadIcon.tsx +11 -0
  226. package/src/assets/ArrowDownIcon.tsx +14 -0
  227. package/src/assets/ArrowDownRightIcon.tsx +14 -0
  228. package/src/assets/ArrowLeftHeadIcon.tsx +11 -0
  229. package/src/assets/ArrowRightHeadIcon.tsx +11 -0
  230. package/src/assets/ArrowRightIcon.tsx +14 -0
  231. package/src/assets/ArrowUpHeadIcon.tsx +11 -0
  232. package/src/assets/ArrowUpIcon.tsx +14 -0
  233. package/src/assets/ArrowUpRightIcon.tsx +14 -0
  234. package/src/assets/CalendarIcon.tsx +14 -0
  235. package/src/assets/DoubleArrowLeftHeadIcon.tsx +18 -0
  236. package/src/assets/DoubleArrowRightHeadIcon.tsx +20 -0
  237. package/src/assets/ExclamationFilledIcon.tsx +14 -0
  238. package/src/assets/LoadingSpinner.tsx +11 -0
  239. package/src/assets/SearchIcon.tsx +14 -0
  240. package/src/assets/XCircleIcon.tsx +14 -0
  241. package/src/assets/index.ts +16 -0
  242. package/src/components/Dropdown/Dropdown.tsx +179 -0
  243. package/src/components/Dropdown/DropdownItem.tsx +86 -0
  244. package/src/components/Dropdown/index.ts +2 -0
  245. package/src/components/Modal/Modal.tsx +113 -0
  246. package/src/components/Modal/index.ts +1 -0
  247. package/src/components/selectUtils.ts +67 -0
  248. package/src/contexts/BaseColorContext.tsx +8 -0
  249. package/src/contexts/HoveredValueContext.tsx +12 -0
  250. package/src/contexts/RootStylesContext.tsx +5 -0
  251. package/src/contexts/SelectedValueContext.tsx +13 -0
  252. package/src/contexts/index.ts +4 -0
  253. package/src/hooks/index.ts +4 -0
  254. package/src/hooks/useInternalState.tsx +18 -0
  255. package/src/hooks/useOnClickOutside.tsx +23 -0
  256. package/src/hooks/useOnWindowResize.tsx +17 -0
  257. package/src/hooks/useSelectOnKeyDown.tsx +80 -0
  258. package/src/index.ts +4 -0
  259. package/src/lib/colorClassNames.ts +3191 -0
  260. package/src/lib/constants.ts +52 -0
  261. package/src/lib/font.ts +14 -0
  262. package/src/lib/hexColors.ts +28 -0
  263. package/src/lib/index.ts +10 -0
  264. package/src/lib/inputTypes.ts +62 -0
  265. package/src/lib/shape.ts +75 -0
  266. package/src/lib/sizing.ts +47 -0
  267. package/src/lib/spacing.ts +264 -0
  268. package/src/lib/theme.ts +49 -0
  269. package/src/lib/utils.tsx +81 -0
  270. package/src/styles.css +5 -0
  271. package/tailwind.config.js +16 -0
  272. package/tsconfig.json +22 -0
@@ -0,0 +1,176 @@
1
+ import React, { Dispatch, Ref, SetStateAction } from 'react';
2
+ import { twMerge } from 'tailwind-merge';
3
+
4
+ import { ArrowDownHeadIcon, CalendarIcon } from '../assets';
5
+
6
+ import {
7
+ BaseColors,
8
+ border,
9
+ borderRadius,
10
+ boxShadow,
11
+ fontSize,
12
+ fontWeight,
13
+ getColorClassNames,
14
+ sizing,
15
+ spacing,
16
+ } from '../lib';
17
+
18
+ import { DateRangePickerOption, DateRangePickerValue } from './DateRangePicker';
19
+ import { DEFAULT_COLOR, colorPalette } from '../lib/theme';
20
+ import {
21
+ formatSelectedDates,
22
+ makeDateRangePickerClassName,
23
+ } from './dateRangePickerUtils';
24
+ import { getSelectButtonColors, hasValue } from '../components/selectUtils';
25
+
26
+ interface DateRangePickerButtonProps {
27
+ value: DateRangePickerValue;
28
+ options: DateRangePickerOption[];
29
+ placeholder: string;
30
+ disabled: boolean;
31
+ calendarRef: Ref<HTMLButtonElement>;
32
+ showCalendar: boolean;
33
+ setShowCalendar: Dispatch<SetStateAction<boolean>>;
34
+ onCalendarKeyDown: (e: React.KeyboardEvent<HTMLButtonElement>) => void;
35
+ enableDropdown: boolean;
36
+ dropdownRef: Ref<HTMLButtonElement>;
37
+ showDropdown: boolean;
38
+ setShowDropdown: Dispatch<SetStateAction<boolean>>;
39
+ onDropdownKeyDown: (e: React.KeyboardEvent<HTMLButtonElement>) => void;
40
+ locale?: Locale;
41
+ dropdownPlaceholder?: string;
42
+ }
43
+
44
+ const DateRangePickerButton = ({
45
+ value,
46
+ options,
47
+ placeholder,
48
+ disabled,
49
+ calendarRef,
50
+ showCalendar,
51
+ setShowCalendar,
52
+ onCalendarKeyDown,
53
+ enableDropdown,
54
+ dropdownRef,
55
+ showDropdown,
56
+ setShowDropdown,
57
+ onDropdownKeyDown,
58
+ locale,
59
+ dropdownPlaceholder = 'Select',
60
+ }: DateRangePickerButtonProps) => {
61
+ const [startDate, endDate, dropdownValue] = value;
62
+
63
+ const hasDateSelection =
64
+ (startDate || endDate) !== null && (startDate || endDate) !== undefined;
65
+ const hasDropdownSelection = hasValue(dropdownValue);
66
+
67
+ const calendarText = hasDateSelection
68
+ ? formatSelectedDates(startDate as Date, endDate as Date, locale)
69
+ : placeholder;
70
+ const dropdownText = dropdownValue
71
+ ? String(options.find(option => option.value === dropdownValue)?.text)
72
+ : dropdownPlaceholder;
73
+
74
+ return (
75
+ <div
76
+ className={twMerge(
77
+ makeDateRangePickerClassName('button'),
78
+ 'flex items-center justify-between',
79
+ getColorClassNames('white').bgColor,
80
+ borderRadius.md.all,
81
+ boxShadow.sm
82
+ )}
83
+ >
84
+ <button
85
+ type="button"
86
+ ref={calendarRef}
87
+ onClick={() => setShowCalendar(!showCalendar)}
88
+ onKeyDown={onCalendarKeyDown}
89
+ className={twMerge(
90
+ makeDateRangePickerClassName('calendarButton'),
91
+ 'flex items-center w-full truncate focus:outline-none focus:ring-2',
92
+ getSelectButtonColors(hasDateSelection, disabled),
93
+ enableDropdown
94
+ ? border.none.right
95
+ : twMerge(borderRadius.md.right, border.sm.right),
96
+ getColorClassNames(BaseColors.Blue, colorPalette.lightRing)
97
+ .focusRingColor,
98
+ spacing.twoXl.paddingX,
99
+ spacing.sm.paddingY,
100
+ borderRadius.md.left,
101
+ border.sm.all
102
+ )}
103
+ disabled={disabled}
104
+ >
105
+ <CalendarIcon
106
+ className={twMerge(
107
+ makeDateRangePickerClassName('calendarButtonIcon'),
108
+ 'flex-none',
109
+ getColorClassNames(DEFAULT_COLOR, colorPalette.lightText).textColor,
110
+ sizing.lg.height,
111
+ sizing.lg.width,
112
+ spacing.threeXs.negativeMarginLeft,
113
+ spacing.lg.marginRight
114
+ )}
115
+ aria-hidden="true"
116
+ />
117
+ <p
118
+ className={twMerge(
119
+ makeDateRangePickerClassName('calendarButtonText'),
120
+ 'whitespace-nowrap truncate',
121
+ fontSize.sm,
122
+ fontWeight.md
123
+ )}
124
+ >
125
+ {calendarText}
126
+ </p>
127
+ </button>
128
+ {enableDropdown ? (
129
+ <button
130
+ type="button"
131
+ ref={dropdownRef}
132
+ onClick={() => setShowDropdown(!showDropdown)}
133
+ className={twMerge(
134
+ makeDateRangePickerClassName('dropdownButton'),
135
+ 'inline-flex justify-between w-48 truncate focus:outline-none focus:ring-2',
136
+ getSelectButtonColors(hasDropdownSelection, disabled),
137
+ getColorClassNames(BaseColors.Blue, colorPalette.lightRing)
138
+ .focusRingColor,
139
+ spacing.twoXl.paddingX,
140
+ spacing.sm.paddingY,
141
+ spacing.px.negativeMarginLeft,
142
+ borderRadius.md.right,
143
+ border.sm.all
144
+ )}
145
+ onKeyDown={onDropdownKeyDown}
146
+ disabled={disabled}
147
+ >
148
+ <p
149
+ className={twMerge(
150
+ makeDateRangePickerClassName('dropdownButtonText'),
151
+ 'whitespace-nowrap truncate',
152
+ fontSize.sm,
153
+ fontWeight.md
154
+ )}
155
+ >
156
+ {dropdownText}
157
+ </p>
158
+ <ArrowDownHeadIcon
159
+ className={twMerge(
160
+ makeDateRangePickerClassName('dropdownButtonIcon'),
161
+ 'flex-none',
162
+ sizing.lg.height,
163
+ sizing.lg.width,
164
+ spacing.twoXs.negativeMarginRight,
165
+ getColorClassNames(DEFAULT_COLOR, colorPalette.lightText)
166
+ .textColor
167
+ )}
168
+ aria-hidden="true"
169
+ />
170
+ </button>
171
+ ) : null}
172
+ </div>
173
+ );
174
+ };
175
+
176
+ export default DateRangePickerButton;
@@ -0,0 +1,460 @@
1
+ import { twMerge } from 'tailwind-merge';
2
+ import {
3
+ addDays,
4
+ format,
5
+ isEqual,
6
+ isToday,
7
+ max,
8
+ min,
9
+ startOfDay,
10
+ startOfMonth,
11
+ startOfToday,
12
+ startOfWeek,
13
+ startOfYear,
14
+ sub,
15
+ } from 'date-fns';
16
+ import { Color } from '../lib/inputTypes';
17
+ import { DateRangePickerOption } from './DateRangePicker';
18
+ import {
19
+ borderRadius,
20
+ fontWeight,
21
+ getColorClassNames,
22
+ makeClassName,
23
+ } from '../lib';
24
+ import { DEFAULT_COLOR, colorPalette } from '../lib/theme';
25
+
26
+ export const makeDateRangePickerClassName = makeClassName('DateRangePicker');
27
+
28
+ export const getWeekdays = (locale: Locale) => {
29
+ const firstDayOfWeek = startOfWeek(new Date());
30
+ return Array.from(Array(7)).map((e, i) =>
31
+ format(addDays(firstDayOfWeek, i), 'EEEEEE', { locale })
32
+ );
33
+ };
34
+
35
+ export const capitalize = (s: string, locale: Locale) => {
36
+ return s.charAt(0).toLocaleUpperCase(locale.code) + s.substring(1);
37
+ };
38
+
39
+ export const getStartDateByDropdownValue = (
40
+ dropdownValue: string | null | undefined,
41
+ dropdownOptions: DateRangePickerOption[]
42
+ ) => {
43
+ const startDate =
44
+ dropdownOptions.find(
45
+ (option: DateRangePickerOption) => option.value === dropdownValue
46
+ )?.startDate ?? null;
47
+ return startDate ? startOfDay(startDate) : null;
48
+ };
49
+
50
+ export const getEndDateByDropdownValue = (
51
+ dropdownValue: string | null | undefined,
52
+ dropdownOptions: DateRangePickerOption[]
53
+ ) =>
54
+ startOfDay(
55
+ dropdownOptions.find(
56
+ (option: DateRangePickerOption) => option.value === dropdownValue
57
+ )?.endDate ?? startOfToday()
58
+ );
59
+
60
+ export const parseStartDate = (
61
+ startDate: Date | null | undefined,
62
+ minDate: Date | null | undefined,
63
+ selectedDropdownValue: string | null | undefined,
64
+ dropdownOptions: DateRangePickerOption[]
65
+ ) => {
66
+ if (selectedDropdownValue) {
67
+ startDate = getStartDateByDropdownValue(
68
+ selectedDropdownValue,
69
+ dropdownOptions
70
+ );
71
+ }
72
+ if (!startDate) return null;
73
+ if (startDate && !minDate) return startOfDay(startDate);
74
+ return startOfDay(max([startDate as Date, minDate as Date]));
75
+ };
76
+
77
+ export const parseEndDate = (
78
+ endDate: Date | null | undefined,
79
+ maxDate: Date | null | undefined,
80
+ selectedDropdownValue: string | null | undefined,
81
+ dropdownOptions: DateRangePickerOption[]
82
+ ) => {
83
+ if (selectedDropdownValue) {
84
+ endDate = getEndDateByDropdownValue(selectedDropdownValue, dropdownOptions);
85
+ }
86
+ if (!endDate) return null;
87
+ if (endDate && !maxDate) return startOfDay(endDate);
88
+
89
+ return startOfDay(min([endDate as Date, maxDate as Date]));
90
+ };
91
+
92
+ export const defaultOptions: DateRangePickerOption[] = [
93
+ // {
94
+ // value: 'tdy',
95
+ // text: 'Today',
96
+ // startDate: startOfToday(),
97
+ // },
98
+ {
99
+ value: 'w',
100
+ text: 'Last 7 days',
101
+ startDate: sub(startOfToday(), { days: 7 }),
102
+ },
103
+ {
104
+ value: 't',
105
+ text: 'Last 30 days',
106
+ startDate: sub(startOfToday(), { days: 30 }),
107
+ },
108
+ {
109
+ value: '90d',
110
+ text: 'Last 90 days',
111
+ startDate: sub(startOfToday(), { days: 90 }),
112
+ },
113
+ {
114
+ value: '6m',
115
+ text: 'Last 6 months',
116
+ startDate: sub(startOfToday(), { months: 6 }),
117
+ },
118
+ {
119
+ value: 'm',
120
+ text: 'This month',
121
+ startDate: startOfMonth(startOfToday()),
122
+ },
123
+ {
124
+ value: 'y',
125
+ text: 'This year',
126
+ startDate: startOfYear(startOfToday()),
127
+ },
128
+ // {
129
+ // value: 'tw',
130
+ // text: 'This week',
131
+ // startDate: startOfWeek(startOfToday()),
132
+ // },
133
+ // {
134
+ // value: '2w',
135
+ // text: 'Last 2 weeks',
136
+ // startDate: sub(startOfToday(), { weeks: 2 }),
137
+ // },
138
+ {
139
+ value: 'at',
140
+ text: 'All time',
141
+ startDate: sub(startOfToday(), { years: 100 }),
142
+ },
143
+ ];
144
+
145
+ export const getDateStyles = (
146
+ date: Date,
147
+ finalStartDate: Date | null,
148
+ finalEndDate: Date | null,
149
+ hoveredDate: Date | undefined,
150
+ isDateDisabled: boolean,
151
+ color: Color
152
+ ) =>
153
+ twMerge(
154
+ getDayBgColorClassName(
155
+ date,
156
+ finalStartDate,
157
+ finalEndDate,
158
+ hoveredDate as Date | null,
159
+ color,
160
+ isDateDisabled
161
+ ),
162
+ getDayTextClassNames(
163
+ date,
164
+ finalStartDate,
165
+ finalEndDate,
166
+ hoveredDate as Date | null,
167
+ color,
168
+ isDateDisabled
169
+ ),
170
+ getDayHoverBgColorClassName(
171
+ date,
172
+ finalStartDate,
173
+ finalEndDate,
174
+ isDateDisabled
175
+ ),
176
+ getDayRoundedClassName(
177
+ date,
178
+ finalStartDate,
179
+ finalEndDate,
180
+ hoveredDate as Date | null
181
+ )
182
+ );
183
+
184
+ const getDayRoundedClassName = (
185
+ day: Date,
186
+ selectedStartDay: Date | null,
187
+ selectedEndDay: Date | null,
188
+ hoveredDay: Date | null
189
+ ): string => {
190
+ if (!selectedStartDay && !selectedEndDay) {
191
+ return borderRadius.md.all;
192
+ }
193
+ if (
194
+ selectedStartDay &&
195
+ selectedEndDay &&
196
+ isEqual(day, selectedStartDay) &&
197
+ isEqual(day, selectedEndDay)
198
+ ) {
199
+ return borderRadius.md.all;
200
+ }
201
+ if (selectedStartDay && selectedEndDay && isEqual(day, selectedStartDay)) {
202
+ return borderRadius.md.left;
203
+ }
204
+ if (
205
+ selectedStartDay &&
206
+ !selectedEndDay &&
207
+ !hoveredDay &&
208
+ isEqual(day, selectedStartDay)
209
+ ) {
210
+ return borderRadius.md.all;
211
+ }
212
+ if (
213
+ selectedStartDay &&
214
+ !selectedEndDay &&
215
+ hoveredDay &&
216
+ day < selectedStartDay
217
+ ) {
218
+ return borderRadius.md.all;
219
+ }
220
+ if (
221
+ selectedStartDay &&
222
+ !selectedEndDay &&
223
+ hoveredDay &&
224
+ isEqual(day, selectedStartDay) &&
225
+ hoveredDay > selectedStartDay
226
+ ) {
227
+ return borderRadius.md.left;
228
+ }
229
+ if (
230
+ selectedStartDay &&
231
+ !selectedEndDay &&
232
+ hoveredDay &&
233
+ day > selectedStartDay &&
234
+ day < hoveredDay
235
+ ) {
236
+ return borderRadius.none.all;
237
+ }
238
+ if (
239
+ selectedStartDay &&
240
+ selectedEndDay &&
241
+ day > selectedStartDay &&
242
+ day < selectedEndDay
243
+ ) {
244
+ return borderRadius.none.all;
245
+ }
246
+ if (
247
+ selectedStartDay &&
248
+ !selectedEndDay &&
249
+ hoveredDay &&
250
+ isEqual(day, hoveredDay) &&
251
+ !isEqual(day, selectedStartDay)
252
+ ) {
253
+ return borderRadius.md.right;
254
+ }
255
+ if (selectedStartDay && selectedEndDay && isEqual(day, selectedEndDay)) {
256
+ return borderRadius.md.right;
257
+ }
258
+ if (
259
+ selectedStartDay &&
260
+ selectedEndDay &&
261
+ (day < selectedStartDay || day > selectedEndDay)
262
+ ) {
263
+ return borderRadius.md.all;
264
+ }
265
+ return borderRadius.md.all;
266
+ };
267
+
268
+ export const displaySelected = (
269
+ selectedStartDay: Date | null,
270
+ selectedEndDay: Date | null
271
+ ) => {
272
+ if (!selectedStartDay && !selectedEndDay) {
273
+ return '';
274
+ } else if (selectedStartDay && !selectedEndDay) {
275
+ const options: Intl.DateTimeFormatOptions = {
276
+ year: 'numeric',
277
+ month: 'short',
278
+ day: 'numeric',
279
+ };
280
+ return selectedStartDay.toLocaleDateString('en-US', options);
281
+ } else if (selectedStartDay && selectedEndDay) {
282
+ if (isEqual(selectedStartDay, selectedEndDay)) {
283
+ const options: Intl.DateTimeFormatOptions = {
284
+ year: 'numeric',
285
+ month: 'short',
286
+ day: 'numeric',
287
+ };
288
+ return selectedStartDay.toLocaleDateString('en-US', options);
289
+ } else if (
290
+ selectedStartDay.getMonth() === selectedEndDay.getMonth() &&
291
+ selectedStartDay.getFullYear() === selectedEndDay.getFullYear()
292
+ ) {
293
+ const optionsStartDate: Intl.DateTimeFormatOptions = {
294
+ month: 'short',
295
+ day: 'numeric',
296
+ };
297
+ return `${selectedStartDay.toLocaleDateString(
298
+ 'en-US',
299
+ optionsStartDate
300
+ )} -
301
+ ${selectedEndDay.getDate()}, ${selectedEndDay.getFullYear()}`;
302
+ } else {
303
+ const options: Intl.DateTimeFormatOptions = {
304
+ year: 'numeric',
305
+ month: 'short',
306
+ day: 'numeric',
307
+ };
308
+ return `${selectedStartDay.toLocaleDateString('en-US', options)} -
309
+ ${selectedEndDay.toLocaleDateString('en-US', options)}`;
310
+ }
311
+ }
312
+ };
313
+
314
+ const getDayBgColorClassName = (
315
+ day: Date,
316
+ selectedStartDay: Date | null,
317
+ selectedEndDay: Date | null,
318
+ hoveredDay: Date | null,
319
+ color: Color,
320
+ isDayDisabled = false
321
+ ): string => {
322
+ if (isDayDisabled) return getColorClassNames('transparent').bgColor;
323
+ if (selectedStartDay && isEqual(day, selectedStartDay)) {
324
+ return getColorClassNames(color, colorPalette.background).bgColor;
325
+ }
326
+ if (
327
+ selectedStartDay &&
328
+ !selectedEndDay &&
329
+ hoveredDay &&
330
+ day > selectedStartDay &&
331
+ day < hoveredDay
332
+ ) {
333
+ return getColorClassNames(DEFAULT_COLOR, colorPalette.lightBackground)
334
+ .bgColor;
335
+ }
336
+ if (selectedEndDay && isEqual(day, selectedEndDay)) {
337
+ return getColorClassNames(color, colorPalette.background).bgColor;
338
+ }
339
+ if (
340
+ selectedStartDay &&
341
+ selectedEndDay &&
342
+ day > selectedStartDay &&
343
+ day < selectedEndDay
344
+ ) {
345
+ return getColorClassNames(DEFAULT_COLOR, colorPalette.lightBackground)
346
+ .bgColor;
347
+ }
348
+ return getColorClassNames('transparent').bgColor;
349
+ };
350
+
351
+ const getDayTextClassNames = (
352
+ day: Date,
353
+ selectedStartDay: Date | null,
354
+ selectedEndDay: Date | null,
355
+ hoveredDay: Date | null,
356
+ color: Color,
357
+ isDayDisabled = false
358
+ ): string => {
359
+ if (isDayDisabled)
360
+ return getColorClassNames(DEFAULT_COLOR, colorPalette.lightText).textColor;
361
+ if (isToday(day)) {
362
+ if (
363
+ (selectedStartDay && isEqual(day, selectedStartDay)) ||
364
+ (selectedEndDay && isEqual(day, selectedEndDay))
365
+ ) {
366
+ return getColorClassNames('white').textColor;
367
+ }
368
+ return twMerge(
369
+ getColorClassNames(color, colorPalette.text).textColor,
370
+ fontWeight.lg
371
+ );
372
+ }
373
+ if (selectedStartDay && isEqual(day, selectedStartDay)) {
374
+ return getColorClassNames('white').textColor;
375
+ }
376
+ if (
377
+ selectedStartDay &&
378
+ !selectedEndDay &&
379
+ hoveredDay &&
380
+ day > selectedStartDay &&
381
+ day < hoveredDay
382
+ ) {
383
+ return getColorClassNames(DEFAULT_COLOR, colorPalette.darkestText)
384
+ .textColor;
385
+ }
386
+ if (selectedEndDay && isEqual(day, selectedEndDay)) {
387
+ return getColorClassNames('white').textColor;
388
+ }
389
+ if (
390
+ selectedStartDay &&
391
+ selectedEndDay &&
392
+ day > selectedStartDay &&
393
+ day < selectedEndDay
394
+ ) {
395
+ return getColorClassNames(color, colorPalette.text).textColor;
396
+ }
397
+ return getColorClassNames(DEFAULT_COLOR, colorPalette.darkestText).textColor;
398
+ };
399
+
400
+ const getDayHoverBgColorClassName = (
401
+ day: Date,
402
+ selectedStartDay: Date | null,
403
+ selectedEndDay: Date | null,
404
+ isDayDisabled = false
405
+ ): string => {
406
+ if (isDayDisabled) return '';
407
+ if (selectedStartDay && isEqual(day, selectedStartDay)) {
408
+ return '';
409
+ }
410
+ if (selectedEndDay && isEqual(day, selectedEndDay)) {
411
+ return '';
412
+ }
413
+ return 'hover:bg-gray-200';
414
+ };
415
+
416
+ export const formatSelectedDates = (
417
+ startDate: Date | null,
418
+ endDate: Date | null,
419
+ locale?: Locale
420
+ ) => {
421
+ const localeCode = locale?.code || 'en-US';
422
+ if (!startDate && !endDate) {
423
+ return '';
424
+ } else if (startDate && !endDate) {
425
+ const options: Intl.DateTimeFormatOptions = {
426
+ year: 'numeric',
427
+ month: 'short',
428
+ day: 'numeric',
429
+ };
430
+ return startDate.toLocaleDateString(localeCode, options);
431
+ } else if (startDate && endDate) {
432
+ if (isEqual(startDate, endDate)) {
433
+ const options: Intl.DateTimeFormatOptions = {
434
+ year: 'numeric',
435
+ month: 'short',
436
+ day: 'numeric',
437
+ };
438
+ return startDate.toLocaleDateString(localeCode, options);
439
+ } else if (
440
+ startDate.getMonth() === endDate.getMonth() &&
441
+ startDate.getFullYear() === endDate.getFullYear()
442
+ ) {
443
+ const optionsStartDate: Intl.DateTimeFormatOptions = {
444
+ month: 'short',
445
+ day: 'numeric',
446
+ };
447
+ return `${startDate.toLocaleDateString(localeCode, optionsStartDate)} -
448
+ ${endDate.getDate()}, ${endDate.getFullYear()}`;
449
+ } else {
450
+ const options: Intl.DateTimeFormatOptions = {
451
+ year: 'numeric',
452
+ month: 'short',
453
+ day: 'numeric',
454
+ };
455
+ return `${startDate.toLocaleDateString(localeCode, options)} -
456
+ ${endDate.toLocaleDateString(localeCode, options)}`;
457
+ }
458
+ }
459
+ return '';
460
+ };
@@ -0,0 +1,3 @@
1
+ export { default as DateRangePicker } from "./DateRangePicker";
2
+
3
+ export { DateRangePickerValue } from "./DateRangePicker";