Skip to content

Commit b1150b1

Browse files
committed
feat: expose outsideClickIgnoreClass
1 parent b9c85a2 commit b1150b1

File tree

3 files changed

+24
-14
lines changed

3 files changed

+24
-14
lines changed

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(
@@ -221,6 +224,7 @@ export default class Calendar extends Component<CalendarProps, CalendarState> {
221224
return {
222225
monthsShown: 1,
223226
forceShowMonthNavigation: false,
227+
outsideClickIgnoreClass: OUTSIDE_CLICK_IGNORE_CLASS,
224228
timeCaption: "Time",
225229
previousYearButtonLabel: "Previous Year",
226230
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,
@@ -1236,7 +1234,7 @@ export default class DatePicker extends Component<
12361234
onSelect={this.handleSelect}
12371235
onClickOutside={this.handleCalendarClickOutside}
12381236
holidays={getHolidaysMap(this.modifyHolidays())}
1239-
outsideClickIgnoreClass={outsideClickIgnoreClass}
1237+
outsideClickIgnoreClass={this.props.outsideClickIgnoreClass}
12401238
onDropdownFocus={this.handleDropdownFocus}
12411239
onTimeChange={this.handleTimeChange}
12421240
className={this.props.calendarClassName}
@@ -1325,7 +1323,8 @@ export default class DatePicker extends Component<
13251323

13261324
renderDateInput = () => {
13271325
const className = clsx(this.props.className, {
1328-
[outsideClickIgnoreClass]: this.state.open,
1326+
[this.props.outsideClickIgnoreClass ||
1327+
DatePicker.defaultProps.outsideClickIgnoreClass]: this.state.open,
13291328
});
13301329

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

src/test/datepicker_test.test.tsx

Lines changed: 15 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,27 @@ 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);
705712
});
706713

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

0 commit comments

Comments
 (0)