@@ -74,25 +74,22 @@ const changeDateToMomentFormat = (format) => {
7474 return "L" ;
7575 }
7676} ;
77-
78- //function to get default date
79- const getDefaultdate = ( selectedDate , format = "dd-MM-yyyy" ) => {
77+ //function to get default format
78+ const getDefaultFormat = ( dateFormat ) => dateFormat || "MM/dd/yyyy" ;
79+ //function to convert formated date to new Date() format
80+ const getDefaultDate = ( dateStr , format ) => {
81+ //get valid date format for moment to convert formated date to new Date() format
82+ const formats = changeDateToMomentFormat ( format ) ;
83+ const parsedDate = moment ( dateStr , formats ) ;
8084 let date ;
81- if ( format && format === "dd-MM-yyyy" ) {
82- const newdate = selectedDate
83- ? selectedDate
84- : moment ( new Date ( ) ) . format ( changeDateToMomentFormat ( format ) ) ;
85- const [ day , month , year ] = newdate . split ( "-" ) ;
86- date = new Date ( `${ year } -${ month } -${ day } ` ) ;
85+ if ( parsedDate . isValid ( ) ) {
86+ date = new Date ( parsedDate . toISOString ( ) ) ;
87+ return date ;
8788 } else {
88- date = new Date ( selectedDate ) ;
89+ date = new Date ( ) ;
90+ return date ;
8991 }
90- const value = date ;
91- return value ;
9292} ;
93- //function to get default format
94- const getDefaultFormat = ( dateFormat ) => dateFormat || "MM/dd/yyyy" ;
95-
9693function Placeholder ( props ) {
9794 //'isTouchDevice' is used to detect whether a device has a touchscreen or is mouse-based
9895 const isTouchDevice = navigator . maxTouchPoints > 0 ;
@@ -104,28 +101,16 @@ function Placeholder(props) {
104101 const holdTimeout = useRef ( null ) ;
105102 const startTime = useRef ( null ) ; // Track when the user starts holdings
106103 const [ isDisableDragging , setIsDisableDragging ] = useState ( true ) ;
107- const [ selectDate , setSelectDate ] = useState ( {
108- date :
109- props . pos . type === "date"
110- ? moment (
111- getDefaultdate (
112- props ?. pos ?. options ?. response ,
113- props . pos ?. options ?. validation ?. format
114- ) . getTime ( )
115- ) . format (
116- changeDateToMomentFormat ( props . pos ?. options ?. validation ?. format )
117- )
118- : "" ,
119- format :
120- props . pos . type === "date"
121- ? getDefaultFormat ( props . pos ?. options ?. validation ?. format )
122- : ""
123- } ) ;
104+ const [ selectDate , setSelectDate ] = useState ( { } ) ;
124105 const [ dateFormat , setDateFormat ] = useState ( [ ] ) ;
125106 const [ clickonWidget , setClickonWidget ] = useState ( { } ) ;
126107 const [ startDate , setStartDate ] = useState (
127- props . pos . type === "date" &&
128- getDefaultdate ( new Date ( ) , props . pos ?. options ?. validation ?. format )
108+ props ?. pos ?. options ?. response
109+ ? getDefaultDate (
110+ props ?. pos ?. options ?. response ,
111+ props . pos ?. options ?. validation ?. format
112+ )
113+ : new Date ( )
129114 ) ;
130115 const [ getCheckboxRenderWidth , setGetCheckboxRenderWidth ] = useState ( {
131116 width : null ,
@@ -164,7 +149,6 @@ function Placeholder(props) {
164149
165150 return ( ) => clearTimeout ( timer ) ;
166151 } , [ props . pos ] ) ;
167-
168152 useEffect ( ( ) => {
169153 const onOutsideClick = ( ) => {
170154 if ( ! isDraggingEnabled ) {
@@ -476,7 +460,7 @@ function Placeholder(props) {
476460 const isDateChange = true ;
477461 const dateObj = {
478462 date : startDate ,
479- format : selectDate . format
463+ format : getDefaultFormat ( props . pos ?. options ?. validation ?. format )
480464 } ;
481465 handleSaveDate ( dateObj , isDateChange ) ; //function to save date and format in local array
482466 }
@@ -485,19 +469,18 @@ function Placeholder(props) {
485469 //function to save date and format on local array onchange date and onclick format
486470 const handleSaveDate = ( data , isDateChange ) => {
487471 let updateDate = data . date ;
488- //check if date change by user
489- if ( isDateChange ) {
490- //`changeDateToMomentFormat` is used to convert date as per required to moment package
491- updateDate = moment ( data . date ) . format (
492- changeDateToMomentFormat ( data . format )
472+ let date ;
473+ if ( data ?. format === "dd-MM-yyyy" ) {
474+ date = isDateChange
475+ ? moment ( updateDate ) . format ( changeDateToMomentFormat ( data . format ) )
476+ : updateDate ;
477+ } else {
478+ //using moment package is used to change date as per the format provided in selectDate obj e.g. - MM/dd/yyyy -> 03/12/2024
479+ const newDate = new Date ( updateDate ) ;
480+ date = moment ( newDate . getTime ( ) ) . format (
481+ changeDateToMomentFormat ( data ?. format )
493482 ) ;
494483 }
495- //using moment package is used to change date as per the format provided in selectDate obj e.g. - MM/dd/yyyy -> 03/12/2024
496- //`getDefaultdate` is used to convert update date in new Date() format
497- const date = moment (
498- getDefaultdate ( updateDate , data ?. format ) . getTime ( )
499- ) . format ( changeDateToMomentFormat ( data ?. format ) ) ;
500-
501484 //`onChangeInput` is used to save data related to date in a placeholder field
502485 onChangeInput (
503486 date ,
0 commit comments