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

(feature) swapRange optional prop #4654

Merged
merged 3 commits into from
Apr 3, 2024
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
7 changes: 7 additions & 0 deletions docs-site/src/components/Examples/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ import SelectsMultiple from "../../examples/selectsMultiple";
import CalendarIconExternal from "../../examples/calendarIconExternal";
import CalendarIconSvgIcon from "../../examples/calendarIconSvgIcon";
import ToggleCalendarOnIconClick from "../../examples/toggleCalendarOnIconClick";
import SwapRange from "../../examples/rangeSwapRange";

import "./style.scss";
import "react-datepicker/dist/react-datepicker.css";
Expand Down Expand Up @@ -461,6 +462,12 @@ export default class exampleComponents extends React.Component {
title: "Range Quarter Picker for one quarter picker",
component: RangeQuarterPickerSelectsRange,
},
{
title: "Range Swap Range",
description:
"Swap the start and end date if the end date is before the start date in a pick sequence.",
component: SwapRange,
},
{
title: "Read only datepicker",
component: ReadOnly,
Expand Down
22 changes: 22 additions & 0 deletions docs-site/src/examples/rangeSwapRange.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
() => {
const [startDate, setStartDate] = useState(new Date());
const [endDate, setEndDate] = useState(null);
const onChange = (dates) => {
const [start, end] = dates;
setStartDate(start);
setEndDate(end);
};
return (
<DatePicker
swapRange
selected={startDate}
onChange={onChange}
startDate={startDate}
endDate={endDate}
excludeDates={[addDays(new Date(), 1), addDays(new Date(), 5)]}
selectsRange
selectsDisabledDaysInRange
inline
/>
);
};
9 changes: 8 additions & 1 deletion src/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ export default class DatePicker extends React.Component {
showQuarterYearPicker: false,
showWeekPicker: false,
strictParsing: false,
swapRange: false,
timeIntervals: 30,
timeCaption: "Time",
previousMonthAriaLabel: "Previous Month",
Expand Down Expand Up @@ -253,6 +254,7 @@ export default class DatePicker extends React.Component {
showWeekNumbers: PropTypes.bool,
showYearDropdown: PropTypes.bool,
strictParsing: PropTypes.bool,
swapRange: PropTypes.bool,
forceShowMonthNavigation: PropTypes.bool,
showDisabledMonthNavigation: PropTypes.bool,
startDate: PropTypes.instanceOf(Date),
Expand Down Expand Up @@ -634,6 +636,7 @@ export default class DatePicker extends React.Component {
selectsMultiple,
selectedDates,
minTime,
swapRange,
} = this.props;

if (
Expand Down Expand Up @@ -690,7 +693,11 @@ export default class DatePicker extends React.Component {
if (changedDate === null) {
onChange([null, null], event);
} else if (isDateBefore(changedDate, startDate)) {
onChange([changedDate, null], event);
if (swapRange) {
onChange([changedDate, startDate], event);
} else {
onChange([changedDate, null], event);
}
} else {
onChange([startDate, changedDate], event);
}
Expand Down
36 changes: 36 additions & 0 deletions test/datepicker_test.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2217,6 +2217,42 @@ describe("DatePicker", () => {
);
expect(endDate).toBeNull();
});

it("should swap dates of range when endDate set before startDate", () => {
const selected = utils.newDate("2024-04-03");
const selectedPrevious = utils.subDays(selected, 3);
let [startDate, endDate] = [selected, null];
const onChange = (dates = []) => {
[startDate, endDate] = dates;
};
const { container } = render(
<DatePicker
swapRange
selected={startDate}
onChange={onChange}
startDate={startDate}
endDate={endDate}
selectsRange
inline
/>,
);

let selectedDay = findSelectedDay(container, selectedPrevious);
// Ensure that we're dealing with a date at the beginning of the month
if (!selectedDay) {
// If it's the beginning of the month & if the selectedPrevious is not being displayed, navigate to the previous month and reselect the selectedPrevious
goToLastMonth(container);
selectedDay = findSelectedDay(container, selectedPrevious);
}

fireEvent.click(selectedDay);
expect(utils.formatDate(startDate, "yyyy-MM-dd")).toBe(
utils.formatDate(selectedPrevious, "yyyy-MM-dd"),
);
expect(utils.formatDate(endDate, "yyyy-MM-dd")).toBe(
utils.formatDate(selected, "yyyy-MM-dd"),
);
});
});

describe("selectsRange without inline", () => {
Expand Down
Loading