Repository: melt-ui/melt-ui
Pull request #1026 (2024-02-29 07:06:38)

fix: use calendar of reference value when parsing date
Closes: #985
This `PR` fixes the issue in `parseStringToDateValue` function which parses date strings to `DateValue` but currently ignores the `calendar` used in original value.


github-actions[bot]: <!-- refined-cf-pages-action:deployment-summary:melt-ui -->

###### built with [Refined Cloudflare Pages Action](https://github.com/AdrianGonz97/refined-cf-pages-action)
## ⚡ Cloudflare Pages Deployment
| Name | Status | Preview | Last Commit |
| :--- | :----- | :------ | :---------- |
| **melt-ui** | 🔨 Building ([View Log](https://github.com/melt-ui/melt-ui/actions/runs/8106656606)) |  | d6364f8b1e3d04ef401ee7eb908feed47135033c |

B-Esmaili: > Thank you!

One more thing worth mention is i found another bug in `@internationalized\date` package and submitted another `PR` there as well. I just wanted to let you know in case you encounter wrong rendering of days it is because of that bug and not related to this library.

Patch:
diff --git a/.changeset/shaggy-ligers-attack.md b/.changeset/shaggy-ligers-attack.md
new file mode 100644
index 00000000..62053b49
--- /dev/null
+++ b/.changeset/shaggy-ligers-attack.md
@@ -0,0 +1,5 @@
+---
+"@melt-ui/svelte": patch
+---
+
+Calendars: ensure dates use the same calendar as the reference value
diff --git a/src/lib/internal/helpers/date/utils.ts b/src/lib/internal/helpers/date/utils.ts
index fa1aa252..2adbcbc7 100644
--- a/src/lib/internal/helpers/date/utils.ts
+++ b/src/lib/internal/helpers/date/utils.ts
@@ -9,6 +9,7 @@ import {
 	parseDate,
 	getLocalTimeZone,
 	getDayOfWeek,
+	toCalendar,
 } from '@internationalized/date';
 
 type GetDefaultDateProps = {
@@ -68,13 +69,19 @@ export function getDefaultDate(props?: GetDefaultDateProps): DateValue {
  * strings, to the same type being used by the date component.
  */
 export function parseStringToDateValue(dateStr: string, referenceVal: DateValue): DateValue {
+	let dateValue: DateValue;
 	if (referenceVal instanceof ZonedDateTime) {
-		return parseZonedDateTime(dateStr);
+		dateValue = parseZonedDateTime(dateStr);
 	} else if (referenceVal instanceof CalendarDateTime) {
-		return parseDateTime(dateStr);
+		dateValue = parseDateTime(dateStr);
 	} else {
-		return parseDate(dateStr);
+		dateValue = parseDate(dateStr);
 	}
+
+	// ensure the parsed date is in the same calendar as the reference date set by the user.
+	return dateValue.calendar !== referenceVal.calendar
+		? toCalendar(dateValue, referenceVal.calendar)
+		: dateValue;
 }
 
 /**


Test patch:
diff --git a/src/lib/internal/helpers/date/tests/utils.spec.ts b/src/lib/internal/helpers/date/tests/utils.spec.ts
index c13d57cb..27ba21c9 100644
--- a/src/lib/internal/helpers/date/tests/utils.spec.ts
+++ b/src/lib/internal/helpers/date/tests/utils.spec.ts
@@ -4,6 +4,8 @@ import {
 	CalendarDateTime,
 	ZonedDateTime,
 	getLocalTimeZone,
+	HebrewCalendar,
+	toCalendar,
 	now,
 	toZoned,
 } from '@internationalized/date';
@@ -76,5 +78,16 @@ describe('Date Utils', () => {
 			const zdtStr = zdt.toString();
 			expect(parseStringToDateValue(zdtStr, zdt)).toEqual(zdt);
 		});
+		it('should return a date in the same calendar as the reference date', () => {
+			const gregorianDate = new CalendarDate(2020, 9, 19);
+			const hebrewDate = toCalendar(gregorianDate, new HebrewCalendar());
+
+			const gregorianDateStr = gregorianDate.toString();
+
+			const parsedDate = parseStringToDateValue(gregorianDateStr, hebrewDate);
+
+			expect(parsedDate).toEqual(hebrewDate);
+			expect(parsedDate).not.toEqual(gregorianDate);
+		});
 	});
 });