Skip to content

feat: expose outsideClickIgnoreClass #5645

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Use yarn instead of npm for running commands
4 changes: 4 additions & 0 deletions src/calendar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ const DROPDOWN_FOCUS_CLASSNAMES = [
"react-datepicker__month-year-select",
];

export const OUTSIDE_CLICK_IGNORE_CLASS =
"react-datepicker-ignore-onclickoutside";

const isDropdownSelect = (element: HTMLDivElement) => {
const classNames = (element.className || "").split(/\s+/);
return DROPDOWN_FOCUS_CLASSNAMES.some(
Expand Down Expand Up @@ -221,6 +224,7 @@ export default class Calendar extends Component<CalendarProps, CalendarState> {
return {
monthsShown: 1,
forceShowMonthNavigation: false,
outsideClickIgnoreClass: OUTSIDE_CLICK_IGNORE_CLASS,
timeCaption: "Time",
previousYearButtonLabel: "Previous Year",
nextYearButtonLabel: "Next Year",
Expand Down
11 changes: 5 additions & 6 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { clsx } from "clsx";
import React, { Component, cloneElement } from "react";

import Calendar from "./calendar";
import Calendar, { OUTSIDE_CLICK_IGNORE_CLASS } from "./calendar";
import CalendarIcon from "./calendar_icon";
import {
newDate,
Expand Down Expand Up @@ -61,8 +61,6 @@

export { registerLocale, setDefaultLocale, getDefaultLocale };

const outsideClickIgnoreClass = "react-datepicker-ignore-onclickoutside";

export { ReactDatePickerCustomHeaderProps } from "./calendar";

// Compares dates year+month combinations
Expand Down Expand Up @@ -112,7 +110,6 @@
| "highlightDates"
| "holidays"
| "shouldFocusDayInline"
| "outsideClickIgnoreClass"
| "monthSelectedIn"
| "onDropdownFocus"
| "onTimeChange"
Expand Down Expand Up @@ -266,6 +263,7 @@
dropdownMode: "scroll" as const,
preventOpenOnFocus: false,
monthsShown: 1,
outsideClickIgnoreClass: OUTSIDE_CLICK_IGNORE_CLASS,
readOnly: false,
withPortal: false,
selectsDisabledDaysInRange: false,
Expand Down Expand Up @@ -1236,7 +1234,7 @@
onSelect={this.handleSelect}
onClickOutside={this.handleCalendarClickOutside}
holidays={getHolidaysMap(this.modifyHolidays())}
outsideClickIgnoreClass={outsideClickIgnoreClass}
outsideClickIgnoreClass={this.props.outsideClickIgnoreClass}
onDropdownFocus={this.handleDropdownFocus}
onTimeChange={this.handleTimeChange}
className={this.props.calendarClassName}
Expand Down Expand Up @@ -1325,7 +1323,8 @@

renderDateInput = () => {
const className = clsx(this.props.className, {
[outsideClickIgnoreClass]: this.state.open,
[this.props.outsideClickIgnoreClass ||
DatePicker.defaultProps.outsideClickIgnoreClass]: this.state.open,

Check warning on line 1327 in src/index.tsx

View check run for this annotation

Codecov / codecov/patch

src/index.tsx#L1327

Added line #L1327 was not covered by tests
});

const customInput = this.props.customInput || <input type="text" />;
Expand Down
41 changes: 33 additions & 8 deletions src/test/datepicker_test.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { render, act, waitFor, fireEvent } from "@testing-library/react";
import { userEvent } from "@testing-library/user-event";
import { enUS, enGB } from "date-fns/locale";
import React, { useState } from "react";
import { OUTSIDE_CLICK_IGNORE_CLASS } from "../calendar";

import {
KeyType,
Expand Down Expand Up @@ -687,21 +688,45 @@ describe("DatePicker", () => {
});
});

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

it("should apply the react-datepicker-ignore-onclickoutside class to date input when open", () => {
it("should apply the default outsideClickIgnoreClass class to date input when open", () => {
const { container } = render(<DatePicker />);
const input = safeQuerySelector<HTMLInputElement>(container, "input");
fireEvent.focus(input);
expect(
input?.classList.contains("react-datepicker-ignore-onclickoutside"),
).toBeTruthy();
expect(input?.classList.contains(OUTSIDE_CLICK_IGNORE_CLASS)).toBe(true);
});

it("should apply the outsideClickIgnoreClass class to date input when open", () => {
const outsideClickIgnoreClass = "ignore-onclickoutside";
const { container } = render(
<DatePicker outsideClickIgnoreClass={outsideClickIgnoreClass} />,
);
const input = safeQuerySelector<HTMLInputElement>(container, "input");
fireEvent.focus(input);
expect(input?.classList.contains(outsideClickIgnoreClass)).toBe(true);
});

it("should apply the default outsideClickIgnoreClass when prop is undefined", () => {
const { container } = render(
<DatePicker outsideClickIgnoreClass={undefined} />,
);
const input = safeQuerySelector<HTMLInputElement>(container, "input");
fireEvent.focus(input);
expect(input?.classList.contains(OUTSIDE_CLICK_IGNORE_CLASS)).toBe(true);
});

it("should apply the default outsideClickIgnoreClass when prop is falsy", () => {
const { container } = render(
<DatePicker outsideClickIgnoreClass={undefined} />,
);
const input = safeQuerySelector<HTMLInputElement>(container, "input");
fireEvent.focus(input);
expect(input?.classList.contains(OUTSIDE_CLICK_IGNORE_CLASS)).toBe(true);
});

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