From 861de9112047099a9212ab7d0f08d58a2e522638 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matuzal=C3=A9m=20Teles?= Date: Mon, 19 Apr 2021 16:24:18 -0300 Subject: [PATCH] fix(@clayui/date-picker): fixes error when not selecting date when the time is invalid --- packages/clay-date-picker/src/index.tsx | 36 ++++++++++--------------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/packages/clay-date-picker/src/index.tsx b/packages/clay-date-picker/src/index.tsx index c04176cb4d..078b876722 100644 --- a/packages/clay-date-picker/src/index.tsx +++ b/packages/clay-date-picker/src/index.tsx @@ -318,14 +318,7 @@ const ClayDatePicker: React.FunctionComponent = React.forwardRef< daysSelectedToString = formatDate(date, dateFormat); if (time) { - daysSelectedToString = formatDate( - parseDate( - `${daysSelectedToString} ${currentTime}`, - `${dateFormat} ${TIME_FORMAT}`, - date - ), - `${dateFormat} ${TIME_FORMAT}` - ); + daysSelectedToString = `${daysSelectedToString} ${currentTime}`; } } @@ -363,7 +356,10 @@ const ClayDatePicker: React.FunctionComponent = React.forwardRef< setDaysSelected([startDate, endDate]); if (time) { - setCurrentTime(startDate.getHours(), startDate.getMinutes()); + setCurrentTime( + startDate.getHours(), + startDate.getMinutes() + ); } } @@ -405,21 +401,17 @@ const ClayDatePicker: React.FunctionComponent = React.forwardRef< hours: number | string, minutes: number | string ) => { - const format = `${dateFormat} ${TIME_FORMAT}`; const [day] = daysSelected; - const dateParsed = parseDate(value, format, NEW_DATE); - const newDate = isValid(dateParsed) - ? dateParsed - : new Date(day.getTime()); - - newDate.setHours(Number(hours)); - newDate.setMinutes(Number(minutes)); - - if (isValid(dateParsed)) { - onValueChange(formatDate(newDate, format), 'time'); - } - + onValueChange( + typeof hours === 'string' && typeof minutes === 'string' + ? `${formatDate(day, dateFormat)} ${hours}:${minutes}` + : formatDate( + setDate(day, {hours, minutes}), + `${dateFormat} ${TIME_FORMAT}` + ), + 'time' + ); setCurrentTime(hours, minutes); };