11import { DateFormatter } from "@internationalized/date"
2- import { createGuards , createMachine , type Params } from "@zag-js/core"
2+ import { createGuards , createMachine , type Params , type PropFn } from "@zag-js/core"
33import {
44 alignDate ,
55 constrainValue ,
@@ -48,6 +48,10 @@ function isDateArrayEqual(a: DateValue[], b: DateValue[] | undefined) {
4848 return true
4949}
5050
51+ function getValueAsString ( value : DateValue [ ] , prop : PropFn < DatePickerSchema > ) {
52+ return value . map ( ( date ) => prop ( "format" ) ( date , { locale : prop ( "locale" ) , timeZone : prop ( "timeZone" ) } ) )
53+ }
54+
5155export const machine = createMachine < DatePickerSchema > ( {
5256 props ( { props } ) {
5357 const locale = props . locale || "en-US"
@@ -125,9 +129,7 @@ export const machine = createMachine<DatePickerSchema>({
125129 const context = getContext ( )
126130 const view = context . get ( "view" )
127131 const value = context . get ( "value" )
128- const valueAsString = value . map ( ( date ) =>
129- prop ( "format" ) ( date , { locale : prop ( "locale" ) , timeZone : prop ( "timeZone" ) } ) ,
130- )
132+ const valueAsString = getValueAsString ( value , prop )
131133 prop ( "onFocusChange" ) ?.( { value, valueAsString, view, focusedValue } )
132134 } ,
133135 } ) ) ,
@@ -138,9 +140,7 @@ export const machine = createMachine<DatePickerSchema>({
138140 hash : ( v ) => v . map ( ( date ) => date . toString ( ) ) . join ( "," ) ,
139141 onChange ( value ) {
140142 const context = getContext ( )
141- const valueAsString = value . map ( ( date ) =>
142- prop ( "format" ) ( date , { locale : prop ( "locale" ) , timeZone : prop ( "timeZone" ) } ) ,
143- )
143+ const valueAsString = getValueAsString ( value , prop )
144144 prop ( "onValueChange" ) ?.( { value, valueAsString, view : context . get ( "view" ) } )
145145 } ,
146146 } ) ) ,
@@ -196,10 +196,7 @@ export const machine = createMachine<DatePickerSchema>({
196196 ! isPreviousRangeInvalid ( context . get ( "startValue" ) , prop ( "min" ) , prop ( "max" ) ) ,
197197 isNextVisibleRangeValid : ( { prop, computed } ) =>
198198 ! isNextRangeInvalid ( computed ( "endValue" ) , prop ( "min" ) , prop ( "max" ) ) ,
199- valueAsString ( { context, prop } ) {
200- const value = context . get ( "value" )
201- return value . map ( ( date ) => prop ( "format" ) ( date , { locale : prop ( "locale" ) , timeZone : prop ( "timeZone" ) } ) )
202- } ,
199+ valueAsString : ( { context, prop } ) => getValueAsString ( context . get ( "value" ) , prop ) ,
203200 } ,
204201
205202 effects : [ "setupLiveRegion" ] ,
@@ -1074,7 +1071,7 @@ export const machine = createMachine<DatePickerSchema>({
10741071 setFocusedValue ( params , date )
10751072 } ,
10761073
1077- selectParsedDate ( { context, event, computed , prop } ) {
1074+ selectParsedDate ( { context, event, prop } ) {
10781075 if ( event . index == null ) return
10791076
10801077 const parse = prop ( "parse" )
@@ -1089,13 +1086,18 @@ export const machine = createMachine<DatePickerSchema>({
10891086
10901087 if ( ! date ) return
10911088
1089+ // constrain date to min/max range
1090+ date = constrainValue ( date , prop ( "min" ) , prop ( "max" ) )
1091+
10921092 const values = Array . from ( context . get ( "value" ) )
10931093 values [ event . index ] = date
10941094
10951095 context . set ( "value" , values )
1096+
10961097 // always sync the input value, even if the selecteddate is not changed
10971098 // e.g. selected value is 02/28/2024, and the input value changed to 02/28
1098- context . set ( "inputValue" , computed ( "valueAsString" ) [ event . index ] )
1099+ const valueAsString = getValueAsString ( values , prop )
1100+ context . set ( "inputValue" , valueAsString [ event . index ] )
10991101 } ,
11001102
11011103 resetView ( { context } ) {
0 commit comments