Skip to content

Commit 870743f

Browse files
committed
fix: date picker callback
1 parent 5257de1 commit 870743f

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

.changeset/public-suits-yell.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@zag-js/date-picker": patch
3+
---
4+
5+
Fix issue where `onValueChange` doesn't get called when value is cleared

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

+12-2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@ import {
3939

4040
const { and } = createGuards<DatePickerSchema>()
4141

42+
function isDateArrayEqual(a: DateValue[], b: DateValue[] | undefined) {
43+
if (a?.length !== b?.length) return false
44+
const len = Math.max(a.length, b.length)
45+
for (let i = 0; i < len; i++) {
46+
if (!isDateEqual(a[i], b[i])) return false
47+
}
48+
return true
49+
}
50+
4251
export const machine = createMachine<DatePickerSchema>({
4352
props({ props }) {
4453
const locale = props.locale || "en-US"
@@ -105,7 +114,7 @@ export const machine = createMachine<DatePickerSchema>({
105114
return {
106115
focusedValue: bindable<DateValue>(() => ({
107116
defaultValue: prop("focusedValue"),
108-
isEqual: (a, b) => b !== null && a !== null && isDateEqual(a, b),
117+
isEqual: isDateEqual,
109118
hash: (v) => v.toString(),
110119
sync: true,
111120
onChange(focusedValue) {
@@ -122,9 +131,10 @@ export const machine = createMachine<DatePickerSchema>({
122131
value: bindable(() => ({
123132
defaultValue: prop("defaultValue"),
124133
value: prop("value"),
125-
isEqual: (a, b) => b != null && a != null && a.every((date, index) => isDateEqual(date, b[index])),
134+
isEqual: isDateArrayEqual,
126135
hash: (v) => v.map((date) => date.toString()).join(","),
127136
onChange(value) {
137+
console.log("onChange", value)
128138
const context = getContext()
129139
const valueAsString = value.map((date) =>
130140
prop("format")(date, { locale: prop("locale"), timeZone: prop("timeZone") }),

0 commit comments

Comments
 (0)