@abdubek/react-datepicker 中文文档教程

发布于 3年前 浏览 29 项目主页 更新于 3年前

React Date Picker

npm 版本测试套件依赖状态codecov下载代码质量:JavascriptTotal Alerts

用于 React 的简单且可重复使用的日期选择器组件(演示< /a>)

Installation

可以通过 npm

npm install react-datepicker --save

或者通过 yarn

yarn add react-datepicker

你'您需要单独安装 React 和 PropTypes,因为这些依赖项未包含在包中。 如果您需要使用默认 en-US 以外的区域设置,您还需要将其从 date-fns 导入到您的项目中(请参阅下面的本地化部分)。 下面是一个如何在 React 视图中使用 Datepicker 的简单示例。 您还需要从这个包中获取 CSS 文件(或提供您自己的文件)。 下面的示例显示了如果您的构建系统支持需要 CSS 文件(Webpack 是一个支持),如何从这个包中包含 CSS。

import React, { useState } from "react";
import DatePicker from "react-datepicker";

import "react-datepicker/dist/react-datepicker.css";

// CSS Modules, react-datepicker-cssmodules.css
// import 'react-datepicker/dist/react-datepicker-cssmodules.css';

const Example = () => {
  const [startDate, setStartDate] = useState(new Date());
  return (
    <DatePicker selected={startDate} onChange={(date) => setStartDate(date)} />
  );
};

Configuration

DatePicker 的最基本用法可以描述为:

<DatePicker selected={startdate} onChange={(date) => setStartDate(date)} />

您可以使用 onSelect 事件处理程序,它会在每次选择某个日历日期时触发

<DatePicker
  selected={date}
  onSelect={handleDateSelect} //when day is clicked
  onChange={handleDateChange} //only when value has changed
/>

onClickOutside 处理程序可能对关闭 datepicker 很有用在 inline 模式下

查看此处获取完整列表可以传递给组件的道具。 主网站 上提供了示例。

Time picker

您还可以通过添加 showTimeSelect 属性来包含时间选择器

<DatePicker
  selected={date}
  onChange={handleDateChange}
  showTimeSelect
  dateFormat="Pp"
/>

默认情况下,时间将以 30 分钟的间隔显示(默认可通过 timeIntervals 属性进行配置)

有关如何使用时间选择器的更多示例在 主站

Localization

日期选择器依赖date-fns 国际化 对其显示组件进行本地化。 默认情况下,日期选择器将使用全局设置的语言环境,即英语。 提供了 3 个设置语言环境的辅助方法:

  • registerLocale (string, object): loads an imported locale object from date-fns
  • setDefaultLocale (string): sets a registered locale as the default for all datepicker instances
  • getDefaultLocale: returns a string showing the currently set default locale
import { registerLocale, setDefaultLocale } from  "react-datepicker";
import es from 'date-fns/locale/es';
registerLocale('es', es)

<DatePicker
  locale="es"
/>

可以通过以下方式更改语言环境:

  • Globally - setDefaultLocale('es');

Compatibility

React

我们一直在努力与最新版本的 React 保持兼容。 我们不能支持所有旧版本的 React。

最新兼容版本:

  • React 16 or newer: React-datepicker v2.9.4 and newer
  • React 15.5: React-datepicker v2.9.3
  • React 15.4.1: needs React-datepicker v0.40.0, newer won't work (due to react-onclickoutside dependencies)
  • React 0.14 or newer: All above React-datepicker v0.13.0
  • React 0.13: React-datepicker v0.13.0
  • pre React 0.13: React-datepicker v0.6.2

Moment.js

直到版本 1.8.0,这个包使用的是 Moment.js。 从 v2.0.0 开始,我们改用 date-fns,它使用原生 Date 对象,以减小包的大小。 如果您要从 1.8.0 切换到 2.0.0 或更高版本,请查看上面更新的示例,查看示例站点以获取最新信息例子。

Browser Support

日期选择器与最新版本的 Chrome、Firefox 和 IE10+ 兼容。

不幸的是,很难在支持遗留浏览器的同时保持我们在未来开发新功能的能力。 对于 IE9 支持,已知需要 classlist polyfill,但这可能会随时更改或中断未来。

Local Development

master 分支包含最新版本的 Datepicker 组件。

开始本地开发:

  1. yarn install
  2. yarn build-dev
  3. yarn start

最后一步在 http://localhost:3000 上将文档应用程序作为一个简单的网络服务器启动。

您可以运行 yarn test 来执行测试套件和 linter。 为了帮助您开发组件,我们设置了一些涵盖基本功能的测试(可在 /tests 中找到)。 尽管我们是测试的忠实拥护者,但这只涵盖了组件的一小部分。 我们强烈建议您在添加新功能时添加测试。

The examples

这些示例托管在 docs 文件夹中,并在加载 Datepicker 的简单应用程序中运行。 要使用新示例扩展示例,您可以简单地复制现有示例之一并更改示例的独特属性。

Accessibility

Keyboard support

  • Left: Move to the previous day.
  • Right: Move to the next day.
  • Up: Move to the previous week.
  • Down: Move to the next week.
  • PgUp: Move to the previous month.
  • PgDn: Move to the next month.
  • Home: Move to the previous year.
  • End: Move to the next year.
  • Enter/Esc/Tab: close the calendar. (Enter & Esc calls preventDefault)

For month picker

  • Left: Move to the previous month.
  • Right: Move to the next month.
  • Enter: Select date and close the calendar

License

版权所有 (c) 2014-2021 HackerOne Inc. 和个人贡献者。 根据 MIT 许可证获得许可,请参阅 LICENSE 以获取完整许可证。

React Date Picker

npm versionTest suiteDependency StatuscodecovDownloadsCode Quality: JavascriptTotal Alerts

A simple and reusable Datepicker component for React (Demo)

Installation

The package can be installed via npm:

npm install react-datepicker --save

Or via yarn:

yarn add react-datepicker

You’ll need to install React and PropTypes separately since those dependencies aren’t included in the package. If you need to use a locale other than the default en-US, you'll also need to import that into your project from date-fns (see Localization section below). Below is a simple example of how to use the Datepicker in a React view. You will also need to require the CSS file from this package (or provide your own). The example below shows how to include the CSS from this package if your build system supports requiring CSS files (Webpack is one that does).

import React, { useState } from "react";
import DatePicker from "react-datepicker";

import "react-datepicker/dist/react-datepicker.css";

// CSS Modules, react-datepicker-cssmodules.css
// import 'react-datepicker/dist/react-datepicker-cssmodules.css';

const Example = () => {
  const [startDate, setStartDate] = useState(new Date());
  return (
    <DatePicker selected={startDate} onChange={(date) => setStartDate(date)} />
  );
};

Configuration

The most basic use of the DatePicker can be described with:

<DatePicker selected={startdate} onChange={(date) => setStartDate(date)} />

You can use onSelect event handler which fires each time some calendar date has been selected

<DatePicker
  selected={date}
  onSelect={handleDateSelect} //when day is clicked
  onChange={handleDateChange} //only when value has changed
/>

onClickOutside handler may be useful to close datepicker in inline mode

See here for a full list of props that may be passed to the component. Examples are given on the main website.

Time picker

You can also include a time picker by adding the showTimeSelect prop

<DatePicker
  selected={date}
  onChange={handleDateChange}
  showTimeSelect
  dateFormat="Pp"
/>

Times will be displayed at 30-minute intervals by default (default configurable via timeIntervals prop)

More examples of how to use the time picker are given on the main website

Localization

The date picker relies on date-fns internationalization to localize its display components. By default, the date picker will use the locale globally set, which is English. Provided are 3 helper methods to set the locale:

  • registerLocale (string, object): loads an imported locale object from date-fns
  • setDefaultLocale (string): sets a registered locale as the default for all datepicker instances
  • getDefaultLocale: returns a string showing the currently set default locale
import { registerLocale, setDefaultLocale } from  "react-datepicker";
import es from 'date-fns/locale/es';
registerLocale('es', es)

<DatePicker
  locale="es"
/>

Locales can be changed in the following way:

  • Globally - setDefaultLocale('es');

Compatibility

React

We're always trying to stay compatible with the latest version of React. We can't support all older versions of React.

Latest compatible versions:

  • React 16 or newer: React-datepicker v2.9.4 and newer
  • React 15.5: React-datepicker v2.9.3
  • React 15.4.1: needs React-datepicker v0.40.0, newer won't work (due to react-onclickoutside dependencies)
  • React 0.14 or newer: All above React-datepicker v0.13.0
  • React 0.13: React-datepicker v0.13.0
  • pre React 0.13: React-datepicker v0.6.2

Moment.js

Up until version 1.8.0, this package was using Moment.js. Starting v2.0.0, we switched to using date-fns, which uses native Date objects, to reduce the size of the package. If you're switching from 1.8.0 to 2.0.0 or higher, please see the updated example above of check out the examples site for up to date examples.

Browser Support

The date picker is compatible with the latest versions of Chrome, Firefox, and IE10+.

Unfortunately, it is difficult to support legacy browsers while maintaining our ability to develop new features in the future. For IE9 support, it is known that the classlist polyfill is needed, but this may change or break at any point in the future.

Local Development

The master branch contains the latest version of the Datepicker component.

To begin local development:

  1. yarn install
  2. yarn build-dev
  3. yarn start

The last step starts documentation app as a simple webserver on http://localhost:3000.

You can run yarn test to execute the test suite and linters. To help you develop the component we’ve set up some tests that cover the basic functionality (can be found in /tests). Even though we’re big fans of testing, this only covers a small piece of the component. We highly recommend you add tests when you’re adding new functionality.

The examples

The examples are hosted within the docs folder and are ran in the simple app that loads the Datepicker. To extend the examples with a new example, you can simply duplicate one of the existing examples and change the unique properties of your example.

Accessibility

Keyboard support

  • Left: Move to the previous day.
  • Right: Move to the next day.
  • Up: Move to the previous week.
  • Down: Move to the next week.
  • PgUp: Move to the previous month.
  • PgDn: Move to the next month.
  • Home: Move to the previous year.
  • End: Move to the next year.
  • Enter/Esc/Tab: close the calendar. (Enter & Esc calls preventDefault)

For month picker

  • Left: Move to the previous month.
  • Right: Move to the next month.
  • Enter: Select date and close the calendar

License

Copyright (c) 2014-2021 HackerOne Inc. and individual contributors. Licensed under MIT license, see LICENSE for the full license.

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文