@@ -3,7 +3,7 @@ import React, { useCallback, useContext, useEffect, useRef } from "react";
33
44import { BORDER_COLOR , DATE_FORMAT , RING_COLOR } from "../constants" ;
55import DatepickerContext from "../contexts/DatepickerContext" ;
6- import { dateIsValid } from "../helpers" ;
6+ import { dateIsValid , parseFormattedDate } from "../helpers" ;
77
88import ToggleButton from "./ToggleButton" ;
99
@@ -53,7 +53,7 @@ const Input: React.FC<Props> = (e: Props) => {
5353
5454 const border = BORDER_COLOR . focus [ primaryColor as keyof typeof BORDER_COLOR . focus ] ;
5555 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" ] ] ;
5757
5858 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 } ` ;
5959
@@ -67,22 +67,24 @@ const Input: React.FC<Props> = (e: Props) => {
6767 const handleInputChange = useCallback (
6868 ( e : React . ChangeEvent < HTMLInputElement > ) => {
6969 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+
7880 const input = inputRef ?. current ;
7981
8082 if (
8183 start . length === 10 &&
8284 end . length === 10 &&
8385 dateIsValid ( new Date ( start ) ) &&
8486 dateIsValid ( new Date ( end ) ) &&
85- dayjs ( start ) . isBefore ( end )
87+ ( dayjs ( start ) . isBefore ( end ) || asSingle )
8688 ) {
8789 changeDatepickerValue (
8890 {
@@ -91,15 +93,23 @@ const Input: React.FC<Props> = (e: Props) => {
9193 } ,
9294 e . target
9395 ) ;
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 ) ;
9598 hideDatepicker ( ) ;
9699 if ( input ) {
97100 input . blur ( ) ;
98101 }
99102 }
100103 changeInputText ( e . target . value ) ;
101104 } ,
102- [ changeDatepickerValue , changeDayHover , changeInputText , hideDatepicker ]
105+ [
106+ changeDatepickerValue ,
107+ changeDayHover ,
108+ changeInputText ,
109+ hideDatepicker ,
110+ displayFormat ,
111+ asSingle
112+ ]
103113 ) ;
104114
105115 const renderToggleIcon = useCallback (
0 commit comments