Skip to content

Commit

Permalink
fix: use current time when navigate month
Browse files Browse the repository at this point in the history
  • Loading branch information
hsuanyi-chou committed Nov 28, 2024
1 parent 5020e0b commit 732dcb5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
24 changes: 19 additions & 5 deletions components/ui/datetime-picker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -675,15 +675,23 @@ const DateTimePicker = React.forwardRef<Partial<DateTimePickerRef>, DateTimePick
* instead of resetting to 00:00
*/
const handleSelect = (newDay: Date | undefined) => {
if (!newDay) return;
if (!newDay) {
return;
}
if (!defaultPopupValue) {
newDay.setHours(month?.getHours() ?? 0, month?.getMinutes() ?? 0, month?.getSeconds() ?? 0);
onChange?.(newDay);
setMonth(newDay);
return;
}
const diff = newDay.getTime() - defaultPopupValue.getTime();
const diffInDays = diff / (1000 * 60 * 60 * 24);
const newDateFull = add(defaultPopupValue, { days: Math.ceil(diffInDays) });
newDateFull.setHours(
month?.getHours() ?? 0,
month?.getMinutes() ?? 0,
month?.getSeconds() ?? 0,
);
onChange?.(newDateFull);
setMonth(newDateFull);
};
Expand All @@ -692,9 +700,6 @@ const DateTimePicker = React.forwardRef<Partial<DateTimePickerRef>, DateTimePick
if (!newDay) {
return;
}
// const diff = newDay.getTime() - defaultValue.getTime();
// const diffInDays = diff / (1000 * 60 * 60 * 24);
// const newDateFull = add(defaultValue, { days: Math.ceil(diffInDays) });
onChange?.(newDay);
setMonth(newDay);
setDisplayDate(newDay);
Expand Down Expand Up @@ -760,7 +765,16 @@ const DateTimePicker = React.forwardRef<Partial<DateTimePickerRef>, DateTimePick
mode="single"
selected={displayDate}
month={month}
onSelect={onSelect}
onSelect={(newDate) => {
if (newDate) {
newDate.setHours(
month?.getHours() ?? 0,
month?.getMinutes() ?? 0,
month?.getSeconds() ?? 0,
);
onSelect(newDate);
}
}}
onMonthChange={handleSelect}
yearRange={yearRange}
locale={locale}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "shadcn-ui-expensions",
"version": "1.0.0",
"version": "1.0.1",
"private": true,
"scripts": {
"dev": "next dev",
Expand Down

0 comments on commit 732dcb5

Please sign in to comment.