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 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
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 @@ export default class Month extends Component<MonthProps> {
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 @@ export default class Month extends Component<MonthProps> {
return isDisabled;
};

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

if (selectsMultiple) {
return selectedDates;
}

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
46 changes: 46 additions & 0 deletions src/test/month_test.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -924,6 +924,52 @@ describe("Month", () => {
it("should have no axe violations", () => runAxe(monthComponent));
});

describe("if 2 months are selected in a multi selection", () => {
let monthComponent: HTMLElement;

beforeEach(() => {
monthComponent = render(
<Month
day={newDate("2015-02-01")}
selectedDates={[newDate("2015-02-01"), newDate("2015-03-01")]}
selectsMultiple
showMonthYearPicker
/>,
).container;
});

it("should return 2 selected classes in the current year", () => {
expect(
monthComponent.querySelectorAll<HTMLElement>(
".react-datepicker__month-text--selected",
).length,
).toBe(2);
});
});

describe("if no months are selected in a multi selection", () => {
let monthComponent: HTMLElement;

beforeEach(() => {
monthComponent = render(
<Month
day={newDate("2015-02-01")}
selectedDates={[]}
selectsMultiple
showMonthYearPicker
/>,
).container;
});

it("should return 0 selected classes in the current year", () => {
expect(
monthComponent.querySelectorAll<HTMLElement>(
".react-datepicker__month-text--selected",
).length,
).toBe(0);
});
});

describe("if month is not selected", () => {
let month: HTMLElement;

Expand Down
Loading