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

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

Open
eduAbreu opened this issue Jan 26, 2024 · 5 comments
Labels

Comments

@eduAbreu
Copy link

eduAbreu commented Jan 26, 2024

react-dates version
react-dates@21.8.0

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?

@2567910

This comment was marked as outdated.

@ljharb

This comment was marked as resolved.

@eduAbreu
Copy link
Author

@ljharb regarding the issue above is this a known issue?

@eduAbreu
Copy link
Author

@2567910 do you have this same issue?

@ljharb
Copy link
Member

ljharb commented Jan 30, 2024

I don’t believe so, unless there’s an open issue describing it already.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants