Skip to content

Commit 79c1702

Browse files
Merge pull request #5703 from qburst/issues/5571/handle-input-blur-event
🐛 Reset inputValue back to null on blur of input (even when the calendar popup is not open)
2 parents bb8049c + 58f4afc commit 79c1702

File tree

2 files changed

+50
-5
lines changed

2 files changed

+50
-5
lines changed

src/index.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,11 +553,20 @@ export default class DatePicker extends Component<
553553
this.cancelFocusInput();
554554
};
555555

556+
resetInputValue = () => {
557+
this.setState({
558+
...this.state,
559+
inputValue: null,
560+
});
561+
};
562+
556563
handleBlur = (event: React.FocusEvent<HTMLElement>) => {
557564
if (!this.state.open || this.props.withPortal || this.props.showTimeInput) {
558565
this.props.onBlur?.(event);
559566
}
560567

568+
this.resetInputValue();
569+
561570
if (this.state.open && this.props.open === false) {
562571
this.setOpen(false);
563572
}

src/test/datepicker_test.test.tsx

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4320,17 +4320,23 @@ describe("DatePicker", () => {
43204320
});
43214321

43224322
describe("input reset", () => {
4323-
const renderDatePickerInput = () => {
4324-
const WrapperComponent = () => {
4323+
const renderDatePickerInput = (open: boolean | null = null) => {
4324+
const WrapperComponent = ({ open }: { open: boolean | null }) => {
43254325
const [date, setDate] = useState<Date | null>(new Date());
4326-
return <DatePicker open={false} selected={date} onChange={setDate} />;
4326+
return (
4327+
<DatePicker
4328+
{...(open !== null && { open })}
4329+
selected={date}
4330+
onChange={setDate}
4331+
/>
4332+
);
43274333
};
43284334

4329-
return render(<WrapperComponent />);
4335+
return render(<WrapperComponent open={open} />);
43304336
};
43314337

43324338
it("should reset the date input element with the previously entered value element on blur even when the calendar open is false", () => {
4333-
const { container } = renderDatePickerInput();
4339+
const { container } = renderDatePickerInput(false);
43344340
const input = safeQuerySelector(container, "input") as HTMLInputElement;
43354341

43364342
if (!input) {
@@ -4357,6 +4363,36 @@ describe("DatePicker", () => {
43574363
fireEvent.blur(input);
43584364
expect(input.value).toBe(DATE_VALUE);
43594365
});
4366+
4367+
it("should reset the date input element with the previously entered value on blur even when the calendar is not open", () => {
4368+
const { container } = renderDatePickerInput();
4369+
const input = safeQuerySelector(container, "input") as HTMLInputElement;
4370+
4371+
fireEvent.click(input);
4372+
4373+
const VALID_DATE_VALUE = "06/23/2025";
4374+
fireEvent.change(input, {
4375+
target: {
4376+
value: VALID_DATE_VALUE,
4377+
},
4378+
});
4379+
fireEvent.blur(input);
4380+
expect(input.value).toBe(VALID_DATE_VALUE);
4381+
4382+
fireEvent.click(input);
4383+
fireEvent.keyDown(input, getKey(KeyType.Escape));
4384+
// Make sure the calendar is hidden
4385+
expect(container.querySelector(".react-datepicker")).toBeFalsy();
4386+
4387+
const INVALID_DATE_VALUE = "2025-02-45";
4388+
fireEvent.change(input, {
4389+
target: {
4390+
value: INVALID_DATE_VALUE,
4391+
},
4392+
});
4393+
fireEvent.blur(input);
4394+
expect(input.value).toBe(VALID_DATE_VALUE);
4395+
});
43604396
});
43614397

43624398
describe("Close on ESC Key", () => {

0 commit comments

Comments
 (0)