diff --git a/docs-site/src/examples/customTimeInput.js b/docs-site/src/examples/customTimeInput.js index a28971b2d..bb62566f6 100644 --- a/docs-site/src/examples/customTimeInput.js +++ b/docs-site/src/examples/customTimeInput.js @@ -4,6 +4,7 @@ onChange(e.target.value)} + onClick={(e) => e.target?.focus()} style={{ border: "solid 1px pink" }} /> ); diff --git a/src/input_time.tsx b/src/input_time.tsx index d039d2ceb..0fe8234ff 100644 --- a/src/input_time.tsx +++ b/src/input_time.tsx @@ -32,6 +32,8 @@ export default class InputTime extends Component< InputTimeProps, InputTimeState > { + inputRef: React.RefObject = React.createRef(); + constructor(props: InputTimeProps) { super(props); @@ -86,8 +88,12 @@ export default class InputTime extends Component< { + this.inputRef.current?.focus(); + }} required value={time} onChange={(event) => { diff --git a/src/test/time_input_test.test.tsx b/src/test/time_input_test.test.tsx index 7dd8fb049..bc332753e 100644 --- a/src/test/time_input_test.test.tsx +++ b/src/test/time_input_test.test.tsx @@ -161,4 +161,19 @@ describe("timeInput", () => { dateSpy.mockRestore(); }); + + it("should focus on the time input when the time input gets the click event", () => { + const { container } = render( + , + ); + + const input = safeQuerySelector(container, "input"); + fireEvent.focus(input); + const timeInput = safeQuerySelector( + container, + 'input[type="time"].react-datepicker-time__input', + ); + fireEvent.click(timeInput); + expect(document.activeElement).toBe(timeInput); + }); });