GithubHelp home page GithubHelp logo

Comments (2)

Sqrrl avatar Sqrrl commented on July 20, 2024

Updating the picker value in Vanilla JavaScript seems to work fine. Haven't tried it with React so far. Not sure what causes it in your example, but I'll look into it.

This seems to be a workaround for now:

import { defineCustomElements } from "wc-datepicker/dist/loader";
import { useState, useEffect, useRef } from "react";
import "wc-datepicker/dist/themes/light.css";

defineCustomElements();

function DateInput() {
  const [value, setValue] = useState("");
  const datepicker = useRef();

  useEffect(() => {
    function updateValue(e) {
      const currentDate = e.target.value;
      const year = currentDate.getFullYear(); // get the year component
      const month = currentDate.getMonth() + 1; // get the month component (adding 1 because January is 0)
      const day = currentDate.getDate(); // get the day component
      const dateString = `${year}-${month
        .toString()
        .padStart(2, "0")}-${day.toString().padStart(2, "0")}`;
      setValue(dateString);
    }
    datepicker.current.addEventListener("selectDate", updateValue);
    return () =>
      datepicker.current.removeEventListener("selectDate", updateValue);
  });

  useEffect(() => {
    datepicker.current.value = value ? new Date(value) : undefined;
  }, [value]);

  return (
    <>
      {value}
      <br />
      <input
        type="date"
        value={value}
        onChange={(e) => setValue(e.target.value)}
      />
      <br />
      <wc-datepicker
        id="datepicker"
        start-date={value ? new Date(value) : undefined}
        ref={(el) => (datepicker.current = el)}
      ></wc-datepicker>
    </>
  );
}

export default DateInput;

I've also moved the defineCustomElements call out of the component declaration. This function should only be called once. Also, resetting the start-date on each value change should not be necessary.

from wc-datepicker.

jayehernandez avatar jayehernandez commented on July 20, 2024

Thanks for the help @Sqrrl, appreciate it. I'll use this workaround for now. Great project, btw!

from wc-datepicker.

Related Issues (9)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.