|
98 | 98 | */ |
99 | 99 | export let onDayClick = () => {}; |
100 | 100 |
|
| 101 | + /** |
| 102 | + * Callback function to handle the navigation click event for months and years |
| 103 | + * @type {(event: Object) => void} |
| 104 | + */ |
| 105 | + export let onNavigationChange = () => {}; |
| 106 | +
|
101 | 107 | /** |
102 | 108 | * Indicates whether the date picker should always be shown. |
103 | 109 | * @type {boolean} |
|
334 | 340 | endDateCalendar = endDateCalendar; |
335 | 341 | }; |
336 | 342 |
|
| 343 | + /** |
| 344 | + * Handles the navigation click event for months and years |
| 345 | + * @param {string} direction - The direction of the navigation (previous or next). |
| 346 | + * @param {string} type - The type of navigation (month or year). |
| 347 | + */ |
| 348 | + const onNavigation = async (direction, type) => { |
| 349 | + await tick(); |
| 350 | +
|
| 351 | + const initial = new Date(defaultYear, defaultMonth); |
| 352 | + const initialDayOffMonth = '01'; |
| 353 | +
|
| 354 | + let current = new Date(startDateYear, startDateMonth); |
| 355 | + let month = startDateMonth + 1; |
| 356 | +
|
| 357 | + const calendar = isMultipane ? endDateCalendar : startDateCalendar; |
| 358 | + const lastWeekOfMonth = calendar[calendar.length - 1].filter(Boolean); |
| 359 | + const lastDayOfMonth = lastWeekOfMonth[lastWeekOfMonth.length - 1]; |
| 360 | +
|
| 361 | + const currentPeriod = { |
| 362 | + start: `${startDateYear}-${month >= 10 ? month : `0${month}`}-${initialDayOffMonth}`, |
| 363 | + end: `${startDateYear}-${month >= 10 ? month : `0${month}`}-${lastDayOfMonth}` |
| 364 | + }; |
| 365 | +
|
| 366 | + if (isMultipane) { |
| 367 | + month += 1; |
| 368 | + currentPeriod.end = `${endDateYear}-${month >= 10 ? month : `0${month}`}-${lastDayOfMonth}`; |
| 369 | + current = new Date(endDateYear, endDateMonth); |
| 370 | + } |
| 371 | +
|
| 372 | + onNavigationChange({ |
| 373 | + direction, |
| 374 | + type, |
| 375 | + currentPeriod, |
| 376 | + isPastPeriod: current < initial |
| 377 | + }); |
| 378 | + } |
| 379 | +
|
337 | 380 | /** |
338 | 381 | * Handles the "to previous month" action in the date picker. |
339 | 382 | */ |
|
344 | 387 | startDateMonth = 11; |
345 | 388 | startDateYear--; |
346 | 389 | } |
| 390 | +
|
| 391 | + onNavigation('previous', 'month'); |
347 | 392 | }; |
348 | 393 |
|
349 | 394 | /** |
350 | 395 | * Handles the "to previous year" action in the date picker. |
351 | 396 | */ |
352 | 397 | const toPrevYear = () => { |
353 | 398 | startDateYear--; |
| 399 | + onNavigation('previous', 'year'); |
354 | 400 | }; |
355 | 401 |
|
356 | 402 | /** |
|
363 | 409 | startDateMonth = 0; |
364 | 410 | startDateYear++; |
365 | 411 | } |
| 412 | + onNavigation('next', 'month'); |
366 | 413 | }; |
367 | 414 |
|
368 | 415 | /** |
369 | 416 | * Handles the "to next year" action in the date picker. |
370 | 417 | */ |
371 | 418 | const toNextYear = () => { |
372 | 419 | startDateYear++; |
| 420 | + onNavigation('next', 'year'); |
373 | 421 | }; |
374 | 422 |
|
375 | 423 | /** |
|
0 commit comments