Skip to content

Commit 3b3c977

Browse files
authored
Merge pull request #7 from marcossaore/feat/add-date-selected
feat: add onDateSelected to know if component changes period
2 parents e22a871 + ff48549 commit 3b3c977

File tree

4 files changed

+56
-1
lines changed

4 files changed

+56
-1
lines changed

docs/package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/src/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export const defaultConfig = {
1616
theme: '',
1717
disabledDates: [],
1818
onDayClick: () => { },
19+
onNavigationChange: () => { },
1920
showYearControls: true,
2021
showPresets: false,
2122
showTimePicker: false,

src/datepicker.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ export interface DatePickerProps {
8585
*/
8686
onDayClick?: (event: Object) => void;
8787

88+
/**
89+
* Callback function to handle the navigation click event for months and years
90+
* @type {(event: Object) => void}
91+
*/
92+
onNavigationChange?: (event: Object) => void;
93+
8894
/**
8995
* Indicates whether the date picker should always be shown.
9096
*/

src/datepicker.svelte

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@
9898
*/
9999
export let onDayClick = () => {};
100100
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+
101107
/**
102108
* Indicates whether the date picker should always be shown.
103109
* @type {boolean}
@@ -334,6 +340,43 @@
334340
endDateCalendar = endDateCalendar;
335341
};
336342
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+
337380
/**
338381
* Handles the "to previous month" action in the date picker.
339382
*/
@@ -344,13 +387,16 @@
344387
startDateMonth = 11;
345388
startDateYear--;
346389
}
390+
391+
onNavigation('previous', 'month');
347392
};
348393
349394
/**
350395
* Handles the "to previous year" action in the date picker.
351396
*/
352397
const toPrevYear = () => {
353398
startDateYear--;
399+
onNavigation('previous', 'year');
354400
};
355401
356402
/**
@@ -363,13 +409,15 @@
363409
startDateMonth = 0;
364410
startDateYear++;
365411
}
412+
onNavigation('next', 'month');
366413
};
367414
368415
/**
369416
* Handles the "to next year" action in the date picker.
370417
*/
371418
const toNextYear = () => {
372419
startDateYear++;
420+
onNavigation('next', 'year');
373421
};
374422
375423
/**

0 commit comments

Comments
 (0)