年份日期范围输入在Reactjs中

huangapple go评论80阅读模式
英文:

Year date range input in Reactjs

问题

有人知道如何在reactjs中实现年份范围输入(选择年份,例如2013-2023)吗?我尝试过使用react-year-range-picker,但它不起作用。

英文:

Does anyone know how to do year range input (to choose the years such as 2013-2023) in reactjs. I have tried with react-year-range-picker. But this doesnot work

答案1

得分: 1

要在React中创建一个年份范围输入,您可以使用react-year-range-picker模块。然而,此模块需要一些对等依赖,如styled-components,所以您需要像这样安装此模块:

npm i react-year-range-picker styled-components --legacy-peer-deps

这段代码可以正常运行:

import React, { useState } from "react";
import { YearRangePicker } from "react-year-range-picker";

const App = () => {
  const [yearRange, setYearRange] = useState({
    startYear: 2010,
    endYear: 2020,
  });

  return (
    <div>
      <h1>年份范围选择器</h1>
      <YearRangePicker
        minYear={2000}
        maxYear={2030}
        onSelect={(startYear, endYear) => {
          setYearRange({ startYear, endYear });
        }}
        startYear={yearRange.startYear}
        endYear={yearRange.endYear}
      />
      <p>
        选择的年份范围{yearRange.startYear} - {yearRange.endYear}
      </p>
    </div>
  );
};

export default App;
英文:

To create a year range input in React, You can use react-year-range-picker module. However, this module requires some peer dependencies such as styled-components so you need to install this module like this:

npm i react-year-range-picker styled-components --legacy-peer-deps

This code works fine:

import React, { useState } from &quot;react&quot;;
import { YearRangePicker } from &quot;react-year-range-picker&quot;;

const App = () =&gt; {
  const [yearRange, setYearRange] = useState({
    startYear: 2010,
    endYear: 2020,
  });

  return (
    &lt;div&gt;
      &lt;h1&gt;Year Range Picker&lt;/h1&gt;
      &lt;YearRangePicker
        minYear={2000}
        maxYear={2030}
        onSelect={(startYear, endYear) =&gt; {
          setYearRange({ startYear, endYear });
        }}
        startYear={yearRange.startYear}
        endYear={yearRange.endYear}
      /&gt;
      &lt;p&gt;
        Selected Years: {yearRange.startYear} - {yearRange.endYear}
      &lt;/p&gt;
    &lt;/div&gt;
  );
};

export default App;

huangapple
  • 本文由 发表于 2023年7月10日 16:12:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76651868.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定