Comments (2)
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.
Thanks for the help @Sqrrl, appreciate it. I'll use this workaround for now. Great project, btw!
from wc-datepicker.
Related Issues (9)
- Kudos!! HOT 1
- [BUG] Broken calendar in some timezones HOT 2
- [QUESTION] …example doesn't work HOT 4
- [BUG] Console error is shown if a user enters unsupported year HOT 3
- [QUESTION] … how to define a default range? HOT 2
- Set default value from html HOT 5
- [QUESTION] Show/hide widget HOT 1
- A11y improvements HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from wc-datepicker.