dash-core-components 4.0.0-rc2 → 4.0.0-rc3
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/dash_core_components/async-datepicker.js +1 -1
- package/dash_core_components/async-datepicker.js.map +1 -1
- package/dash_core_components/async-dropdown.js +1 -2
- package/dash_core_components/async-dropdown.js.map +1 -1
- package/dash_core_components/async-graph.js +1 -1
- package/dash_core_components/async-graph.js.map +1 -1
- package/dash_core_components/async-highlight.js +1 -1
- package/dash_core_components/async-highlight.js.map +1 -1
- package/dash_core_components/async-markdown.js +1 -1
- package/dash_core_components/async-markdown.js.map +1 -1
- package/dash_core_components/async-mathjax.js +1 -1
- package/dash_core_components/async-slider.js +1 -1
- package/dash_core_components/async-slider.js.map +1 -1
- package/dash_core_components/async-upload.js +1 -1
- package/dash_core_components/async-upload.js.map +1 -1
- package/dash_core_components/dash_core_components-shared.js +1 -1
- package/dash_core_components/dash_core_components-shared.js.map +1 -1
- package/dash_core_components/dash_core_components.js +1 -1
- package/dash_core_components/dash_core_components.js.map +1 -1
- package/dash_core_components/proptypes.js +116 -12
- package/lib/components/Clipboard.react.js +1 -1
- package/lib/components/DatePickerRange.tsx +70 -0
- package/lib/components/DatePickerSingle.tsx +72 -0
- package/lib/components/Input.tsx +1 -1
- package/lib/components/Tab.tsx +19 -0
- package/lib/components/Tabs.tsx +311 -0
- package/lib/components/css/calendar.css +66 -0
- package/lib/components/css/datepickers.css +233 -0
- package/lib/components/css/dcc.css +1 -0
- package/lib/components/css/dropdown.css +6 -3
- package/lib/components/css/input.css +8 -2
- package/lib/components/css/sliders.css +22 -0
- package/lib/components/css/tabs.css +113 -0
- package/lib/fragments/DatePickerRange.tsx +444 -0
- package/lib/fragments/DatePickerSingle.tsx +249 -0
- package/lib/fragments/Dropdown.tsx +8 -4
- package/lib/fragments/Graph.react.js +18 -45
- package/lib/fragments/RangeSlider.tsx +2 -0
- package/lib/index.ts +4 -6
- package/lib/types.ts +353 -27
- package/lib/utils/DatePickerPersistence.ts +14 -0
- package/lib/utils/LazyLoader/datePickerRange.ts +4 -0
- package/lib/utils/LazyLoader/datePickerSingle.ts +4 -0
- package/lib/utils/calendar/Calendar.tsx +382 -0
- package/lib/utils/calendar/CalendarDay.tsx +75 -0
- package/lib/utils/calendar/CalendarDayPadding.tsx +15 -0
- package/lib/utils/calendar/CalendarMonth.tsx +247 -0
- package/lib/utils/calendar/CalendarMonthHeader.tsx +23 -0
- package/lib/utils/calendar/createMonthGrid.ts +28 -0
- package/lib/utils/calendar/helpers.ts +272 -0
- package/lib/utils/computeSliderMarkers.ts +71 -116
- package/lib/utils/optionRendering.tsx +22 -15
- package/lib/utils/sliderRendering.tsx +40 -2
- package/package.json +16 -13
- package/lib/components/DatePickerRange.react.js +0 -244
- package/lib/components/DatePickerSingle.react.js +0 -209
- package/lib/components/Dropdown.react.js.old +0 -228
- package/lib/components/Tab.react.js +0 -80
- package/lib/components/Tab.react.js.old +0 -79
- package/lib/components/Tabs.react.js +0 -295
- package/lib/components/Textarea.react.js.old +0 -287
- package/lib/fragments/DatePickerRange.react.js +0 -188
- package/lib/fragments/DatePickerSingle.react.js +0 -132
- package/lib/utils/DatePickerPersistence.js +0 -18
- package/lib/utils/LazyLoader/datePickerRange.js +0 -8
- package/lib/utils/LazyLoader/datePickerSingle.js +0 -8
- package/lib/utils/convertToMoment.js +0 -30
- package/lib/utils/optionTypes.js +0 -39
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
import React, {useCallback, useMemo} from 'react';
|
|
2
|
+
import {
|
|
3
|
+
addDays,
|
|
4
|
+
subDays,
|
|
5
|
+
addWeeks,
|
|
6
|
+
subWeeks,
|
|
7
|
+
addMonths,
|
|
8
|
+
subMonths,
|
|
9
|
+
setDay,
|
|
10
|
+
startOfWeek,
|
|
11
|
+
endOfWeek,
|
|
12
|
+
format,
|
|
13
|
+
} from 'date-fns';
|
|
14
|
+
import type {Day} from 'date-fns';
|
|
15
|
+
import CalendarDay from './CalendarDay';
|
|
16
|
+
import CalendarDayPadding from './CalendarDayPadding';
|
|
17
|
+
import {createMonthGrid} from './createMonthGrid';
|
|
18
|
+
import {
|
|
19
|
+
isDateInRange,
|
|
20
|
+
isDateDisabled,
|
|
21
|
+
isSameDay,
|
|
22
|
+
getUserLocale,
|
|
23
|
+
} from './helpers';
|
|
24
|
+
import {CalendarDirection} from '../../types';
|
|
25
|
+
import '../../components/css/calendar.css';
|
|
26
|
+
import CalendarMonthHeader from './CalendarMonthHeader';
|
|
27
|
+
|
|
28
|
+
export enum NavigationDirection {
|
|
29
|
+
Backward = -1,
|
|
30
|
+
Forward = 1,
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
type CalendarMonthProps = {
|
|
34
|
+
year: number;
|
|
35
|
+
month: number; // 0-11 representing January-December;
|
|
36
|
+
dateFocused?: Date;
|
|
37
|
+
selectedDates?: Date[];
|
|
38
|
+
highlightedDatesRange?: [Date, Date];
|
|
39
|
+
minDateAllowed?: Date;
|
|
40
|
+
maxDateAllowed?: Date;
|
|
41
|
+
disabledDates?: Date[];
|
|
42
|
+
onSelectionStart?: (date: Date) => void;
|
|
43
|
+
onSelectionEnd?: (date: Date) => void;
|
|
44
|
+
onDayFocused?: (date: Date) => void;
|
|
45
|
+
onDaysHighlighted?: (date: Date) => void;
|
|
46
|
+
firstDayOfWeek?: Day;
|
|
47
|
+
showOutsideDays?: boolean;
|
|
48
|
+
daySize?: number;
|
|
49
|
+
monthFormat?: string;
|
|
50
|
+
showMonthHeader?: boolean;
|
|
51
|
+
direction?: CalendarDirection;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
export const CalendarMonth = ({
|
|
55
|
+
year,
|
|
56
|
+
month,
|
|
57
|
+
onSelectionStart,
|
|
58
|
+
onSelectionEnd,
|
|
59
|
+
onDayFocused,
|
|
60
|
+
onDaysHighlighted,
|
|
61
|
+
selectedDates = [],
|
|
62
|
+
highlightedDatesRange,
|
|
63
|
+
minDateAllowed,
|
|
64
|
+
maxDateAllowed,
|
|
65
|
+
disabledDates,
|
|
66
|
+
monthFormat,
|
|
67
|
+
firstDayOfWeek = 0,
|
|
68
|
+
showOutsideDays = true,
|
|
69
|
+
// eslint-disable-next-line no-magic-numbers
|
|
70
|
+
daySize = 36,
|
|
71
|
+
showMonthHeader = false,
|
|
72
|
+
direction = CalendarDirection.LeftToRight,
|
|
73
|
+
...props
|
|
74
|
+
}: CalendarMonthProps): JSX.Element => {
|
|
75
|
+
const gridDates = useMemo(
|
|
76
|
+
() => createMonthGrid(year, month, firstDayOfWeek, showOutsideDays),
|
|
77
|
+
[year, month, firstDayOfWeek, showOutsideDays]
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
const isDisabled = useCallback(
|
|
81
|
+
(date: Date): boolean => {
|
|
82
|
+
return isDateDisabled(
|
|
83
|
+
date,
|
|
84
|
+
minDateAllowed,
|
|
85
|
+
maxDateAllowed,
|
|
86
|
+
disabledDates
|
|
87
|
+
);
|
|
88
|
+
},
|
|
89
|
+
[minDateAllowed, maxDateAllowed, disabledDates]
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
const computeIsOutside = useCallback(
|
|
93
|
+
(date: Date): boolean => {
|
|
94
|
+
return date.getMonth() !== month;
|
|
95
|
+
},
|
|
96
|
+
[month]
|
|
97
|
+
);
|
|
98
|
+
|
|
99
|
+
const daysOfTheWeek = useMemo(() => {
|
|
100
|
+
return Array.from({length: 7}, (_, i) => {
|
|
101
|
+
const date = setDay(new Date(), (i + firstDayOfWeek) % 7);
|
|
102
|
+
return format(date, 'EEEEEE', {locale: getUserLocale()});
|
|
103
|
+
});
|
|
104
|
+
}, [firstDayOfWeek]);
|
|
105
|
+
|
|
106
|
+
const handleKeyDown = useCallback(
|
|
107
|
+
(e: React.KeyboardEvent, date: Date) => {
|
|
108
|
+
let newDate: Date | null = null;
|
|
109
|
+
|
|
110
|
+
switch (e.key) {
|
|
111
|
+
case ' ':
|
|
112
|
+
case 'Enter': {
|
|
113
|
+
e.preventDefault();
|
|
114
|
+
const isOutside = computeIsOutside(date);
|
|
115
|
+
if (!isDisabled(date) && (!isOutside || showOutsideDays)) {
|
|
116
|
+
// Keyboard selection: only call onSelectionEnd
|
|
117
|
+
// Calendar will handle completing immediately by setting both start and end
|
|
118
|
+
onSelectionEnd?.(date);
|
|
119
|
+
}
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
case 'ArrowRight':
|
|
123
|
+
newDate =
|
|
124
|
+
direction === CalendarDirection.RightToLeft
|
|
125
|
+
? subDays(date, 1)
|
|
126
|
+
: addDays(date, 1);
|
|
127
|
+
break;
|
|
128
|
+
case 'ArrowLeft':
|
|
129
|
+
newDate =
|
|
130
|
+
direction === CalendarDirection.RightToLeft
|
|
131
|
+
? addDays(date, 1)
|
|
132
|
+
: subDays(date, 1);
|
|
133
|
+
break;
|
|
134
|
+
case 'ArrowDown':
|
|
135
|
+
newDate = addWeeks(date, 1);
|
|
136
|
+
break;
|
|
137
|
+
case 'ArrowUp':
|
|
138
|
+
newDate = subWeeks(date, 1);
|
|
139
|
+
break;
|
|
140
|
+
case 'PageDown':
|
|
141
|
+
newDate = addMonths(date, 1);
|
|
142
|
+
break;
|
|
143
|
+
case 'PageUp':
|
|
144
|
+
newDate = subMonths(date, 1);
|
|
145
|
+
break;
|
|
146
|
+
case 'Home':
|
|
147
|
+
newDate = startOfWeek(date, {weekStartsOn: firstDayOfWeek});
|
|
148
|
+
break;
|
|
149
|
+
case 'End':
|
|
150
|
+
newDate = endOfWeek(date, {weekStartsOn: firstDayOfWeek});
|
|
151
|
+
break;
|
|
152
|
+
default:
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
if (newDate) {
|
|
157
|
+
e.preventDefault();
|
|
158
|
+
if (isDateInRange(newDate, minDateAllowed, maxDateAllowed)) {
|
|
159
|
+
onDayFocused?.(newDate);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
},
|
|
163
|
+
[
|
|
164
|
+
onDayFocused,
|
|
165
|
+
onSelectionStart,
|
|
166
|
+
onSelectionEnd,
|
|
167
|
+
computeIsOutside,
|
|
168
|
+
isDisabled,
|
|
169
|
+
showOutsideDays,
|
|
170
|
+
minDateAllowed,
|
|
171
|
+
maxDateAllowed,
|
|
172
|
+
direction,
|
|
173
|
+
firstDayOfWeek,
|
|
174
|
+
]
|
|
175
|
+
);
|
|
176
|
+
|
|
177
|
+
const calendarWidth = daySize * 7 + 16; // 16px for table padding
|
|
178
|
+
|
|
179
|
+
return (
|
|
180
|
+
<table
|
|
181
|
+
className="dash-datepicker-calendar"
|
|
182
|
+
style={{width: `${calendarWidth}px`}}
|
|
183
|
+
>
|
|
184
|
+
<thead>
|
|
185
|
+
{showMonthHeader && (
|
|
186
|
+
<tr>
|
|
187
|
+
<CalendarMonthHeader
|
|
188
|
+
year={year}
|
|
189
|
+
month={month}
|
|
190
|
+
monthFormat={monthFormat}
|
|
191
|
+
/>
|
|
192
|
+
</tr>
|
|
193
|
+
)}
|
|
194
|
+
<tr>
|
|
195
|
+
{daysOfTheWeek.map((day, i) => (
|
|
196
|
+
<th key={i}>
|
|
197
|
+
<span>{day}</span>
|
|
198
|
+
</th>
|
|
199
|
+
))}
|
|
200
|
+
</tr>
|
|
201
|
+
</thead>
|
|
202
|
+
<tbody>
|
|
203
|
+
{gridDates.map((week, i) => (
|
|
204
|
+
<tr key={i} className="dash-datepicker-calendar-week">
|
|
205
|
+
{week.map((date, j) =>
|
|
206
|
+
date ? (
|
|
207
|
+
<CalendarDay
|
|
208
|
+
key={j}
|
|
209
|
+
date={date}
|
|
210
|
+
isOutside={computeIsOutside(date)}
|
|
211
|
+
showOutsideDays={showOutsideDays}
|
|
212
|
+
onMouseDown={() => {
|
|
213
|
+
onDaysHighlighted?.(date);
|
|
214
|
+
onSelectionStart?.(date);
|
|
215
|
+
}}
|
|
216
|
+
onMouseUp={() => onSelectionEnd?.(date)}
|
|
217
|
+
onMouseEnter={() =>
|
|
218
|
+
onDaysHighlighted?.(date)
|
|
219
|
+
}
|
|
220
|
+
onKeyDown={e => handleKeyDown(e, date)}
|
|
221
|
+
isFocused={isSameDay(
|
|
222
|
+
date,
|
|
223
|
+
props.dateFocused
|
|
224
|
+
)}
|
|
225
|
+
isSelected={selectedDates.some(d =>
|
|
226
|
+
isSameDay(date, d)
|
|
227
|
+
)}
|
|
228
|
+
isHighlighted={
|
|
229
|
+
highlightedDatesRange !== undefined &&
|
|
230
|
+
isDateInRange(
|
|
231
|
+
date,
|
|
232
|
+
highlightedDatesRange[0],
|
|
233
|
+
highlightedDatesRange[1]
|
|
234
|
+
)
|
|
235
|
+
}
|
|
236
|
+
isDisabled={isDisabled(date)}
|
|
237
|
+
/>
|
|
238
|
+
) : (
|
|
239
|
+
<CalendarDayPadding key={j} />
|
|
240
|
+
)
|
|
241
|
+
)}
|
|
242
|
+
</tr>
|
|
243
|
+
))}
|
|
244
|
+
</tbody>
|
|
245
|
+
</table>
|
|
246
|
+
);
|
|
247
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import React, {useMemo} from 'react';
|
|
2
|
+
import {formatDate} from './helpers';
|
|
3
|
+
|
|
4
|
+
const CalendarMonthHeader = (props: {
|
|
5
|
+
year: number;
|
|
6
|
+
month: number;
|
|
7
|
+
monthFormat?: string;
|
|
8
|
+
}) => {
|
|
9
|
+
const label = useMemo(() => {
|
|
10
|
+
return formatDate(
|
|
11
|
+
new Date(props.year, props.month, 1),
|
|
12
|
+
props.monthFormat
|
|
13
|
+
);
|
|
14
|
+
}, [props]);
|
|
15
|
+
|
|
16
|
+
return (
|
|
17
|
+
<th colSpan={7} className="dash-datepicker-calendar-month-header">
|
|
18
|
+
{label}
|
|
19
|
+
</th>
|
|
20
|
+
);
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export default CalendarMonthHeader;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import {getDay, getDaysInMonth, addDays, getMonth, type Day} from 'date-fns';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Creates a 2D array of Date objects representing a calendar month grid.
|
|
5
|
+
* Always returns exactly 6 rows (weeks) to maintain consistent calendar height.
|
|
6
|
+
*/
|
|
7
|
+
export const createMonthGrid = (
|
|
8
|
+
year: number,
|
|
9
|
+
month: number,
|
|
10
|
+
firstDayOfWeek: Day,
|
|
11
|
+
showOutsideDays = true
|
|
12
|
+
): (Date | null)[][] => {
|
|
13
|
+
const firstDay = new Date(year, month, 1);
|
|
14
|
+
const offset = (getDay(firstDay) - firstDayOfWeek + 7) % 7;
|
|
15
|
+
const startDate = addDays(firstDay, -offset);
|
|
16
|
+
const weeksNeeded = Math.ceil((offset + getDaysInMonth(firstDay)) / 7);
|
|
17
|
+
|
|
18
|
+
return Array.from({length: 6}, (_, week) =>
|
|
19
|
+
Array.from({length: 7}, (_, day) => {
|
|
20
|
+
if (week >= weeksNeeded) {
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const date = addDays(startDate, week * 7 + day);
|
|
25
|
+
return showOutsideDays || getMonth(date) === month ? date : null;
|
|
26
|
+
})
|
|
27
|
+
);
|
|
28
|
+
};
|
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
import {
|
|
2
|
+
format,
|
|
3
|
+
parse,
|
|
4
|
+
parseISO,
|
|
5
|
+
isValid,
|
|
6
|
+
startOfDay,
|
|
7
|
+
startOfMonth,
|
|
8
|
+
isSameDay as dateFnsIsSameDay,
|
|
9
|
+
isBefore,
|
|
10
|
+
isAfter,
|
|
11
|
+
isWithinInterval,
|
|
12
|
+
min,
|
|
13
|
+
max,
|
|
14
|
+
} from 'date-fns';
|
|
15
|
+
import type {Locale} from 'date-fns';
|
|
16
|
+
import {DatePickerSingleProps} from '../../types';
|
|
17
|
+
|
|
18
|
+
declare global {
|
|
19
|
+
interface Window {
|
|
20
|
+
dateFns?: {
|
|
21
|
+
locale?: Record<string, Locale>;
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Converts relevant moment.js format tokens to unicode tokens suitable for use
|
|
28
|
+
* in date-fns. This maintains backwards compatibility with our publicly
|
|
29
|
+
* documented format strings while enabling us to use the date-fns library
|
|
30
|
+
* internally.
|
|
31
|
+
*/
|
|
32
|
+
function convertFormatTokens(momentFormat: string): string {
|
|
33
|
+
return momentFormat
|
|
34
|
+
.replace(/dd/g, 'EEEEEE') // Day of week abbreviation: Mo, Tu, We
|
|
35
|
+
.replace(/Do/g, 'do') // Ordinal day: 1st, 2nd, 3rd
|
|
36
|
+
.replace(/YYYY/g, 'yyyy') // 4-digit year
|
|
37
|
+
.replace(/YY/g, 'yy') // 2-digit year
|
|
38
|
+
.replace(/DD/g, 'dd') // Day of month with leading zero
|
|
39
|
+
.replace(/D/g, 'd') // Day of month
|
|
40
|
+
.replace(/X/g, 't'); // Unix timestamp (seconds)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Matches the user's preferred locale against the locales that have been loaded
|
|
45
|
+
* externally on the page from the assets folder or via a script tag
|
|
46
|
+
*/
|
|
47
|
+
export function getUserLocale(): Locale | undefined {
|
|
48
|
+
// Check if page has loaded any external locales
|
|
49
|
+
const availableLocales = window.dateFns?.locale ?? {};
|
|
50
|
+
|
|
51
|
+
// Match available locales against user locale preferences
|
|
52
|
+
const localeKeys = Object.keys(availableLocales);
|
|
53
|
+
const userLanguages = navigator.languages || [navigator.language];
|
|
54
|
+
for (const lang of userLanguages) {
|
|
55
|
+
// First check full locale string for regional variants (e.g., 'fr-CA')
|
|
56
|
+
const normalizedLang = lang.replace('-', '');
|
|
57
|
+
if (availableLocales[normalizedLang]) {
|
|
58
|
+
return availableLocales[normalizedLang];
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// Fallback to simple language code (e.g., 'fr')
|
|
62
|
+
const langCode = lang.split('-')[0];
|
|
63
|
+
if (availableLocales[langCode]) {
|
|
64
|
+
return availableLocales[langCode];
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// No match found against user language preferences, we'll use first
|
|
69
|
+
// loaded locale (ultimately determined by script order in HTML)
|
|
70
|
+
return availableLocales[localeKeys[0]];
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function formatDate(date?: Date, formatStr = 'YYYY-MM-DD'): string {
|
|
74
|
+
if (!date) {
|
|
75
|
+
return '';
|
|
76
|
+
}
|
|
77
|
+
const convertedFormat = convertFormatTokens(formatStr);
|
|
78
|
+
return format(date, convertedFormat, {locale: getUserLocale()});
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/*
|
|
82
|
+
* Outputs a date object in YYYY-MM-DD format, suitable for use in props
|
|
83
|
+
*/
|
|
84
|
+
export function dateAsStr(
|
|
85
|
+
date?: Date
|
|
86
|
+
): `${string}-${string}-${string}` | undefined {
|
|
87
|
+
if (!date) {
|
|
88
|
+
return undefined;
|
|
89
|
+
}
|
|
90
|
+
return formatDate(date, 'YYYY-MM-DD') as `${string}-${string}-${string}`;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export function strAsDate(
|
|
94
|
+
dateStr?: string,
|
|
95
|
+
formatStr?: string
|
|
96
|
+
): Date | undefined {
|
|
97
|
+
if (!dateStr) {
|
|
98
|
+
return undefined;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const locale = getUserLocale();
|
|
102
|
+
let parsed = formatStr
|
|
103
|
+
? parse(dateStr, convertFormatTokens(formatStr), new Date(), {
|
|
104
|
+
locale,
|
|
105
|
+
})
|
|
106
|
+
: parseISO(dateStr);
|
|
107
|
+
|
|
108
|
+
// Fallback to native Date constructor for non-ISO formats
|
|
109
|
+
if (!isValid(parsed)) {
|
|
110
|
+
parsed = new Date(dateStr);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return isValid(parsed) ? startOfDay(parsed) : undefined;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
type AnyDayFormat = string | Date | DatePickerSingleProps['date'];
|
|
117
|
+
export function isSameDay(day1?: AnyDayFormat, day2?: AnyDayFormat): boolean {
|
|
118
|
+
if (!day1 && !day2) {
|
|
119
|
+
return true; // Both undefined/null - considered the same
|
|
120
|
+
}
|
|
121
|
+
if (!day1 || !day2) {
|
|
122
|
+
return false; // Only one is defined - considered different
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// Convert strings to Dates using strAsDate logic
|
|
126
|
+
const date1 = typeof day1 === 'string' ? strAsDate(day1) : day1;
|
|
127
|
+
const date2 = typeof day2 === 'string' ? strAsDate(day2) : day2;
|
|
128
|
+
|
|
129
|
+
// strAsDate returns undefined for invalid dates
|
|
130
|
+
if (!date1 || !date2) {
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return dateFnsIsSameDay(date1, date2);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export function isDateInRange(
|
|
138
|
+
targetDate: Date,
|
|
139
|
+
minDate?: Date,
|
|
140
|
+
maxDate?: Date
|
|
141
|
+
): boolean {
|
|
142
|
+
const target = startOfDay(targetDate);
|
|
143
|
+
|
|
144
|
+
// If both dates are provided, normalize them to ensure min <= max
|
|
145
|
+
if (minDate && maxDate) {
|
|
146
|
+
const dates = [startOfDay(minDate), startOfDay(maxDate)];
|
|
147
|
+
return isWithinInterval(target, {
|
|
148
|
+
start: min(dates),
|
|
149
|
+
end: max(dates),
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
if (minDate && isBefore(target, startOfDay(minDate))) {
|
|
154
|
+
return false;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
if (maxDate && isAfter(target, startOfDay(maxDate))) {
|
|
158
|
+
return false;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
return true;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Checks if a date is disabled based on min/max constraints and disabled dates array.
|
|
166
|
+
*/
|
|
167
|
+
export function isDateDisabled(
|
|
168
|
+
date: Date,
|
|
169
|
+
minDate?: Date,
|
|
170
|
+
maxDate?: Date,
|
|
171
|
+
disabledDates?: Date[]
|
|
172
|
+
): boolean {
|
|
173
|
+
// Check if date is outside min/max range
|
|
174
|
+
if (!isDateInRange(date, minDate, maxDate)) {
|
|
175
|
+
return true;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// Check if date is in the disabled dates array
|
|
179
|
+
if (disabledDates) {
|
|
180
|
+
return disabledDates.some(d => isSameDay(date, d));
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
return false;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
export function formatMonth(
|
|
187
|
+
year: number,
|
|
188
|
+
month: number,
|
|
189
|
+
formatStr?: string
|
|
190
|
+
): string {
|
|
191
|
+
const {monthFormat} = extractFormats(formatStr);
|
|
192
|
+
const convertedFormat = convertFormatTokens(monthFormat);
|
|
193
|
+
return format(new Date(year, month, 1), convertedFormat, {
|
|
194
|
+
locale: getUserLocale(),
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Extracts separate month and year format strings from a combined month_format, e.g. "MMM YY".
|
|
200
|
+
*/
|
|
201
|
+
export function extractFormats(format?: string): {
|
|
202
|
+
monthFormat: string;
|
|
203
|
+
yearFormat: string;
|
|
204
|
+
} {
|
|
205
|
+
if (!format) {
|
|
206
|
+
return {monthFormat: 'MMMM', yearFormat: 'YYYY'};
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// Extract month tokens (MMMM, MMM, MM, M)
|
|
210
|
+
const monthMatch = format.match(/M{1,4}/);
|
|
211
|
+
const monthFormat = monthMatch ? monthMatch[0] : 'MMMM';
|
|
212
|
+
|
|
213
|
+
// Extract year tokens (YYYY, YY)
|
|
214
|
+
const yearMatch = format.match(/Y{2,4}/);
|
|
215
|
+
const yearFormat = yearMatch ? yearMatch[0] : 'YYYY';
|
|
216
|
+
|
|
217
|
+
return {monthFormat, yearFormat};
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Generates month options for a dropdown based on a format string.
|
|
222
|
+
*/
|
|
223
|
+
export function getMonthOptions(
|
|
224
|
+
year: number,
|
|
225
|
+
formatStr?: string,
|
|
226
|
+
minDate?: Date,
|
|
227
|
+
maxDate?: Date
|
|
228
|
+
): Array<{label: string; value: number; disabled?: boolean}> {
|
|
229
|
+
const {monthFormat} = extractFormats(formatStr);
|
|
230
|
+
const convertedFormat = convertFormatTokens(monthFormat);
|
|
231
|
+
|
|
232
|
+
return Array.from({length: 12}, (_, i) => {
|
|
233
|
+
const monthStart = new Date(year, i, 1);
|
|
234
|
+
const label = format(monthStart, convertedFormat, {
|
|
235
|
+
locale: getUserLocale(),
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
// Check if this month is outside the allowed range (month-level comparison)
|
|
239
|
+
const disabled =
|
|
240
|
+
(minDate && isBefore(monthStart, startOfMonth(minDate))) ||
|
|
241
|
+
(maxDate && isAfter(monthStart, startOfMonth(maxDate)));
|
|
242
|
+
|
|
243
|
+
return {label, value: i, disabled};
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* Formats a year according to the year format extracted from month_format.
|
|
249
|
+
*/
|
|
250
|
+
export function formatYear(year: number, formatStr?: string): string {
|
|
251
|
+
const {yearFormat} = extractFormats(formatStr);
|
|
252
|
+
const convertedFormat = convertFormatTokens(yearFormat);
|
|
253
|
+
return format(new Date(year, 0, 1), convertedFormat, {
|
|
254
|
+
locale: getUserLocale(),
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Parses a year string and converts it to a full 4-digit year.
|
|
260
|
+
* Uses date-fns pivot: 2-digit years are interpreted within ±50 years of current year.
|
|
261
|
+
* Example (when current year is 2025): 00-74 → 2000-2074, 75-99 → 1975-1999
|
|
262
|
+
*/
|
|
263
|
+
export function parseYear(yearStr: string): number | undefined {
|
|
264
|
+
const formats = ['yy', 'yyyy'];
|
|
265
|
+
for (const fmt of formats) {
|
|
266
|
+
const parsed = parse(yearStr.trim(), fmt, new Date());
|
|
267
|
+
if (isValid(parsed)) {
|
|
268
|
+
return parsed.getFullYear();
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
return undefined;
|
|
272
|
+
}
|