clayui-date-picker 3.165.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.
- package/LICENSE.md +30 -0
- package/README.md +19 -0
- package/lib/cjs/DateNavigation.js +124 -0
- package/lib/cjs/DayNumber.js +116 -0
- package/lib/cjs/DaysTable.js +49 -0
- package/lib/cjs/Helpers.js +98 -0
- package/lib/cjs/Hooks.js +298 -0
- package/lib/cjs/InputDate.js +58 -0
- package/lib/cjs/Select.js +56 -0
- package/lib/cjs/TimePicker.js +82 -0
- package/lib/cjs/Weekday.js +38 -0
- package/lib/cjs/WeekdayHeader.js +57 -0
- package/lib/cjs/index.js +705 -0
- package/lib/cjs/types.js +33 -0
- package/lib/esm/DateNavigation.js +94 -0
- package/lib/esm/DayNumber.js +86 -0
- package/lib/esm/DaysTable.js +19 -0
- package/lib/esm/Helpers.js +68 -0
- package/lib/esm/Hooks.js +278 -0
- package/lib/esm/InputDate.js +28 -0
- package/lib/esm/Select.js +26 -0
- package/lib/esm/TimePicker.js +52 -0
- package/lib/esm/Weekday.js +8 -0
- package/lib/esm/WeekdayHeader.js +27 -0
- package/lib/esm/index.js +697 -0
- package/lib/esm/types.js +13 -0
- package/package.json +51 -0
package/lib/esm/index.js
ADDED
|
@@ -0,0 +1,697 @@
|
|
|
1
|
+
import Button from "@clayui/button";
|
|
2
|
+
import { KeyboardArrowsIndicator } from "@clayui/core";
|
|
3
|
+
import DropDown from "@clayui/drop-down";
|
|
4
|
+
import { ClayInput } from "@clayui/form";
|
|
5
|
+
import Icon from "@clayui/icon";
|
|
6
|
+
import {
|
|
7
|
+
ClayPortal,
|
|
8
|
+
FocusScope,
|
|
9
|
+
sub,
|
|
10
|
+
useControlledState,
|
|
11
|
+
useId
|
|
12
|
+
} from "@clayui/shared";
|
|
13
|
+
import React, { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
14
|
+
import DateNavigation from "./DateNavigation";
|
|
15
|
+
import DayNumber from "./DayNumber";
|
|
16
|
+
import DaysTable from "./DaysTable";
|
|
17
|
+
import {
|
|
18
|
+
clamp,
|
|
19
|
+
formatDate,
|
|
20
|
+
isAfter,
|
|
21
|
+
isBefore,
|
|
22
|
+
isSameDay,
|
|
23
|
+
isValid,
|
|
24
|
+
parseDate,
|
|
25
|
+
range as createRange,
|
|
26
|
+
setDate,
|
|
27
|
+
setMonth
|
|
28
|
+
} from "./Helpers";
|
|
29
|
+
import {
|
|
30
|
+
useCalendarNavigation,
|
|
31
|
+
useCurrentTime,
|
|
32
|
+
useDaysSelected,
|
|
33
|
+
useWeeks
|
|
34
|
+
} from "./Hooks";
|
|
35
|
+
import InputDate from "./InputDate";
|
|
36
|
+
import TimePicker from "./TimePicker";
|
|
37
|
+
import Weekday from "./Weekday";
|
|
38
|
+
import WeekdayHeader from "./WeekdayHeader";
|
|
39
|
+
var FirstDayOfWeek = /* @__PURE__ */ ((FirstDayOfWeek2) => {
|
|
40
|
+
FirstDayOfWeek2[FirstDayOfWeek2["Sunday"] = 0] = "Sunday";
|
|
41
|
+
FirstDayOfWeek2[FirstDayOfWeek2["Monday"] = 1] = "Monday";
|
|
42
|
+
FirstDayOfWeek2[FirstDayOfWeek2["Tuesday"] = 2] = "Tuesday";
|
|
43
|
+
FirstDayOfWeek2[FirstDayOfWeek2["Wednesday"] = 3] = "Wednesday";
|
|
44
|
+
FirstDayOfWeek2[FirstDayOfWeek2["Thursday"] = 4] = "Thursday";
|
|
45
|
+
FirstDayOfWeek2[FirstDayOfWeek2["Friday"] = 5] = "Friday";
|
|
46
|
+
FirstDayOfWeek2[FirstDayOfWeek2["Saturday"] = 6] = "Saturday";
|
|
47
|
+
return FirstDayOfWeek2;
|
|
48
|
+
})(FirstDayOfWeek || {});
|
|
49
|
+
const DEFAULT_DATE_TIME = {
|
|
50
|
+
hours: 12,
|
|
51
|
+
milliseconds: 0,
|
|
52
|
+
minutes: 0,
|
|
53
|
+
seconds: 0
|
|
54
|
+
};
|
|
55
|
+
const NEW_DATE = /* @__PURE__ */ new Date();
|
|
56
|
+
const TIME_FORMAT = "HH:mm";
|
|
57
|
+
const TIME_FORMAT_12H = "hh:mm aa";
|
|
58
|
+
function normalizeTime(date) {
|
|
59
|
+
return setDate(date, { hours: 12, milliseconds: 0, minutes: 0, seconds: 0 });
|
|
60
|
+
}
|
|
61
|
+
const DatePicker = React.forwardRef(
|
|
62
|
+
({
|
|
63
|
+
ariaLabels = {
|
|
64
|
+
buttonChooseDate: "Choose date",
|
|
65
|
+
buttonDot: "Select current date",
|
|
66
|
+
buttonNextMonth: "Select the next month",
|
|
67
|
+
buttonPreviousMonth: "Select the previous month",
|
|
68
|
+
chooseDate: "Use the calendar to choose a Date. Current selection {0}",
|
|
69
|
+
dialog: "Choose date",
|
|
70
|
+
openCalendar: "Open Calendar Picker",
|
|
71
|
+
outOfRange: "The entered value is outside the allowed range and was not applied",
|
|
72
|
+
selectMonth: "Select a month",
|
|
73
|
+
selectYear: "Select a year"
|
|
74
|
+
},
|
|
75
|
+
dateFormat = "yyyy-MM-dd",
|
|
76
|
+
defaultExpanded,
|
|
77
|
+
defaultMonth,
|
|
78
|
+
defaultValue,
|
|
79
|
+
disabled,
|
|
80
|
+
displayKeyboardArrowsIndicator = false,
|
|
81
|
+
expanded,
|
|
82
|
+
firstDayOfWeek = 0,
|
|
83
|
+
footerElement,
|
|
84
|
+
id,
|
|
85
|
+
initialExpanded = false,
|
|
86
|
+
initialMonth,
|
|
87
|
+
inputName,
|
|
88
|
+
max,
|
|
89
|
+
min,
|
|
90
|
+
months = [
|
|
91
|
+
"January",
|
|
92
|
+
"February",
|
|
93
|
+
"March",
|
|
94
|
+
"April",
|
|
95
|
+
"May",
|
|
96
|
+
"June",
|
|
97
|
+
"July",
|
|
98
|
+
"August",
|
|
99
|
+
"September",
|
|
100
|
+
"October",
|
|
101
|
+
"November",
|
|
102
|
+
"December"
|
|
103
|
+
],
|
|
104
|
+
onChange,
|
|
105
|
+
onExpandedChange,
|
|
106
|
+
onNavigation = () => {
|
|
107
|
+
},
|
|
108
|
+
onValueChange,
|
|
109
|
+
placeholder,
|
|
110
|
+
range,
|
|
111
|
+
spritemap,
|
|
112
|
+
time = false,
|
|
113
|
+
timezone,
|
|
114
|
+
use12Hours = false,
|
|
115
|
+
useNative = false,
|
|
116
|
+
value,
|
|
117
|
+
weekdaysShort = ["S", "M", "T", "W", "T", "F", "S"],
|
|
118
|
+
years = {
|
|
119
|
+
end: NEW_DATE.getFullYear(),
|
|
120
|
+
start: NEW_DATE.getFullYear()
|
|
121
|
+
},
|
|
122
|
+
yearsCheck = true,
|
|
123
|
+
...otherProps
|
|
124
|
+
}, ref) => {
|
|
125
|
+
const [parsedMin, parsedMax] = useMemo(() => {
|
|
126
|
+
const parseBound = (value2) => {
|
|
127
|
+
if (!value2) {
|
|
128
|
+
return void 0;
|
|
129
|
+
}
|
|
130
|
+
const parsed = parseDate(
|
|
131
|
+
value2,
|
|
132
|
+
time ? `${dateFormat} ${use12Hours ? TIME_FORMAT_12H : TIME_FORMAT}` : dateFormat,
|
|
133
|
+
NEW_DATE
|
|
134
|
+
);
|
|
135
|
+
return isValid(parsed) ? parsed : void 0;
|
|
136
|
+
};
|
|
137
|
+
const minDate = parseBound(min);
|
|
138
|
+
const maxDate = parseBound(max);
|
|
139
|
+
if (minDate && maxDate && !isBefore(minDate, maxDate)) {
|
|
140
|
+
console.warn(
|
|
141
|
+
"ClayDatePicker: `min` must be earlier than `max`. Both bounds will be ignored."
|
|
142
|
+
);
|
|
143
|
+
return [void 0, void 0];
|
|
144
|
+
}
|
|
145
|
+
return [minDate, maxDate];
|
|
146
|
+
}, [min, max, dateFormat, time, use12Hours]);
|
|
147
|
+
const isDayDisabled = useCallback(
|
|
148
|
+
(date) => {
|
|
149
|
+
if (parsedMin && isBefore(date, parsedMin) && !isSameDay(date, parsedMin)) {
|
|
150
|
+
return true;
|
|
151
|
+
}
|
|
152
|
+
if (parsedMax && isAfter(date, parsedMax) && !isSameDay(date, parsedMax)) {
|
|
153
|
+
return true;
|
|
154
|
+
}
|
|
155
|
+
return false;
|
|
156
|
+
},
|
|
157
|
+
[parsedMin, parsedMax]
|
|
158
|
+
);
|
|
159
|
+
const isDateTimeOutOfRange = useCallback(
|
|
160
|
+
(date) => {
|
|
161
|
+
if (parsedMin && isBefore(date, parsedMin)) {
|
|
162
|
+
return true;
|
|
163
|
+
}
|
|
164
|
+
if (parsedMax && isAfter(date, parsedMax)) {
|
|
165
|
+
return true;
|
|
166
|
+
}
|
|
167
|
+
return false;
|
|
168
|
+
},
|
|
169
|
+
[parsedMin, parsedMax]
|
|
170
|
+
);
|
|
171
|
+
const getDefaultMonth = useCallback(
|
|
172
|
+
() => clamp(
|
|
173
|
+
defaultMonth ?? initialMonth ?? /* @__PURE__ */ new Date(),
|
|
174
|
+
parsedMin,
|
|
175
|
+
parsedMax
|
|
176
|
+
),
|
|
177
|
+
[defaultMonth, initialMonth, parsedMin, parsedMax]
|
|
178
|
+
);
|
|
179
|
+
const [internalValue, setValue, isUncontrolled] = useControlledState({
|
|
180
|
+
defaultName: "defaultValue",
|
|
181
|
+
defaultValue,
|
|
182
|
+
handleName: "onChange",
|
|
183
|
+
name: "value",
|
|
184
|
+
onChange: onChange ?? onValueChange,
|
|
185
|
+
value
|
|
186
|
+
});
|
|
187
|
+
const [daysSelected, setDaysSelected] = useDaysSelected(() => {
|
|
188
|
+
if (internalValue) {
|
|
189
|
+
const days = hasDaysSelected({
|
|
190
|
+
checkRangeYears: yearsCheck,
|
|
191
|
+
dateFormat,
|
|
192
|
+
is12Hours: use12Hours,
|
|
193
|
+
isTime: time,
|
|
194
|
+
value: internalValue,
|
|
195
|
+
years
|
|
196
|
+
});
|
|
197
|
+
if (days) {
|
|
198
|
+
return [normalizeTime(days[0]), normalizeTime(days[1])];
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
const date = normalizeTime(getDefaultMonth());
|
|
202
|
+
return [date, date];
|
|
203
|
+
});
|
|
204
|
+
const [currentMonth, setCurrentMonth] = useState(
|
|
205
|
+
() => (
|
|
206
|
+
// Normalize the date to always set noon to avoid time zone problems
|
|
207
|
+
// and to the 1st of the month.
|
|
208
|
+
setDate(daysSelected[0], { date: 1, ...DEFAULT_DATE_TIME })
|
|
209
|
+
)
|
|
210
|
+
);
|
|
211
|
+
const chooseDateRef = useRef(null);
|
|
212
|
+
const [currentTime, setCurrentTime] = useCurrentTime(() => {
|
|
213
|
+
if (time && internalValue) {
|
|
214
|
+
const [startDate] = fromStringToRange(
|
|
215
|
+
internalValue,
|
|
216
|
+
`${dateFormat} ${use12Hours ? TIME_FORMAT_12H : TIME_FORMAT}`,
|
|
217
|
+
NEW_DATE
|
|
218
|
+
);
|
|
219
|
+
if (startDate.toString() !== "Invalid Date") {
|
|
220
|
+
const hours = use12Hours ? formatDate(startDate, "hh") : formatDate(startDate, "HH");
|
|
221
|
+
const minutes = formatDate(startDate, "mm");
|
|
222
|
+
return use12Hours ? `${hours}:${minutes} ${formatDate(startDate, "a")}` : `${hours}:${minutes}`;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
return "--:--";
|
|
226
|
+
}, use12Hours);
|
|
227
|
+
const [weeks, setWeeks] = useWeeks(currentMonth, firstDayOfWeek);
|
|
228
|
+
const [expandedValue, setExpandedValue] = useControlledState({
|
|
229
|
+
defaultName: "defaultExpanded",
|
|
230
|
+
defaultValue: defaultExpanded === void 0 ? initialExpanded : defaultExpanded,
|
|
231
|
+
handleName: "onExpandedChange",
|
|
232
|
+
name: "expanded",
|
|
233
|
+
onChange: onExpandedChange,
|
|
234
|
+
value: expanded
|
|
235
|
+
});
|
|
236
|
+
const triggerElementRef = useRef(null);
|
|
237
|
+
const liveRegionRef = useRef(null);
|
|
238
|
+
const announceOutOfRange = useCallback(() => {
|
|
239
|
+
const node = liveRegionRef.current;
|
|
240
|
+
if (!node || !ariaLabels.outOfRange) {
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
node.textContent = "";
|
|
244
|
+
node.textContent = ariaLabels.outOfRange;
|
|
245
|
+
}, [ariaLabels.outOfRange]);
|
|
246
|
+
const menuElementRef = useRef(null);
|
|
247
|
+
const changeMonth = (date) => {
|
|
248
|
+
const dateNormalized = setDate(date, {
|
|
249
|
+
date: 1,
|
|
250
|
+
...DEFAULT_DATE_TIME
|
|
251
|
+
});
|
|
252
|
+
setCurrentMonth(dateNormalized);
|
|
253
|
+
onNavigation(dateNormalized);
|
|
254
|
+
if (!useNative) {
|
|
255
|
+
setWeeks(dateNormalized);
|
|
256
|
+
}
|
|
257
|
+
};
|
|
258
|
+
const timePickerConfig = useMemo(() => {
|
|
259
|
+
if (!time || use12Hours || !parsedMin && !parsedMax) {
|
|
260
|
+
return void 0;
|
|
261
|
+
}
|
|
262
|
+
const [day] = daysSelected;
|
|
263
|
+
let hoursMin = 0;
|
|
264
|
+
let hoursMax = 23;
|
|
265
|
+
let minutesMin = 0;
|
|
266
|
+
let minutesMax = 59;
|
|
267
|
+
const currentHours = Number(currentTime.split(":")[0]);
|
|
268
|
+
if (parsedMin && isSameDay(day, parsedMin)) {
|
|
269
|
+
hoursMin = parsedMin.getHours();
|
|
270
|
+
if (currentHours === hoursMin) {
|
|
271
|
+
minutesMin = parsedMin.getMinutes();
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
if (parsedMax && isSameDay(day, parsedMax)) {
|
|
275
|
+
hoursMax = parsedMax.getHours();
|
|
276
|
+
if (currentHours === hoursMax) {
|
|
277
|
+
minutesMax = parsedMax.getMinutes();
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
return {
|
|
281
|
+
use12Hours: {
|
|
282
|
+
ampm: { am: "AM", pm: "PM" },
|
|
283
|
+
hours: { max: 12, min: 1 },
|
|
284
|
+
minutes: { max: 59, min: 0 }
|
|
285
|
+
},
|
|
286
|
+
use24Hours: {
|
|
287
|
+
ampm: { am: "AM", pm: "PM" },
|
|
288
|
+
hours: { max: hoursMax, min: hoursMin },
|
|
289
|
+
minutes: { max: minutesMax, min: minutesMin }
|
|
290
|
+
}
|
|
291
|
+
};
|
|
292
|
+
}, [time, use12Hours, daysSelected, currentTime, parsedMin, parsedMax]);
|
|
293
|
+
const memoizedYears = useMemo(
|
|
294
|
+
() => createRange(years).map((elem) => {
|
|
295
|
+
return {
|
|
296
|
+
label: elem,
|
|
297
|
+
value: elem
|
|
298
|
+
};
|
|
299
|
+
}),
|
|
300
|
+
[years]
|
|
301
|
+
);
|
|
302
|
+
const memoizedMonths = useMemo(
|
|
303
|
+
() => months.map((month, index) => {
|
|
304
|
+
return {
|
|
305
|
+
label: month,
|
|
306
|
+
value: index
|
|
307
|
+
};
|
|
308
|
+
}),
|
|
309
|
+
[months]
|
|
310
|
+
);
|
|
311
|
+
const handleDayClicked = (date) => {
|
|
312
|
+
if (isDayDisabled(date)) {
|
|
313
|
+
announceOutOfRange();
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
const [startDate, endDate] = daysSelected;
|
|
317
|
+
let newDaysSelected;
|
|
318
|
+
let daysSelectedToString;
|
|
319
|
+
if (range) {
|
|
320
|
+
if (startDate.toString() !== endDate.toString()) {
|
|
321
|
+
newDaysSelected = [date, date];
|
|
322
|
+
} else if (date < startDate) {
|
|
323
|
+
newDaysSelected = [date, endDate];
|
|
324
|
+
} else {
|
|
325
|
+
newDaysSelected = [startDate, date];
|
|
326
|
+
}
|
|
327
|
+
daysSelectedToString = fromRangeToString(
|
|
328
|
+
newDaysSelected,
|
|
329
|
+
dateFormat
|
|
330
|
+
);
|
|
331
|
+
const [newStartDate, newEndDate] = newDaysSelected;
|
|
332
|
+
if (newStartDate.getMonth() !== newEndDate.getMonth()) {
|
|
333
|
+
changeMonth(startDate);
|
|
334
|
+
}
|
|
335
|
+
} else {
|
|
336
|
+
newDaysSelected = [date, date];
|
|
337
|
+
daysSelectedToString = formatDate(date, dateFormat);
|
|
338
|
+
if (time) {
|
|
339
|
+
daysSelectedToString = `${daysSelectedToString} ${currentTime}`;
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
setDaysSelected(newDaysSelected);
|
|
343
|
+
setValue(daysSelectedToString);
|
|
344
|
+
};
|
|
345
|
+
const updateDate = useCallback(
|
|
346
|
+
(value2) => {
|
|
347
|
+
const currentDate = getDefaultMonth();
|
|
348
|
+
if (!value2) {
|
|
349
|
+
changeMonth(currentDate);
|
|
350
|
+
setDaysSelected([currentDate, currentDate]);
|
|
351
|
+
if (time) {
|
|
352
|
+
setCurrentTime(
|
|
353
|
+
"--",
|
|
354
|
+
"--",
|
|
355
|
+
use12Hours ? "--" : void 0
|
|
356
|
+
);
|
|
357
|
+
}
|
|
358
|
+
} else {
|
|
359
|
+
const days = hasDaysSelected({
|
|
360
|
+
checkRangeYears: yearsCheck,
|
|
361
|
+
dateFormat,
|
|
362
|
+
is12Hours: use12Hours,
|
|
363
|
+
isTime: time,
|
|
364
|
+
value: value2,
|
|
365
|
+
years
|
|
366
|
+
});
|
|
367
|
+
if (days) {
|
|
368
|
+
const [startDate, endDate] = days;
|
|
369
|
+
const referenceStart = time ? startDate : setDate(startDate, {
|
|
370
|
+
hours: 12,
|
|
371
|
+
milliseconds: 0,
|
|
372
|
+
minutes: 0,
|
|
373
|
+
seconds: 0
|
|
374
|
+
});
|
|
375
|
+
const referenceEnd = time ? endDate : setDate(endDate, {
|
|
376
|
+
hours: 12,
|
|
377
|
+
milliseconds: 0,
|
|
378
|
+
minutes: 0,
|
|
379
|
+
seconds: 0
|
|
380
|
+
});
|
|
381
|
+
const startOutOfRange = time ? isDateTimeOutOfRange(referenceStart) : isDayDisabled(referenceStart);
|
|
382
|
+
const endOutOfRange = time ? isDateTimeOutOfRange(referenceEnd) : isDayDisabled(referenceEnd);
|
|
383
|
+
if (startOutOfRange || endOutOfRange) {
|
|
384
|
+
announceOutOfRange();
|
|
385
|
+
return;
|
|
386
|
+
}
|
|
387
|
+
if (time) {
|
|
388
|
+
setCurrentTime(
|
|
389
|
+
startDate.getHours(),
|
|
390
|
+
startDate.getMinutes(),
|
|
391
|
+
use12Hours ? formatDate(
|
|
392
|
+
startDate,
|
|
393
|
+
"a"
|
|
394
|
+
) : void 0
|
|
395
|
+
);
|
|
396
|
+
} else {
|
|
397
|
+
changeMonth(startDate);
|
|
398
|
+
setDaysSelected([startDate, endDate]);
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
},
|
|
403
|
+
[
|
|
404
|
+
dateFormat,
|
|
405
|
+
use12Hours,
|
|
406
|
+
time,
|
|
407
|
+
years,
|
|
408
|
+
yearsCheck,
|
|
409
|
+
isDayDisabled,
|
|
410
|
+
isDateTimeOutOfRange,
|
|
411
|
+
announceOutOfRange
|
|
412
|
+
]
|
|
413
|
+
);
|
|
414
|
+
const inputChange = (event) => {
|
|
415
|
+
const { value: value2 } = event.target;
|
|
416
|
+
updateDate(value2);
|
|
417
|
+
setValue(value2);
|
|
418
|
+
};
|
|
419
|
+
useEffect(() => {
|
|
420
|
+
if (!isUncontrolled) {
|
|
421
|
+
updateDate(internalValue);
|
|
422
|
+
}
|
|
423
|
+
}, [isUncontrolled, internalValue]);
|
|
424
|
+
const handleDotClicked = () => {
|
|
425
|
+
const currentDateTime = getDefaultMonth();
|
|
426
|
+
if (isDayDisabled(currentDateTime)) {
|
|
427
|
+
announceOutOfRange();
|
|
428
|
+
return;
|
|
429
|
+
}
|
|
430
|
+
const [, endDate] = daysSelected;
|
|
431
|
+
changeMonth(currentDateTime);
|
|
432
|
+
const newDaysSelected = range && endDate < currentDateTime ? [endDate, currentDateTime] : [currentDateTime, endDate];
|
|
433
|
+
let dateFormatted;
|
|
434
|
+
if (range) {
|
|
435
|
+
dateFormatted = fromRangeToString(newDaysSelected, dateFormat);
|
|
436
|
+
} else if (time) {
|
|
437
|
+
dateFormatted = `${formatDate(
|
|
438
|
+
currentDateTime,
|
|
439
|
+
dateFormat
|
|
440
|
+
)} ${setCurrentTime(
|
|
441
|
+
currentDateTime.getHours(),
|
|
442
|
+
currentDateTime.getMinutes(),
|
|
443
|
+
use12Hours ? formatDate(currentDateTime, "a") : void 0
|
|
444
|
+
)}`;
|
|
445
|
+
} else {
|
|
446
|
+
dateFormatted = formatDate(currentDateTime, dateFormat);
|
|
447
|
+
}
|
|
448
|
+
setDaysSelected(newDaysSelected);
|
|
449
|
+
setValue(dateFormatted);
|
|
450
|
+
};
|
|
451
|
+
const handleTimeChange = (hours, minutes, ampm) => {
|
|
452
|
+
const [day] = daysSelected;
|
|
453
|
+
if (hours === "--" && typeof minutes === "number") {
|
|
454
|
+
hours = 0;
|
|
455
|
+
}
|
|
456
|
+
if (minutes === "--" && typeof hours === "number") {
|
|
457
|
+
minutes = 0;
|
|
458
|
+
}
|
|
459
|
+
if (typeof hours === "number" && typeof minutes === "number" && (parsedMin || parsedMax)) {
|
|
460
|
+
let hours24 = hours;
|
|
461
|
+
if (use12Hours) {
|
|
462
|
+
if (ampm === "PM" && hours < 12) {
|
|
463
|
+
hours24 = hours + 12;
|
|
464
|
+
} else if (ampm === "AM" && hours === 12) {
|
|
465
|
+
hours24 = 0;
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
const dateTimeSelected = setDate(day, {
|
|
469
|
+
hours: hours24,
|
|
470
|
+
minutes
|
|
471
|
+
});
|
|
472
|
+
if (isDateTimeOutOfRange(dateTimeSelected)) {
|
|
473
|
+
announceOutOfRange();
|
|
474
|
+
return;
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
if (internalValue) {
|
|
478
|
+
let date = typeof hours === "string" && typeof minutes === "string" ? `${formatDate(day, dateFormat)} ${hours}:${minutes}` : formatDate(
|
|
479
|
+
setDate(day, { hours, minutes }),
|
|
480
|
+
`${dateFormat} ${TIME_FORMAT}`
|
|
481
|
+
);
|
|
482
|
+
if (use12Hours) {
|
|
483
|
+
date += ` ${ampm}`;
|
|
484
|
+
}
|
|
485
|
+
setValue(date);
|
|
486
|
+
}
|
|
487
|
+
setCurrentTime(hours, minutes, use12Hours ? ampm : void 0);
|
|
488
|
+
};
|
|
489
|
+
const handleCalendarButtonClicked = () => setExpandedValue(!expandedValue);
|
|
490
|
+
const calendarNavigation = useCalendarNavigation({
|
|
491
|
+
daysSelected,
|
|
492
|
+
isOpen: expandedValue,
|
|
493
|
+
onChangeMonth: (month, year) => {
|
|
494
|
+
if (typeof year === "number") {
|
|
495
|
+
changeMonth(
|
|
496
|
+
setDate(currentMonth, {
|
|
497
|
+
year: currentMonth.getFullYear() + year
|
|
498
|
+
})
|
|
499
|
+
);
|
|
500
|
+
} else {
|
|
501
|
+
const date = setMonth(memoizedYears, month, currentMonth);
|
|
502
|
+
if (date) {
|
|
503
|
+
changeMonth(date);
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
},
|
|
507
|
+
weeks
|
|
508
|
+
});
|
|
509
|
+
const ariaControls = useId();
|
|
510
|
+
return /* @__PURE__ */ React.createElement(FocusScope, { arrowKeysUpDown: false }, /* @__PURE__ */ React.createElement("div", { className: "date-picker" }, /* @__PURE__ */ React.createElement(
|
|
511
|
+
"div",
|
|
512
|
+
{
|
|
513
|
+
"aria-atomic": "true",
|
|
514
|
+
"aria-live": "polite",
|
|
515
|
+
className: "sr-only",
|
|
516
|
+
ref: liveRegionRef
|
|
517
|
+
}
|
|
518
|
+
), /* @__PURE__ */ React.createElement(ClayInput.Group, { ref: triggerElementRef }, /* @__PURE__ */ React.createElement(ClayInput.GroupItem, { className: "input-group-item-focusable" }, /* @__PURE__ */ React.createElement(
|
|
519
|
+
InputDate,
|
|
520
|
+
{
|
|
521
|
+
...otherProps,
|
|
522
|
+
ariaLabel: ariaLabels.input,
|
|
523
|
+
disabled,
|
|
524
|
+
id,
|
|
525
|
+
inputName,
|
|
526
|
+
onChange: inputChange,
|
|
527
|
+
placeholder,
|
|
528
|
+
ref,
|
|
529
|
+
useNative,
|
|
530
|
+
value: internalValue
|
|
531
|
+
}
|
|
532
|
+
), !useNative && /* @__PURE__ */ React.createElement(ClayInput.GroupInsetItem, { after: true }, /* @__PURE__ */ React.createElement(
|
|
533
|
+
Button,
|
|
534
|
+
{
|
|
535
|
+
"aria-controls": ariaControls,
|
|
536
|
+
"aria-expanded": expandedValue,
|
|
537
|
+
"aria-haspopup": "dialog",
|
|
538
|
+
"aria-label": ariaLabels.buttonChooseDate,
|
|
539
|
+
className: "date-picker-dropdown-toggle",
|
|
540
|
+
"data-testid": "date-button",
|
|
541
|
+
disabled,
|
|
542
|
+
displayType: "unstyled",
|
|
543
|
+
onClick: handleCalendarButtonClicked,
|
|
544
|
+
ref: chooseDateRef,
|
|
545
|
+
title: ariaLabels.openCalendar
|
|
546
|
+
},
|
|
547
|
+
/* @__PURE__ */ React.createElement(
|
|
548
|
+
Icon,
|
|
549
|
+
{
|
|
550
|
+
spritemap,
|
|
551
|
+
symbol: "calendar"
|
|
552
|
+
}
|
|
553
|
+
)
|
|
554
|
+
)))), !useNative && /* @__PURE__ */ React.createElement(
|
|
555
|
+
DropDown.Menu,
|
|
556
|
+
{
|
|
557
|
+
active: expandedValue,
|
|
558
|
+
alignElementRef: triggerElementRef,
|
|
559
|
+
"aria-label": ariaLabels.dialog,
|
|
560
|
+
className: "date-picker date-picker-dropdown-menu",
|
|
561
|
+
"data-testid": "dropdown",
|
|
562
|
+
id: ariaControls,
|
|
563
|
+
lock: true,
|
|
564
|
+
onActiveChange: setExpandedValue,
|
|
565
|
+
ref: menuElementRef,
|
|
566
|
+
role: "dialog",
|
|
567
|
+
triggerRef: chooseDateRef
|
|
568
|
+
},
|
|
569
|
+
/* @__PURE__ */ React.createElement(
|
|
570
|
+
"div",
|
|
571
|
+
{
|
|
572
|
+
"aria-label": ariaLabels.chooseDate && sub(ariaLabels.chooseDate, [
|
|
573
|
+
formatDate(currentMonth, "MMMM yyyy")
|
|
574
|
+
]),
|
|
575
|
+
className: "date-picker-calendar",
|
|
576
|
+
role: "group"
|
|
577
|
+
},
|
|
578
|
+
/* @__PURE__ */ React.createElement(
|
|
579
|
+
DateNavigation,
|
|
580
|
+
{
|
|
581
|
+
ariaLabels,
|
|
582
|
+
currentMonth,
|
|
583
|
+
disabled,
|
|
584
|
+
months: memoizedMonths,
|
|
585
|
+
onDotClicked: handleDotClicked,
|
|
586
|
+
onMonthChange: changeMonth,
|
|
587
|
+
spritemap,
|
|
588
|
+
years: memoizedYears
|
|
589
|
+
}
|
|
590
|
+
),
|
|
591
|
+
/* @__PURE__ */ React.createElement(
|
|
592
|
+
"div",
|
|
593
|
+
{
|
|
594
|
+
...calendarNavigation.gridProps,
|
|
595
|
+
className: "date-picker-calendar-body",
|
|
596
|
+
role: "grid"
|
|
597
|
+
},
|
|
598
|
+
/* @__PURE__ */ React.createElement(
|
|
599
|
+
WeekdayHeader,
|
|
600
|
+
{
|
|
601
|
+
firstDayOfWeek,
|
|
602
|
+
weekdaysShort
|
|
603
|
+
},
|
|
604
|
+
({ key, weekday }) => /* @__PURE__ */ React.createElement(
|
|
605
|
+
Weekday,
|
|
606
|
+
{
|
|
607
|
+
key,
|
|
608
|
+
weekday
|
|
609
|
+
}
|
|
610
|
+
)
|
|
611
|
+
),
|
|
612
|
+
/* @__PURE__ */ React.createElement(DaysTable, { weeks }, ({ day, key }) => /* @__PURE__ */ React.createElement(
|
|
613
|
+
DayNumber,
|
|
614
|
+
{
|
|
615
|
+
day,
|
|
616
|
+
daysSelected,
|
|
617
|
+
disabled,
|
|
618
|
+
index: key,
|
|
619
|
+
isFocused: calendarNavigation.isFocused(
|
|
620
|
+
day
|
|
621
|
+
),
|
|
622
|
+
key,
|
|
623
|
+
onClick: handleDayClicked,
|
|
624
|
+
outOfRange: isDayDisabled(
|
|
625
|
+
day.date
|
|
626
|
+
),
|
|
627
|
+
range
|
|
628
|
+
}
|
|
629
|
+
))
|
|
630
|
+
),
|
|
631
|
+
(footerElement || time) && /* @__PURE__ */ React.createElement("div", { className: "date-picker-calendar-footer" }, time && /* @__PURE__ */ React.createElement(
|
|
632
|
+
TimePicker,
|
|
633
|
+
{
|
|
634
|
+
config: timePickerConfig,
|
|
635
|
+
currentTime,
|
|
636
|
+
disabled,
|
|
637
|
+
onTimeChange: handleTimeChange,
|
|
638
|
+
spritemap,
|
|
639
|
+
timezone,
|
|
640
|
+
use12Hours
|
|
641
|
+
}
|
|
642
|
+
), !time && footerElement && React.Children.only(
|
|
643
|
+
footerElement({ spritemap })
|
|
644
|
+
))
|
|
645
|
+
)
|
|
646
|
+
), !useNative && expandedValue && displayKeyboardArrowsIndicator && /* @__PURE__ */ React.createElement(ClayPortal, null, /* @__PURE__ */ React.createElement(
|
|
647
|
+
KeyboardArrowsIndicator,
|
|
648
|
+
{
|
|
649
|
+
anchorRef: menuElementRef,
|
|
650
|
+
direction: "all"
|
|
651
|
+
}
|
|
652
|
+
))));
|
|
653
|
+
}
|
|
654
|
+
);
|
|
655
|
+
const RANGE_SEPARATOR = " - ";
|
|
656
|
+
function isYearWithinYears(year, years) {
|
|
657
|
+
return years.start <= year && year <= years.end;
|
|
658
|
+
}
|
|
659
|
+
function fromStringToRange(value, dateFormat, referenceDate) {
|
|
660
|
+
const [startDateString, endDateString] = value.split(RANGE_SEPARATOR);
|
|
661
|
+
const startDate = parseDate(startDateString, dateFormat, referenceDate);
|
|
662
|
+
return [
|
|
663
|
+
startDate,
|
|
664
|
+
endDateString ? parseDate(endDateString, dateFormat, referenceDate) : startDate
|
|
665
|
+
];
|
|
666
|
+
}
|
|
667
|
+
function hasDaysSelected({
|
|
668
|
+
checkRangeYears,
|
|
669
|
+
dateFormat,
|
|
670
|
+
is12Hours,
|
|
671
|
+
isTime,
|
|
672
|
+
value,
|
|
673
|
+
years
|
|
674
|
+
}) {
|
|
675
|
+
const [startDate, endDate] = fromStringToRange(
|
|
676
|
+
value,
|
|
677
|
+
isTime ? `${dateFormat} ${is12Hours ? TIME_FORMAT_12H : TIME_FORMAT}` : dateFormat,
|
|
678
|
+
NEW_DATE
|
|
679
|
+
);
|
|
680
|
+
const isValidYear = checkRangeYears ? isYearWithinYears(startDate.getFullYear(), years) && isYearWithinYears(endDate.getFullYear(), years) : true;
|
|
681
|
+
if (isValid(startDate) && isValid(endDate) && isValidYear) {
|
|
682
|
+
return [startDate, endDate];
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
function fromRangeToString(range, dateFormat) {
|
|
686
|
+
const [startDate, endDate] = range;
|
|
687
|
+
return `${formatDate(startDate, dateFormat)}${RANGE_SEPARATOR}${formatDate(
|
|
688
|
+
endDate,
|
|
689
|
+
dateFormat
|
|
690
|
+
)}`;
|
|
691
|
+
}
|
|
692
|
+
DatePicker.displayName = "ClayDatePicker";
|
|
693
|
+
var index_default = DatePicker;
|
|
694
|
+
export {
|
|
695
|
+
FirstDayOfWeek,
|
|
696
|
+
index_default as default
|
|
697
|
+
};
|
package/lib/esm/types.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
var FirstDayOfWeek = /* @__PURE__ */ ((FirstDayOfWeek2) => {
|
|
2
|
+
FirstDayOfWeek2[FirstDayOfWeek2["Sunday"] = 0] = "Sunday";
|
|
3
|
+
FirstDayOfWeek2[FirstDayOfWeek2["Monday"] = 1] = "Monday";
|
|
4
|
+
FirstDayOfWeek2[FirstDayOfWeek2["Tuesday"] = 2] = "Tuesday";
|
|
5
|
+
FirstDayOfWeek2[FirstDayOfWeek2["Wednesday"] = 3] = "Wednesday";
|
|
6
|
+
FirstDayOfWeek2[FirstDayOfWeek2["Thursday"] = 4] = "Thursday";
|
|
7
|
+
FirstDayOfWeek2[FirstDayOfWeek2["Friday"] = 5] = "Friday";
|
|
8
|
+
FirstDayOfWeek2[FirstDayOfWeek2["Saturday"] = 6] = "Saturday";
|
|
9
|
+
return FirstDayOfWeek2;
|
|
10
|
+
})(FirstDayOfWeek || {});
|
|
11
|
+
export {
|
|
12
|
+
FirstDayOfWeek
|
|
13
|
+
};
|