@@ -9,19 +9,21 @@ export default function useDateInput({
99 validRange,
1010 inputMode,
1111 onChange,
12+ onValidationError,
1213} : {
1314 onChange : ( d : Date ) => void
1415 locale : undefined | string
1516 value : Date | undefined
1617 validRange : ValidRangeType | undefined
1718 inputMode : 'start' | 'end'
19+ onValidationError ?: ( ( error : string | null ) => void ) | undefined
1820} ) {
1921 const { isDisabled, isWithinValidRange, validStart, validEnd } =
2022 useRangeChecker ( validRange )
2123 const [ error , setError ] = React . useState < null | string > ( null )
2224 const formatter = useInputFormatter ( { locale } )
2325 const inputFormat = useInputFormat ( { formatter, locale } )
24- const formattedValue = value !== null ? formatter . format ( value ) : ''
26+ const formattedValue = value ? formatter . format ( value ) : ''
2527 const onChangeText = ( date : string ) => {
2628 const dayIndex = inputFormat . indexOf ( 'DD' )
2729 const monthIndex = inputFormat . indexOf ( 'MM' )
@@ -35,13 +37,13 @@ export default function useDateInput({
3537 const month = Number ( date . slice ( monthIndex , monthIndex + 2 ) )
3638
3739 if ( Number . isNaN ( day ) || Number . isNaN ( year ) || Number . isNaN ( month ) ) {
38- setError (
39- getTranslation (
40- locale ,
41- 'notAccordingToDateFormat' ,
42- ( ) => 'notAccordingToDateFormat'
43- ) ( inputFormat )
44- )
40+ const inputError = getTranslation (
41+ locale ,
42+ 'notAccordingToDateFormat' ,
43+ ( ) => 'notAccordingToDateFormat'
44+ ) ( inputFormat )
45+ setError ( inputError )
46+ onValidationError && onValidationError ( inputError )
4547 return
4648 }
4749
@@ -51,7 +53,9 @@ export default function useDateInput({
5153 : new Date ( year , month - 1 , day )
5254
5355 if ( isDisabled ( finalDate ) ) {
54- setError ( getTranslation ( locale , 'dateIsDisabled' ) )
56+ const inputError = getTranslation ( locale , 'dateIsDisabled' )
57+ setError ( inputError )
58+ onValidationError && onValidationError ( inputError )
5559 return
5660 }
5761 if ( ! isWithinValidRange ( finalDate ) ) {
@@ -80,11 +84,14 @@ export default function useDateInput({
8084 ) ( formatter . format ( validEnd ) )
8185 : '' ,
8286 ]
87+ const inputError = errors . filter ( ( n ) => n ) . join ( ' ' )
8388 setError ( errors . filter ( ( n ) => n ) . join ( ' ' ) )
89+ onValidationError && onValidationError ( inputError )
8490 return
8591 }
8692
8793 setError ( null )
94+ onValidationError && onValidationError ( null )
8895 if ( inputMode === 'end' ) {
8996 onChange ( finalDate )
9097 } else {
0 commit comments