Skip to content

Commit 0e1429e

Browse files
committed
fix: datepicker types
1 parent 78a9a6f commit 0e1429e

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

packages/machines/date-picker/src/date-picker.machine.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,16 @@ export const machine = createMachine<DatePickerSchema>({
745745

746746
let announceText: string
747747
if (prop("selectionMode") === "range") {
748-
announceText = formatSelectedDate(value[0], value[1], locale, timeZone)
748+
const [startDate, endDate] = value
749+
if (startDate && endDate) {
750+
announceText = formatSelectedDate(startDate, endDate, locale, timeZone)
751+
} else if (startDate) {
752+
announceText = formatSelectedDate(startDate, null, locale, timeZone)
753+
} else if (endDate) {
754+
announceText = formatSelectedDate(endDate, null, locale, timeZone)
755+
} else {
756+
announceText = ""
757+
}
749758
} else {
750759
announceText = value
751760
.map((date) => formatSelectedDate(date, null, locale, timeZone))

packages/machines/date-picker/src/date-picker.utils.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ import { DateFormatter, type DateValue } from "@internationalized/date"
22
import { clampValue, match } from "@zag-js/utils"
33
import type { DateView, IntlTranslations } from "./date-picker.types"
44

5-
export function adjustStartAndEndDate(value: Array<DateValue | null | undefined>) {
5+
export function adjustStartAndEndDate(value: Array<DateValue | null | undefined>): DateValue[] {
66
const [startDate, endDate] = value
7-
if (!startDate || !endDate) return value
8-
return startDate.compare(endDate) <= 0 ? value : [endDate, startDate]
7+
let result: Array<DateValue | null | undefined>
8+
if (!startDate || !endDate) result = value
9+
else result = startDate.compare(endDate) <= 0 ? value : [endDate, startDate]
10+
return result as DateValue[]
911
}
1012

11-
export function isDateWithinRange(date: DateValue, value: (DateValue | null)[]) {
13+
export function isDateWithinRange(date: DateValue, value: Array<DateValue | null | undefined>) {
1214
const [startDate, endDate] = value
1315
if (!startDate || !endDate) return false
1416
return startDate.compare(date) <= 0 && endDate.compare(date) >= 0

0 commit comments

Comments
 (0)