Skip to content
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

I want the date picker modal to be open by default when the page loads, i didn't saw anything #256

Open
rahabonyani opened this issue Jun 1, 2024 · 1 comment

Comments

@rahabonyani
Copy link

No description provided.

@rahabonyani rahabonyani changed the title I want the date picker modal to be open by default when the page loads, i didn't saw anything The date picker modal to be open by default Jun 1, 2024
@rahabonyani rahabonyani changed the title The date picker modal to be open by default I want the date picker modal to be open by default when the page loads, i didn't saw anything Jun 1, 2024
@MeepCastana
Copy link

import React, { useEffect, useRef } from 'react';
import Datepicker from 'react-tailwindcss-datepicker';

const Test = () => {
  const datepickerWrapperRef = useRef(null);

  useEffect(() => {
    const openDatepicker = () => {
      setTimeout(() => {
        const inputElement = datepickerWrapperRef.current?.querySelector('input');
        if (inputElement) {
          inputElement.focus(); 
          const clickEvent = new MouseEvent('click', { bubbles: true });
          inputElement.dispatchEvent(clickEvent);
        }
      }, 500); 
    };

    openDatepicker();
  }, []);

  return (
    <div ref={datepickerWrapperRef}>
      <Datepicker
      />
    </div>
  );
};

export default Test;

If you want to ensure your datepicker opens automatically when the page loads, you can use a combination of React hooks and a simulated click event. Here's how:

Reference the Wrapper div

Create a useRef hook to reference the div containing the Datepicker component. This will allow you to interact with the DOM element directly.

Use setTimeout to Delay the Click Event

To make sure the simulated click event is dispatched after the component is fully rendered, use the setTimeout function. This will delay the click event, allowing the DOM to be completely ready.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants