From 153a9c94f49014730c5dc1617139c7dc1caa4235 Mon Sep 17 00:00:00 2001 From: Abdul-Kadir Coskun <59434446+Chizaruu@users.noreply.github.com> Date: Sun, 27 Oct 2024 21:22:44 +1100 Subject: [PATCH] fix: make isSelected reactive in datepicker (#1466) --- src/lib/datepicker/Datepicker.svelte | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/lib/datepicker/Datepicker.svelte b/src/lib/datepicker/Datepicker.svelte index 503ea4400..c4d5edb7f 100644 --- a/src/lib/datepicker/Datepicker.svelte +++ b/src/lib/datepicker/Datepicker.svelte @@ -161,12 +161,17 @@ return date.toLocaleDateString(locale, dateFormat); } - function isSelected(day: Date): boolean { + function isSameDate(date1: Date | null, date2: Date | null): boolean { + if (!date1 || !date2) return false; + return date1.toDateString() === date2.toDateString(); + } + + $: isSelected = (day: Date): boolean => { if (range) { - return !!(rangeFrom && day.toDateString() === rangeFrom.toDateString()) || !!(rangeTo && day.toDateString() === rangeTo.toDateString()); + return isSameDate(day, rangeFrom) || isSameDate(day, rangeTo); } - return !!(value && day.toDateString() === value.toDateString()); - } + return isSameDate(day, value); + }; function isInRange(day: Date): boolean { if (!range || !rangeFrom || !rangeTo) return false;