Skip to content

Commit

Permalink
fix(@clayui/date-picker): fixes error when not selecting date when th…
Browse files Browse the repository at this point in the history
…e time is invalid
  • Loading branch information
matuzalemsteles committed Apr 19, 2021
1 parent 9df5a14 commit 861de91
Showing 1 changed file with 14 additions and 22 deletions.
36 changes: 14 additions & 22 deletions packages/clay-date-picker/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -318,14 +318,7 @@ const ClayDatePicker: React.FunctionComponent<IProps> = React.forwardRef<
daysSelectedToString = formatDate(date, dateFormat);

if (time) {
daysSelectedToString = formatDate(
parseDate(
`${daysSelectedToString} ${currentTime}`,
`${dateFormat} ${TIME_FORMAT}`,
date
),
`${dateFormat} ${TIME_FORMAT}`
);
daysSelectedToString = `${daysSelectedToString} ${currentTime}`;
}
}

Expand Down Expand Up @@ -363,7 +356,10 @@ const ClayDatePicker: React.FunctionComponent<IProps> = React.forwardRef<
setDaysSelected([startDate, endDate]);

if (time) {
setCurrentTime(startDate.getHours(), startDate.getMinutes());
setCurrentTime(
startDate.getHours(),
startDate.getMinutes()
);
}
}

Expand Down Expand Up @@ -405,21 +401,17 @@ const ClayDatePicker: React.FunctionComponent<IProps> = 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);
};

Expand Down

0 comments on commit 861de91

Please sign in to comment.