Skip to content

Commit e7e26d1

Browse files
Merge pull request #5645 from meriouma/dev/expose-outsideClickIgnoreClass
feat: expose outsideClickIgnoreClass
2 parents 3bfde22 + 3e3d80f commit e7e26d1

File tree

4 files changed

+43
-14
lines changed

4 files changed

+43
-14
lines changed

CLAUDE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Use yarn instead of npm for running commands

src/calendar.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ const DROPDOWN_FOCUS_CLASSNAMES = [
8080
"react-datepicker__month-year-select",
8181
];
8282

83+
export const OUTSIDE_CLICK_IGNORE_CLASS =
84+
"react-datepicker-ignore-onclickoutside";
85+
8386
const isDropdownSelect = (element: HTMLDivElement) => {
8487
const classNames = (element.className || "").split(/\s+/);
8588
return DROPDOWN_FOCUS_CLASSNAMES.some(
@@ -225,6 +228,7 @@ export default class Calendar extends Component<CalendarProps, CalendarState> {
225228
return {
226229
monthsShown: 1,
227230
forceShowMonthNavigation: false,
231+
outsideClickIgnoreClass: OUTSIDE_CLICK_IGNORE_CLASS,
228232
timeCaption: "Time",
229233
previousYearButtonLabel: "Previous Year",
230234
nextYearButtonLabel: "Next Year",

src/index.tsx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { clsx } from "clsx";
22
import React, { Component, cloneElement } from "react";
33

4-
import Calendar from "./calendar";
4+
import Calendar, { OUTSIDE_CLICK_IGNORE_CLASS } from "./calendar";
55
import CalendarIcon from "./calendar_icon";
66
import {
77
newDate,
@@ -61,8 +61,6 @@ export { default as CalendarContainer } from "./calendar_container";
6161

6262
export { registerLocale, setDefaultLocale, getDefaultLocale };
6363

64-
const outsideClickIgnoreClass = "react-datepicker-ignore-onclickoutside";
65-
6664
export { ReactDatePickerCustomHeaderProps } from "./calendar";
6765

6866
// Compares dates year+month combinations
@@ -112,7 +110,6 @@ export type DatePickerProps = OmitUnion<
112110
| "highlightDates"
113111
| "holidays"
114112
| "shouldFocusDayInline"
115-
| "outsideClickIgnoreClass"
116113
| "monthSelectedIn"
117114
| "onDropdownFocus"
118115
| "onTimeChange"
@@ -266,6 +263,7 @@ export default class DatePicker extends Component<
266263
dropdownMode: "scroll" as const,
267264
preventOpenOnFocus: false,
268265
monthsShown: 1,
266+
outsideClickIgnoreClass: OUTSIDE_CLICK_IGNORE_CLASS,
269267
readOnly: false,
270268
withPortal: false,
271269
selectsDisabledDaysInRange: false,
@@ -1245,7 +1243,7 @@ export default class DatePicker extends Component<
12451243
onSelect={this.handleSelect}
12461244
onClickOutside={this.handleCalendarClickOutside}
12471245
holidays={getHolidaysMap(this.modifyHolidays())}
1248-
outsideClickIgnoreClass={outsideClickIgnoreClass}
1246+
outsideClickIgnoreClass={this.props.outsideClickIgnoreClass}
12491247
onDropdownFocus={this.handleDropdownFocus}
12501248
onTimeChange={this.handleTimeChange}
12511249
className={this.props.calendarClassName}
@@ -1334,7 +1332,8 @@ export default class DatePicker extends Component<
13341332

13351333
renderDateInput = () => {
13361334
const className = clsx(this.props.className, {
1337-
[outsideClickIgnoreClass]: this.state.open,
1335+
[this.props.outsideClickIgnoreClass ||
1336+
DatePicker.defaultProps.outsideClickIgnoreClass]: this.state.open,
13381337
});
13391338

13401339
const customInput = this.props.customInput || <input type="text" />;

src/test/datepicker_test.test.tsx

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { render, act, waitFor, fireEvent } from "@testing-library/react";
22
import { userEvent } from "@testing-library/user-event";
33
import { enUS, enGB } from "date-fns/locale";
44
import React, { useState } from "react";
5+
import { OUTSIDE_CLICK_IGNORE_CLASS } from "../calendar";
56

67
import {
78
KeyType,
@@ -687,21 +688,45 @@ describe("DatePicker", () => {
687688
});
688689
});
689690

690-
it("should not apply the react-datepicker-ignore-onclickoutside class to the date input when closed", () => {
691+
it("should not apply the default outsideClickIgnoreClass class to the date input when closed", () => {
691692
const { container } = render(<DatePicker />);
692693
const input = safeQuerySelector(container, "input");
693-
expect(
694-
input?.classList.contains("react-datepicker-ignore-onclickoutside"),
695-
).toBeFalsy();
694+
expect(input?.classList.contains(OUTSIDE_CLICK_IGNORE_CLASS)).toBe(false);
696695
});
697696

698-
it("should apply the react-datepicker-ignore-onclickoutside class to date input when open", () => {
697+
it("should apply the default outsideClickIgnoreClass class to date input when open", () => {
699698
const { container } = render(<DatePicker />);
700699
const input = safeQuerySelector<HTMLInputElement>(container, "input");
701700
fireEvent.focus(input);
702-
expect(
703-
input?.classList.contains("react-datepicker-ignore-onclickoutside"),
704-
).toBeTruthy();
701+
expect(input?.classList.contains(OUTSIDE_CLICK_IGNORE_CLASS)).toBe(true);
702+
});
703+
704+
it("should apply the outsideClickIgnoreClass class to date input when open", () => {
705+
const outsideClickIgnoreClass = "ignore-onclickoutside";
706+
const { container } = render(
707+
<DatePicker outsideClickIgnoreClass={outsideClickIgnoreClass} />,
708+
);
709+
const input = safeQuerySelector<HTMLInputElement>(container, "input");
710+
fireEvent.focus(input);
711+
expect(input?.classList.contains(outsideClickIgnoreClass)).toBe(true);
712+
});
713+
714+
it("should apply the default outsideClickIgnoreClass when prop is undefined", () => {
715+
const { container } = render(
716+
<DatePicker outsideClickIgnoreClass={undefined} />,
717+
);
718+
const input = safeQuerySelector<HTMLInputElement>(container, "input");
719+
fireEvent.focus(input);
720+
expect(input?.classList.contains(OUTSIDE_CLICK_IGNORE_CLASS)).toBe(true);
721+
});
722+
723+
it("should apply the default outsideClickIgnoreClass when prop is falsy", () => {
724+
const { container } = render(
725+
<DatePicker outsideClickIgnoreClass={undefined} />,
726+
);
727+
const input = safeQuerySelector<HTMLInputElement>(container, "input");
728+
fireEvent.focus(input);
729+
expect(input?.classList.contains(OUTSIDE_CLICK_IGNORE_CLASS)).toBe(true);
705730
});
706731

707732
it("should toggle the open status of calendar on click of the icon when toggleCalendarOnIconClick is set to true", () => {

0 commit comments

Comments
 (0)