Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to open HTML date input interface with react ref?

I have a date input element that I want to open it’s interface programmatically with React refs. Implementation provided blow didn’t work.


function myComponent() {
  const dateRef = useRef(null);
  return (
    <>
      <input type="date" ref={dateRef} />;
      <button
        onClick={() => {
          dateRef.current.click();
        }}
      >
        Open Date Interface
      </button>
    </>
  );
}

Here, when I click the button I want to open date picker interface. How can I achieve this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

That was close, you can just modify the click() method into showPicker()

function myComponent() {
  const dateRef = useRef(null);
  return (
    <>
      <input type="date" ref={dateRef} />;
      <button
        onClick={() => {
          dateRef.current.showPicker();
        }}
      >
        Open Date Interface
      </button>
    </>
  );
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading