Skip to content

Commit b2a10ec

Browse files
feedback on when to set and reset start and end dates
1 parent 6424d82 commit b2a10ec

File tree

1 file changed

+28
-9
lines changed

1 file changed

+28
-9
lines changed

src/components/DatePicker/DateRangePicker.tsx

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -208,19 +208,16 @@ const Calendar = ({
208208
const isCurrentDate = isSameDate(today, fullDate);
209209
const isDisabled = futureDatesDisabled ? fullDate > today : false;
210210
const isBetweenStartAndEndDates = Boolean(
211-
startDate &&
212-
endDate &&
213-
((fullDate > startDate && fullDate < endDate) ||
214-
(fullDate < startDate && fullDate > endDate))
211+
startDate && endDate && fullDate > startDate && fullDate < endDate
215212
);
216213

217214
const shouldShowRangeIndicator =
218215
!endDate &&
219216
Boolean(
220217
startDate &&
221218
hoveredDate &&
222-
((fullDate > startDate && fullDate < hoveredDate) ||
223-
(fullDate < startDate && fullDate > hoveredDate))
219+
fullDate > startDate &&
220+
fullDate < hoveredDate
224221
);
225222

226223
const handleMouseEnter = () => {
@@ -275,7 +272,7 @@ export const DateRangePicker = ({
275272
disabled = false,
276273
futureDatesDisabled = false,
277274
onSelectDate,
278-
placeholder,
275+
placeholder = "start date - end date",
279276
}: DatePickerProps) => {
280277
const [isOpen, setIsOpen] = useState<boolean>(false);
281278
const [selectedDate, setSelectedDate] = useState<Date>();
@@ -295,15 +292,37 @@ export const DateRangePicker = ({
295292
};
296293

297294
const handleSelectDate = (selectedDate: Date): void => {
295+
// Start date and end date are selected, user clicks end date.
296+
// Reset the end date.
297+
if (endDate && isSameDate(endDate, selectedDate)) {
298+
setEndDate(undefined);
299+
return;
300+
}
301+
298302
if (startDate) {
299303
if (isSameDate(startDate, selectedDate)) {
300-
console.log("same date!");
304+
// Start date and end date are selected, user clicks start date.
305+
// Set the start date to the old end date, reset end date.
306+
if (endDate) {
307+
setStartDate(endDate);
308+
setEndDate(undefined);
309+
return;
310+
}
311+
312+
// Start date is selected, user clicks start date.
313+
// Reset the start date.
314+
setStartDate(undefined);
315+
return;
301316
}
302317

318+
// Start date is selected, user clicks an earlier date.
319+
// Set the earlier date to the new start date.
303320
if (selectedDate < startDate) {
304-
setEndDate(startDate);
321+
setStartDate(selectedDate);
322+
return;
305323
}
306324

325+
// Otherwise, set the end date to the date the user clicked.
307326
setEndDate(selectedDate);
308327
setSelectedDate(selectedDate);
309328
onSelectDate(selectedDate);

0 commit comments

Comments
 (0)