Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Allowing past dates is messing with dates selection at DayPickerRangeController component #2251

@eduAbreu

Description

@eduAbreu

react-dates version
[email protected]

Describe the bug
When allowing past dates and trying to select an older past date range, after I select the starting date and if I switch between the next months, for some reason days in the next months until the today date are being selected without my interaction.

Source code (including props configuration)
Steps to reproduce the behavior:

export const isOutsideRange = (day, cutOffDate) => {
	const isAfterToday = moment().isAfter(day, 'day');

	if (cutOffDate) {
		return !moment(day).isSameOrBefore(cutOffDate) || isAfterToday;
	} else {
		return isAfterToday;
	}
};

export const DateRange = ({
	isDateDisabled = isOutsideRange,
	startDate,
	endDate,
	onDatesChange,
	initialVisibleMonth,
	cutOffDate,
	allowPastDates,
	ResetIcon,
}) => {
	const [focusedInput, setFocusedInput] = useState(START_DATE);

	const onDatesChangeCallback = useCallback(
		({ startDate, endDate }) => {
			let start = startDate?.clone().utc(0).startOf('day');
			let end = endDate?.clone().utc(0).endOf('day');

			onDatesChange({
				startDate: start,
				endDate: end,
			});
		},
		[onDatesChange]
	);

	const onClear = useCallback(() => {
		onDatesChange({ startDate: null, endDate: null });
		setFocusedInput(START_DATE);
	}, [onDatesChange]);

	return (
		<>
			<PeriodHeader
				startDate={startDate?.format('L')}
				endDate={endDate?.format('L')}
				onClear={onClear}
				ResetIcon={ResetIcon}
			/>
			<Styled.Section className="date-range">
				<DayPickerRangeController
					startDate={startDate}
					endDate={endDate}
					onDatesChange={onDatesChangeCallback}
					focusedInput={focusedInput}
					onFocusChange={focusedInput => {
						setFocusedInput(!focusedInput ? START_DATE : focusedInput);
					}}
					numberOfMonths={1}
					isOutsideRange={day =>
						!allowPastDates &&
						isDateDisabled(
							day,
							cutOffDate ? moment(+cutOffDate).add(1, 'months') : null
						)
					}
					weekDayFormat={'dd'}
					minimumNights={0}
					navPrev={<Styled.NavPrevButton />}
					navNext={<Styled.NavNextButton data-test="date-selector-next-button" />}
					daySize={40}
					enableOutsideDays
					initialVisibleMonth={() => moment(initialVisibleMonth || startDate)}
					verticalBorderSpacing={4}
				/>
			</Styled.Section>
		</>
	);
};

Screenshots/Gifs
https://github.com/react-dates/react-dates/assets/3449691/d52ba6de-1dde-4c61-b074-5b3a20be1c02

Desktop (please complete the following information):
Tested in both Chrome and Firefox and both have this issue.

Did anyone had this issue before?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions