benitolopez / hotel-datepicker

Date range picker for hotels
https://hoteldatepicker.org
MIT License
307 stars 95 forks source link

cannot show datepicker on nextJs #194

Closed alvinChristianto closed 1 month ago

alvinChristianto commented 1 month ago

Hi, i want to implement hotel-datepicker inside react function (nextJs),. Its fresh instalation of nextJs. It already show the input tag, but when i click the input tag, nothing show. here is the screenshot of my code ` "use client"; import React, { useRef, useEffect } from "react";

import * as fecha from "fecha"; import HotelDatepicker from "hotel-datepicker"; import "hotel-datepicker/dist/css/hotel-datepicker.css";

function dateHotel() { const inputRef = useRef(null);

const handleDatepickerInit = () => { if (inputRef.current) { var datepicker = new HotelDatepicker(inputRef.current); } };

useEffect(() => { handleDatepickerInit(); console.log("here"); }, []); // Run only once on component mount

return (

  <section className="">
    <div>
      <input ref={inputRef} type="text" id="input-id" />
    </div>
  </section>

); }

export default dateHotel; `

this code only show the input tag and when I click it, nothing show image

ps : I already install hotel-datepicker and fetca

benitolopez commented 1 month ago

Hello, modify the code to:

"use client";
import React, { useRef, useEffect } from "react";

import * as fecha from "fecha";
import HotelDatepicker from "hotel-datepicker";
import "hotel-datepicker/dist/css/hotel-datepicker.css";

function DatepickerComponent() {
  const inputRef = useRef(null);
  let datepicker = null;

  const handleDatepickerInit = () => {
    if (inputRef.current) {
      datepicker = new HotelDatepicker(inputRef.current);
    }
  };

  useEffect(() => {
    handleDatepickerInit();
    return () => {
      if (datepicker) {
        datepicker.destroy();
      }
    };
  }, []); // Run only once on component mount

  return (
    <section className="">
      <div>
        <input ref={inputRef} type="text" id="input-id" />
      </div>
    </section>
  );
}

export default DatepickerComponent;
alvinChristianto commented 1 month ago

I did it, and yes its show the calendar event. but how to get Value of date. I assume it access getValue(), but dont know where to place it

alvinChristianto commented 1 month ago

i did this, and it works pretty good to get date check in, date check out and nights , Thanks @benitolopez

"use client";
import React, { useRef, useEffect, useState } from "react";

import * as fecha from "fecha";
import HotelDatepicker from "hotel-datepicker";
import "hotel-datepicker/dist/css/hotel-datepicker.css";

function DatepickerComponent() {
  const inputRef = useRef(null);
  let datepicker = null;
  const [inDate, setInDate] = useState("");
  const [outDate, setOutDate] = useState("");
  const [nights, setNights] = useState(0);

  const handleDatepickerInit = () => {
    if (inputRef.current) {
      datepicker = new HotelDatepicker(inputRef.current);
    }
  };

  useEffect(() => {
    handleDatepickerInit();
    return () => {
      if (datepicker) { 
        datepicker.destroy();
      }
    };
  }, []); // Run only once on component mount

  useEffect(() => {
    if (inputRef.current) {
      const handleAfterClose = () => {
        const fullValue = datepicker.getValue();

        const dt = fullValue.split(" - ")
        setInDate(dt[0])
        setOutDate(dt[1])

        setNights(datepicker.getNights())

      };

      inputRef.current.addEventListener("afterClose", handleAfterClose);

    }
  }, [inputRef]);

  return (
    <section className="">
      <div>
        <input ref={inputRef} type="text" id="input-id" className="text-slate-900 p-3 rounded-md"/>
      </div>
    </section>
  );
}

export default DatepickerComponent;