Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(blade): focus on selected date by default in datepicker #2567

Merged
merged 6 commits into from
Mar 12, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/wet-beds-sparkle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@razorpay/blade': patch
---

fix(blade): focus on selected date by default in datepicker
19 changes: 16 additions & 3 deletions packages/blade/src/components/DatePicker/Calendar.web.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/* eslint-disable @typescript-eslint/explicit-function-return-type */
import dayjs from 'dayjs';
import React from 'react';
import type { CalendarLevel } from '@mantine/dates';
import type { CalendarLevel, DatesRangeValue } from '@mantine/dates';
import { useDatesContext, DatePicker } from '@mantine/dates';
import type { CalendarProps, DateSelectionType, PickerType, DateValue } from './types';
import { CalendarHeader } from './CalendarHeader';
Expand All @@ -29,12 +29,14 @@ const Calendar = <Type extends DateSelectionType>({
onPrevious,
presets,
showLevelChangeLink,
oldValue,
...props
}: CalendarProps<Type> & {
date?: Date;
defaultDate?: Date;
onDateChange?: (date: DateValue) => void;
showLevelChangeLink?: boolean;
oldValue: DatesRangeValue | null;
}): React.ReactElement => {
const isRange = selectionType === 'range';

Expand Down Expand Up @@ -64,7 +66,18 @@ const Calendar = <Type extends DateSelectionType>({

const dateContext = useDatesContext();
const isMobile = useIsMobile();
const currentDate = _date ?? shiftTimezone('add', new Date());
const currentDate = React.useMemo(() => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain whats happening here? the date prop as is should work?

Copy link
Contributor Author

@tewarig tewarig Mar 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are already using dateProp. Currently, _date is passed to Date.
Whenever the user clicks on the next month/year, we update _date, and the date picker focuses on _date.

I modified this logic slightly:

  1. If _date exists, focus on it.
  2. Otherwise, focus on oldValue.
  3. If oldValue is also unavailable, focus on currentDate.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is oldValue?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the last applied value in the date picker.
Whenever you click/select any value on the date picker, its value changes. But if you click on Cancel, we revert back to the older value using oldValue. When you click on Apply, we save the applied value in oldValue to retrieve it next time.

if (_date) {
return _date;
}
if (Array.isArray(oldValue) && oldValue[1]) {
return oldValue[1];
}
if (!Array.isArray(oldValue) && oldValue) {
return oldValue;
}
return shiftTimezone('add', new Date());
}, [_date, oldValue]);
const numberOfColumns = isMobile || !isRange ? 1 : 2;
const columnsToScroll = numberOfColumns;

Expand Down Expand Up @@ -133,7 +146,7 @@ const Calendar = <Type extends DateSelectionType>({
<DatePicker
withCellSpacing={false}
type={isRange ? 'range' : 'default'}
date={_date}
date={currentDate}
locale={dateContext.locale}
level={level}
onDateChange={setDate}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ const DatePicker = <Type extends DateSelectionType = 'single'>({
setPicker(() => picker);
forceRerender();
}}
oldValue={oldValue}
/>
{isMobile ? null : (
<CalendarFooter
Expand Down
Loading