Skip to content
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
5 changes: 1 addition & 4 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -700,10 +700,7 @@ export class DatePicker extends Component<DatePickerProps, DatePickerState> {
};

resetInputValue = () => {
this.setState({
...this.state,
inputValue: null,
});
this.setState({ inputValue: null });
};

handleBlur = (event: React.FocusEvent<HTMLElement>) => {
Expand Down
41 changes: 41 additions & 0 deletions src/test/reset_input_value.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { render } from "@testing-library/react";
import React, { useState, act } from "react";

import DatePicker from "../index";

function Fixture() {
const [date, setDate] = useState<Date | null>(null);
return (
<div>
<DatePicker id="start" selected={date} onChange={setDate} />
<button id="outside">outside</button>
</div>
);
}

const openCalendars = () => document.querySelectorAll(".react-datepicker");

describe("DatePicker resetInputValue", () => {
it("stays closed when an outside mousedown and the input blur land in the same task", () => {
render(<Fixture />);
const input = document.querySelector<HTMLInputElement>("#start")!;
const outside = document.querySelector<HTMLButtonElement>("#outside")!;

act(() => {
input.focus();
input.dispatchEvent(new FocusEvent("focusin", { bubbles: true }));
});
expect(openCalendars()).toHaveLength(1);

// A browser dispatches the outside mousedown and the resulting input blur
// in a single task. React has not committed setOpen(false) by the time the
// blur handler runs, so resetInputValue must not write back a snapshot of
// this.state: that snapshot still says open, and the calendar reopens.
act(() => {
outside.dispatchEvent(new MouseEvent("mousedown", { bubbles: true }));
input.dispatchEvent(new FocusEvent("focusout", { bubbles: true }));
});

expect(openCalendars()).toHaveLength(0);
});
});