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: Simplify the logic for available dates calculation #32073

Merged
merged 2 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 4 additions & 4 deletions src/components/NewDatePicker/CalendarPicker/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {addMonths, endOfMonth, format, getYear, isSameDay, parseISO, setDate, setYear, startOfDay, subMonths} from 'date-fns';
import {addMonths, endOfDay, endOfMonth, format, getYear, isSameDay, parseISO, setDate, setYear, startOfDay, startOfMonth, subMonths} from 'date-fns';
import Str from 'expensify-common/lib/str';
import PropTypes from 'prop-types';
import React from 'react';
Expand Down Expand Up @@ -127,8 +127,8 @@ class CalendarPicker extends React.PureComponent {
const currentMonthView = this.state.currentDateView.getMonth();
const currentYearView = this.state.currentDateView.getFullYear();
const calendarDaysMatrix = generateMonthMatrix(currentYearView, currentMonthView);
const hasAvailableDatesNextMonth = startOfDay(endOfMonth(new Date(this.props.maxDate))) > addMonths(new Date(this.state.currentDateView), 1);
const hasAvailableDatesPrevMonth = startOfDay(new Date(this.props.minDate)) < endOfMonth(subMonths(new Date(this.state.currentDateView), 1));
const hasAvailableDatesNextMonth = startOfDay(new Date(this.props.maxDate)) > endOfMonth(new Date(this.state.currentDateView));
const hasAvailableDatesPrevMonth = endOfDay(new Date(this.props.minDate)) < startOfMonth(new Date(this.state.currentDateView));

return (
<View>
Expand Down Expand Up @@ -219,7 +219,7 @@ class CalendarPicker extends React.PureComponent {
const isBeforeMinDate = currentDate < startOfDay(new Date(this.props.minDate));
const isAfterMaxDate = currentDate > startOfDay(new Date(this.props.maxDate));
const isDisabled = !day || isBeforeMinDate || isAfterMaxDate;
const isSelected = isSameDay(parseISO(this.props.value), new Date(currentYearView, currentMonthView, day));
const isSelected = !!day && isSameDay(parseISO(this.props.value), new Date(currentYearView, currentMonthView, day));
return (
<PressableWithoutFeedback
key={`${index}_day-${day}`}
Expand Down
15 changes: 14 additions & 1 deletion tests/unit/CalendarPickerTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,19 @@ describe('CalendarPicker', () => {
expect(getByTestId('next-month-arrow')).toBeDisabled();
});

test('should allow navigating to the month of the max date when it has less days than the selected date', () => {
const maxDate = new Date('2003-11-27'); // This month has 30 days
const value = '2003-10-31';
const {getByTestId} = render(
<CalendarPicker
maxDate={maxDate}
value={value}
/>,
);

expect(getByTestId('next-month-arrow')).not.toBeDisabled();
});

test('should open the calendar on a month from max date if it is earlier than current month', () => {
const onSelectedMock = jest.fn();
const maxDate = new Date('2011-03-01');
Expand Down Expand Up @@ -217,7 +230,7 @@ describe('CalendarPicker', () => {
expect(getByLabelText('16')).not.toBeDisabled();
});

test('should not allow to press max date', () => {
test('should allow to press max date', () => {
const value = '2003-02-17';
const maxDate = new Date('2003-02-24');
const {getByLabelText} = render(
Expand Down
Loading