react-datepicker years range

匆匆过客 提交于 2021-02-10 22:43:37

问题


I am using the react-datepicker package.https://reactdatepicker.com/

I am trying to create a custom date picker with a years range, following their example but I can not make the range work. I am not even sure if I am importing that range from the right place tbh.

My code:

import DatePicker, {getYear, getMonth, range}  from 'react-datepicker';
import "react-datepicker/dist/react-datepicker.css";


const Foo = () => {
    const [startDate, setStartDate] = useState(new Date());
    const years = range(1990, getYear(new Date()) + 1, 1);
    const months = [
      "January",
      "February",
      "March",
      "April",
      "May",
      "June",
      "July",
      "August",
      "September",
      "October",
      "November",
      "December"
    ];
    return (
      <DatePicker
        renderCustomHeader={({
          date,
          changeYear,
          changeMonth,
          decreaseMonth,
          increaseMonth,
          prevMonthButtonDisabled,
          nextMonthButtonDisabled
        }) => (
          <div
            style={{
              margin: 10,
              display: "flex",
              justifyContent: "center"
            }}
          >
            <button onClick={decreaseMonth} disabled={prevMonthButtonDisabled}>
              {"<"}
            </button>
            <select
              value={getYear(date)}
              onChange={({ target: { value } }) => changeYear(value)}
            >
              {years.map(option => (
                <option key={option} value={option}>
                  {option}
                </option>
              ))}
            </select>
  
            <select
              value={months[getMonth(date)]}
              onChange={({ target: { value } }) =>
                changeMonth(months.indexOf(value))
              }
            >
              {months.map(option => (
                <option key={option} value={option}>
                  {option}
                </option>
              ))}
            </select>
  
            <button onClick={increaseMonth} disabled={nextMonthButtonDisabled}>
              {">"}
            </button>
          </div>
        )}
        selected={startDate}
        onChange={date => setStartDate(date)}
      />
    );
  };

My error:

TypeError: Object(...) is not a function

const years = range(1990, getYear(new Date()) + 1, 1);

回答1:


Use below imports and all the code in documentation will work fine!

import { getMonth, getYear } from 'date-fns';
import range from "lodash/range";



回答2:


Functions getYear and getMonth are imported from date-fns library into react-datepicker/src/date_utils.js

I don't know about range function. I see it's being used in one example at their site (just a bunch of examples, no documentation) but there's nothing else.

You can simply import from date-fns (being a dependency of DataPicker) and create a range function.

import React, { useState } from "react";
import DatePicker from "react-datepicker";
import getYear from "date-fns/getYear";
import getMonth from "date-fns/getYear";
import "react-datepicker/dist/react-datepicker.css";

const Foo = () => {
  const [startDate, setStartDate] = useState(new Date());

  const range = (start, end) => {
    return new Array(end - start).fill().map((d, i) => i + start);
  };
  const years = range(1990, getYear(new Date()));
(...)

You can check a working demo here.



来源:https://stackoverflow.com/questions/63221355/react-datepicker-years-range

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!