sbmdkl / nepali-datepicker-reactjs

20 stars 16 forks source link

Default value.... #5

Closed bsalpsingh closed 3 years ago

bsalpsingh commented 3 years ago

Unable to set the default value from states as it always starts from today's date....is there a way to pass dates from States to show on the input filed...

sbmdkl commented 3 years ago

You can pass defaultDate props to <Calendar /> component. Here is a working example for setting default date from the state:

import React, { useState } from "react";

import Calendar from "@sbmdkl/nepali-datepicker-reactjs";
import "@sbmdkl/nepali-datepicker-reactjs/dist/index.css";

function App() {
  const [date, setDate] = useState("");
  const [myDate, setMyDate] = useState("2075-10-12");

  const handleDate = ({ bsDate, adDate }) => {
    setDate({ date: bsDate });
  };
  return (
    <div>
      <h1>Nepali Date Picker for React</h1>
      <Calendar defaultDate={myDate} onChange={handleDate} theme="deepdark" />
    </div>
  );
}

export default App;

Demo: https://codesandbox.io/s/mystifying-leaf-h9ixt?file=/src/App.js:0-561

sjonchhe commented 2 years ago

defaultDate doesnt update on rerender. it only works if default date exists in initial render

I have got 2 datepickers, I am trying to set default date of second date picker when date has been selected on the first datepicker. But it default date on second datepicker doesnt get updated. Is there any workaround for this? https://codesandbox.io/s/zen-monad-s5t7u5?file=/src/App.js