From b9d9ba38600030e16ecdd4db1fc7a1cae6d493d1 Mon Sep 17 00:00:00 2001 From: Hunter Johnston <64506580+huntabyte@users.noreply.github.com> Date: Sat, 2 Mar 2024 18:48:47 -0500 Subject: [PATCH] fix: Range Calendar/Picker single day range selection (#1037) --- .changeset/curly-olives-trade.md | 5 +++++ src/lib/builders/date-range-field/create.ts | 3 ++- src/lib/builders/range-calendar/create.ts | 3 ++- 3 files changed, 9 insertions(+), 2 deletions(-) create mode 100644 .changeset/curly-olives-trade.md diff --git a/.changeset/curly-olives-trade.md b/.changeset/curly-olives-trade.md new file mode 100644 index 0000000000..47f7b425cc --- /dev/null +++ b/.changeset/curly-olives-trade.md @@ -0,0 +1,5 @@ +--- +'@melt-ui/svelte': patch +--- + +Range Calendar/Picker: fixed bug preventing a range with the same start and end date (Closes #898) diff --git a/src/lib/builders/date-range-field/create.ts b/src/lib/builders/date-range-field/create.ts index 981a459388..9997b79eca 100644 --- a/src/lib/builders/date-range-field/create.ts +++ b/src/lib/builders/date-range-field/create.ts @@ -6,6 +6,7 @@ import { getDefaultDate, getFirstSegment, isBefore, + isBeforeOrSame, } from '$lib/internal/helpers/date/index.js'; import { addMeltEventListener, @@ -154,7 +155,7 @@ export function createDateRangeField(props?: CreateDateRangeFieldProps) { return false; } - if (!isBefore($value?.start, $value?.end)) { + if (!isBeforeOrSame($value?.start, $value?.end)) { return true; } diff --git a/src/lib/builders/range-calendar/create.ts b/src/lib/builders/range-calendar/create.ts index 85dc803628..9ba8300919 100644 --- a/src/lib/builders/range-calendar/create.ts +++ b/src/lib/builders/range-calendar/create.ts @@ -784,8 +784,9 @@ export function createRangeCalendar( } } - if ($startValue && isSameDay($startValue, date) && !preventDeselect.get() && !$endValue) { + if ($startValue && $endValue && isSameDay($endValue, date) && !preventDeselect.get()) { startValue.set(undefined); + endValue.set(undefined); placeholder.set(date); announcer.announce('Selected date is now empty.', 'polite'); return;