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

Add multiple months visual selection #4944

Merged
merged 2 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions docs-site/src/components/Examples/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ import CalendarStartDay from "../../examples/calendarStartDay";
import ExternalForm from "../../examples/externalForm";
import CalendarIcon from "../../examples/calendarIcon";
import SelectsMultiple from "../../examples/selectsMultiple";
import SelectsMultipleMonths from "../../examples/selectsMultipleMonths";
import CalendarIconExternal from "../../examples/calendarIconExternal";
import CalendarIconSvgIcon from "../../examples/calendarIconSvgIcon";
import ToggleCalendarOnIconClick from "../../examples/toggleCalendarOnIconClick";
Expand Down Expand Up @@ -502,6 +503,10 @@ export default class exampleComponents extends React.Component {
title: "Select multiple dates",
component: SelectsMultiple,
},
{
title: "Select multiple months",
component: SelectsMultipleMonths,
},
{
title: "Strict parsing",
component: StrictParsing,
Expand Down
17 changes: 17 additions & 0 deletions docs-site/src/examples/selectsMultipleMonths.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
() => {
const [selectedDates, setSelectedDates] = useState([new Date()]);
const onChange = (dates) => {
setSelectedDates(dates);
};
return (
<DatePicker
selectedDates={selectedDates}
selectsMultiple
showMonthYearPicker
show
onChange={onChange}
shouldCloseOnSelect={false}
disabledKeyboardNavigation
/>
);
};
28 changes: 25 additions & 3 deletions src/month.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,11 @@
isSelectedMonth = (day: Date, m: number, selected: Date) =>
getMonth(selected) === m && getYear(day) === getYear(selected);

isSelectMonthInList = (day: Date, m: number, selectedDates: Date[]) =>
selectedDates.some((selectedDate) =>
this.isSelectedMonth(day, m, selectedDate),
);

isSelectedQuarter = (day: Date, q: number, selected: Date): boolean =>
getQuarter(day) === q && getYear(day) === getYear(selected);

Expand Down Expand Up @@ -779,20 +784,37 @@
return isDisabled;
};

getSelection() {
const { selected, selectedDates, selectsMultiple } = this.props;

if (selectsMultiple) {
return selectedDates;

Check warning on line 791 in src/month.tsx

View check run for this annotation

Codecov / codecov/patch

src/month.tsx#L791

Added line #L791 was not covered by tests
}

if (selected) {
return [selected];
}

return undefined;
}

getMonthClassNames = (m: number) => {
const { day, startDate, endDate, selected, preSelection, monthClassName } =
const { day, startDate, endDate, preSelection, monthClassName } =
this.props;
const _monthClassName = monthClassName
? monthClassName(setMonth(day, m))
: undefined;

const selection = this.getSelection();

return clsx(
"react-datepicker__month-text",
`react-datepicker__month-${m}`,
_monthClassName,
{
"react-datepicker__month-text--disabled": this.isMonthDisabled(m),
"react-datepicker__month-text--selected": selected
? this.isSelectedMonth(day, m, selected)
"react-datepicker__month-text--selected": selection
? this.isSelectMonthInList(day, m, selection)
: undefined,
"react-datepicker__month-text--keyboard-selected":
!this.props.disabledKeyboardNavigation &&
Expand Down
Loading