Skip to content

Commit f09896e

Browse files
committed
refactor(events): add onDateChange event
1 parent f624a63 commit f09896e

File tree

6 files changed

+44
-4
lines changed

6 files changed

+44
-4
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ Try it in the [Svelte REPL](https://svelte.dev/repl/cae0ce6e92634878b6e1a587146d
8383
### Events
8484
| Prop | Description | Default |
8585
| :----------------- | :------------------------------------------------------------------------------------ | :-------------------------- |
86+
| onDateChange | Callback function to handle date change events. | `function`
8687
| onDayClick | Callback function to handle day click events. | `function`
8788
| onNavigationChange | Callback function to handle the navigation click event for months and years | `function`
8889

@@ -162,6 +163,7 @@ DatePicker CSS variables:
162163
--datepicker-container-font-family: var(--datepicker-font-family);
163164
--datepicker-container-left: 0;
164165
--datepicker-container-position: absolute;
166+
--datepicker-container-top: 105%;
165167
--datepicker-container-width: fit-content;
166168
--datepicker-container-zindex: 99;
167169

docs/src/App.svelte

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,12 @@
300300
<th>Description</th>
301301
<th>Default</th>
302302
</tr>
303+
<tr>
304+
<td>onDateChange</td>
305+
<td><code>function</code></td>
306+
<td>Callback function to handle when date changes.</td>
307+
<td><code>None</code></td>
308+
</tr>
303309
<tr>
304310
<td>onDayClick</td>
305311
<td><code>function</code></td>

docs/src/examples/range/range.svelte

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131
console.log(e, 'onNavigationChange');
3232
};
3333
34+
const onDateChange = (args) => {
35+
console.log(args, 'onDateChange');
36+
};
37+
3438
const toggleDatePicker = () => (isOpen = !isOpen);
3539
const formatDate = (dateString) => dateString && format(new Date(dateString), dateFormat) || '';
3640
@@ -44,6 +48,7 @@
4448
bind:startDate
4549
bind:endDate
4650
{onNavigationChange}
51+
{onDateChange}
4752
isRange
4853
{isMultipane}
4954
{showPresets}

docs/src/examples/single/single.svelte

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,14 @@
2929
console.log(e, 'onNavigationChange');
3030
};
3131
32+
const onDateChange = (args) => {
33+
console.log(args, 'onDateChange');
34+
};
35+
3236
$: formattedStartDate = formatDate(startDate);
3337
</script>
3438

35-
<DatePicker bind:isOpen bind:startDate {...$$props} {onNavigationChange}>
39+
<DatePicker bind:isOpen bind:startDate {...$$props} {onNavigationChange} {onDateChange}>
3640
<input type="text" bind:value={formattedStartDate} on:change={onChange} on:click={toggleDatePicker} />
3741
</DatePicker>
3842

docs/src/examples/theme/theme.svelte

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,8 @@
209209
--datepicker-presets-button-color: #fff;
210210
--datepicker-presets-button-color-active: #fff;
211211
--datepicker-presets-button-color-hover: #333;
212-
--datepicker-presets-button-color-focus: #333
212+
--datepicker-presets-button-color-focus: #333;
213+
--datepicker-spacing: 90px;
214+
213215
}
214216
</style>

src/datepicker.svelte

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,13 @@
9393
*/
9494
export let enabledDates = [];
9595
96+
/**
97+
* Callback function triggered when a date or date range changes.
98+
* @type {function}
99+
* @default () => {}
100+
*/
101+
export let onDateChange = () => {};
102+
96103
/**
97104
* Callback function to handle day click events.
98105
* @type {(event: Object) => void}
@@ -483,7 +490,6 @@
483490
*
484491
* @param {number} startDate - The timestamp of the start date.
485492
* @param {number} endDate - The timestamp of the end date.
486-
* @param {string[]} disabled - An array of disabled dates.
487493
* @returns {string[]} - An array of dates within the specified range.
488494
*/
489495
const getDatesInRange = (startDate, endDate) => {
@@ -495,10 +501,11 @@
495501
const formattedDate = `${
496502
dateRangeStart.getMonth() + 1
497503
}/${dateRangeStart.getDate()}/${dateRangeStart.getFullYear()}`;
504+
498505
if (
499506
(!enabled && !disabled) ||
500507
(enabled.length && enabled.includes(formattedDate)) ||
501-
(disabled.length && !disabled.includes(formattedDate))
508+
!disabled.includes(formattedDate)
502509
) {
503510
datesInRange.push(formattedDate);
504511
}
@@ -545,6 +552,10 @@
545552
};
546553
547554
onDayClick(event);
555+
556+
if ((isRange && startDate && endDate) || (!isRange && startDate)) {
557+
onDateChange(event);
558+
}
548559
};
549560
550561
/**
@@ -772,6 +783,16 @@
772783
startDate = start;
773784
endDate = end;
774785
786+
if (isRange && startDate && endDate) {
787+
onDateChange({
788+
startDate,
789+
startDateTime,
790+
endDate,
791+
endDateTime,
792+
rangeDates: getDatesInRange(startDate, endDate)
793+
});
794+
}
795+
775796
if (!alwaysShow) {
776797
isOpen = false;
777798
}

0 commit comments

Comments
 (0)