@@ -3,7 +3,7 @@ import React, { useCallback, useContext, useEffect, useRef } from "react";
3
3
4
4
import { BORDER_COLOR , DATE_FORMAT , RING_COLOR } from "../constants" ;
5
5
import DatepickerContext from "../contexts/DatepickerContext" ;
6
- import { dateIsValid } from "../helpers" ;
6
+ import { dateIsValid , parseFormattedDate } from "../helpers" ;
7
7
8
8
import ToggleButton from "./ToggleButton" ;
9
9
@@ -53,7 +53,7 @@ const Input: React.FC<Props> = (e: Props) => {
53
53
54
54
const border = BORDER_COLOR . focus [ primaryColor as keyof typeof BORDER_COLOR . focus ] ;
55
55
const ring =
56
- RING_COLOR [ "second-focus" ] [ primaryColor as keyof ( typeof RING_COLOR ) [ "second-focus" ] ] ;
56
+ RING_COLOR [ "second-focus" ] [ primaryColor as keyof typeof RING_COLOR [ "second-focus" ] ] ;
57
57
58
58
const defaultInputClassName = `relative transition-all duration-300 py-2.5 pl-4 pr-14 w-full border-gray-300 dark:bg-slate-800 dark:text-white/80 dark:border-slate-600 rounded-lg tracking-wide font-light text-sm placeholder-gray-400 bg-white focus:ring disabled:opacity-40 disabled:cursor-not-allowed ${ border } ${ ring } ` ;
59
59
@@ -67,22 +67,24 @@ const Input: React.FC<Props> = (e: Props) => {
67
67
const handleInputChange = useCallback (
68
68
( e : React . ChangeEvent < HTMLInputElement > ) => {
69
69
const inputValue = e . target . value ;
70
- const start = `${ inputValue . slice ( 0 , 4 ) } -${ inputValue . slice ( 5 , 7 ) } -${ inputValue . slice (
71
- 8 ,
72
- 10
73
- ) } `;
74
- const end = `${ inputValue . slice ( 13 , 17 ) } -${ inputValue . slice ( 18 , 20 ) } -${ inputValue . slice (
75
- 21 ,
76
- inputValue . length
77
- ) } `;
70
+
71
+ const start = parseFormattedDate ( inputValue . slice ( 0 , 10 ) , displayFormat ) . format (
72
+ "YYYY-MM-DD"
73
+ ) ;
74
+ const end = asSingle
75
+ ? start
76
+ : parseFormattedDate ( inputValue . slice ( 11 , inputValue . length ) , displayFormat ) . format (
77
+ "YYYY-MM-DD"
78
+ ) ;
79
+
78
80
const input = inputRef ?. current ;
79
81
80
82
if (
81
83
start . length === 10 &&
82
84
end . length === 10 &&
83
85
dateIsValid ( new Date ( start ) ) &&
84
86
dateIsValid ( new Date ( end ) ) &&
85
- dayjs ( start ) . isBefore ( end )
87
+ ( dayjs ( start ) . isBefore ( end ) || asSingle )
86
88
) {
87
89
changeDatepickerValue (
88
90
{
@@ -91,15 +93,23 @@ const Input: React.FC<Props> = (e: Props) => {
91
93
} ,
92
94
e . target
93
95
) ;
94
- changeDayHover ( dayjs ( end ) . add ( - 1 , "day" ) . format ( DATE_FORMAT ) ) ;
96
+ if ( ! asSingle ) changeDayHover ( dayjs ( end ) . add ( - 1 , "day" ) . format ( DATE_FORMAT ) ) ;
97
+ else changeDayHover ( start ) ;
95
98
hideDatepicker ( ) ;
96
99
if ( input ) {
97
100
input . blur ( ) ;
98
101
}
99
102
}
100
103
changeInputText ( e . target . value ) ;
101
104
} ,
102
- [ changeDatepickerValue , changeDayHover , changeInputText , hideDatepicker ]
105
+ [
106
+ changeDatepickerValue ,
107
+ changeDayHover ,
108
+ changeInputText ,
109
+ hideDatepicker ,
110
+ displayFormat ,
111
+ asSingle
112
+ ]
103
113
) ;
104
114
105
115
const renderToggleIcon = useCallback (
0 commit comments