1 | # react-datetime
|
2 |
|
3 | [![Build Status](https://secure.travis-ci.org/YouCanBookMe/react-datetime.svg)](https://travis-ci.org/YouCanBookMe/react-datetime)
|
4 | [![npm version](https://badge.fury.io/js/react-datetime.svg)](http://badge.fury.io/js/react-datetime)
|
5 |
|
6 | A date and time picker in the same React.js component. It can be used as a datepicker, timepicker or both at the same time. It is **highly customizable** and it even allows to edit date's milliseconds.
|
7 |
|
8 | This project started as a fork of https://github.com/quri/react-bootstrap-datetimepicker but the code and the API has changed a lot.
|
9 |
|
10 | ## Installation
|
11 |
|
12 | Install using npm:
|
13 | ```sh
|
14 | npm install --save react-datetime
|
15 | ```
|
16 |
|
17 | Install using yarn:
|
18 | ```sh
|
19 | yarn add react-datetime
|
20 | ```
|
21 |
|
22 | ## Usage
|
23 |
|
24 | [React.js](http://facebook.github.io/react/) and [Moment.js](http://momentjs.com/) are peer dependencies for react-datetime (as well as [Moment.js timezones](https://momentjs.com/timezone/) if you want to use the `displayTimeZone` prop). These dependencies are not installed along with react-datetime automatically, but your project needs to have them installed in order to make the datepicker work. You can then use the datepicker like in the example below.
|
25 |
|
26 |
|
27 | ```js
|
28 | require('react-datetime');
|
29 |
|
30 | ...
|
31 |
|
32 | render: function() {
|
33 | return <Datetime />;
|
34 | }
|
35 | ```
|
36 | [See this example working](http://codepen.io/simeg/pen/mEmQmP).
|
37 |
|
38 | **Don't forget to add the [CSS stylesheet](https://github.com/YouCanBookMe/react-datetime/blob/master/css/react-datetime.css) to make it work out of the box.**
|
39 |
|
40 | ## API
|
41 |
|
42 | | Name | Type | Default | Description |
|
43 | | ------------ | ------- | ------- | ----------- |
|
44 | | **value** | `Date` | `new Date()` | Represents the selected date by the component, in order to use it as a [controlled component](https://facebook.github.io/react/docs/forms.html#controlled-components). This prop is parsed by Moment.js, so it is possible to use a date `string` or a `moment` object. |
|
45 | | **defaultValue** | `Date` | `new Date()` | Represents the selected date for the component to use it as a [uncontrolled component](https://facebook.github.io/react/docs/uncontrolled-components.html). This prop is parsed by Moment.js, so it is possible to use a date `string` or a `moment` object. |
|
46 | | **viewDate** | `Date` | `new Date()` | Represents the month which is viewed on opening the calendar when there is no selected date. This prop is parsed by Moment.js, so it is possible to use a date `string` or a `moment` object. |
|
47 | | **dateFormat** | `boolean` or `string` | `true` | Defines the format for the date. It accepts any [Moment.js date format](http://momentjs.com/docs/#/displaying/format/) (not in localized format). If `true` the date will be displayed using the defaults for the current locale. If `false` the datepicker is disabled and the component can be used as timepicker, see [available units docs](#specify-available-units). |
|
48 | | **timeFormat** | `boolean` or `string` | `true` | Defines the format for the time. It accepts any [Moment.js time format](http://momentjs.com/docs/#/displaying/format/) (not in localized format). If `true` the time will be displayed using the defaults for the current locale. If `false` the timepicker is disabled and the component can be used as datepicker, see [available units docs](#specify-available-units). |
|
49 | | **input** | `boolean` | `true` | Whether to show an input field to edit the date manually. |
|
50 | | **open** | `boolean` | `null` | Whether to open or close the picker. If not set react-datetime will open the datepicker on input focus and close it on click outside. |
|
51 | | **locale** | `string` | `null` | Manually set the locale for the react-datetime instance. Moment.js locale needs to be loaded to be used, see [i18n docs](#i18n).
|
52 | | **utc** | `boolean` | `false` | When true, input time values will be interpreted as UTC (Zulu time) by Moment.js. Otherwise they will default to the user's local timezone.
|
53 | | **displayTimeZone** | `string` | `null` | **Needs [moment's timezone](https://momentjs.com/timezone/) available in your project.** When specified, input time values will be displayed in the given time zone. Otherwise they will default to the user's local timezone (unless `utc` specified).
|
54 | | **onChange** | `function` | empty function | Callback trigger when the date changes. The callback receives the selected `moment` object as only parameter, if the date in the input is valid. If the date in the input is not valid, the callback receives the value of the input (a string). |
|
55 | | **onFocus** | `function` | empty function | Callback trigger for when the user opens the datepicker. The callback receives an event of type SyntheticEvent. |
|
56 | | **onBlur** | `function` | empty function | Callback trigger for when the user clicks outside of the input, simulating a regular onBlur. The callback receives the selected `moment` object as only parameter, if the date in the input is valid. If the date in the input is not valid, the callback returned. |
|
57 | | **onViewModeChange** | `function` | empty function | Callback trigger when the view mode changes. The callback receives the selected view mode string (`years`, `months`, `days` or `time`) as only parameter.|
|
58 | | **onNavigateBack** | `function` | empty function | Callback trigger when the user navigates to the previous month, year or decade. The callback receives the amount and type ('month', 'year') as parameters. |
|
59 | | **onNavigateForward** | `function` | empty function | Callback trigger when the user navigates to the next month, year or decade. The callback receives the amount and type ('month', 'year') as parameters. |
|
60 | | **viewMode** | `string` or `number` | `'days'` | The default view to display when the picker is shown (`'years'`, `'months'`, `'days'`, `'time'`). |
|
61 | | **className** | `string` or `string array` | `''` | Extra class name for the outermost markup element. |
|
62 | | **inputProps** | `object` | `undefined` | Defines additional attributes for the input element of the component. For example: `onClick`, `placeholder`, `disabled`, `required`, `name` and `className` (`className` *sets* the class attribute for the input element). See [Customize the Input Appearance](#customize-the-input-appearance). |
|
63 | | **isValidDate** | `function` | `() => true` | Define the dates that can be selected. The function receives `(currentDate, selectedDate)` and shall return a `true` or `false` whether the `currentDate` is valid or not. See [selectable dates](#selectable-dates).|
|
64 | | **renderInput** | `function` | `undefined` | Replace the rendering of the input element. The function has the following arguments: the default calculated `props` for the input, `openCalendar` (a function which opens the calendar) and `closeCalendar` (a function which closes the calendar). Must return a React component or `null`. See [Customize the Input Appearance](#customize-the-input-appearance). |
|
65 | | **renderDay** | `function` | `DOM.td(day)` | Customize the way that the days are shown in the daypicker. The accepted function has the `selectedDate`, the current date and the default calculated `props` for the cell, and must return a React component. See [Customize the Datepicker Appearance](#customize-the-datepicker-appearance). |
|
66 | | **renderMonth** | `function` | `DOM.td(month)` | Customize the way that the months are shown in the monthpicker. The accepted function has the `selectedDate`, the current date and the default calculated `props` for the cell, the `month` and the `year` to be shown, and must return a React component. See [Customize the Datepicker Appearance](#customize-the-datepicker-appearance). |
|
67 | | **renderYear** | `function` | `DOM.td(year)` | Customize the way that the years are shown in the year picker. The accepted function has the `selectedDate`, the current date and the default calculated `props` for the cell, the `year` to be shown, and must return a React component. See [Customize the Datepicker Appearance](#customize-the-datepicker-appearance). |
|
68 | | **strictParsing** | `boolean` | `true` | Whether to use Moment.js's [strict parsing](http://momentjs.com/docs/#/parsing/string-format/) when parsing input.
|
69 | | **closeOnSelect** | `boolean` | `false` | When `true`, once the day has been selected, the datepicker will be automatically closed.
|
70 | | **closeOnTab** | `boolean` | `true` | When `true` and the input is focused, pressing the `tab` key will close the datepicker.
|
71 | | **timeConstraints** | `object` | `null` | Add some constraints to the timepicker. It accepts an `object` with the format `{ hours: { min: 9, max: 15, step: 2 }}`, this example means the hours can't be lower than `9` and higher than `15`, and it will change adding or subtracting `2` hours everytime the buttons are clicked. The constraints can be added to the `hours`, `minutes`, `seconds` and `milliseconds`.
|
72 | | **disableCloseOnClickOutside** | `boolean` | `false` | When `true`, keep the datepicker open when click event is triggered outside of component. When `false`, close it.
|
73 |
|
74 | ## i18n
|
75 | Different language and date formats are supported by react-datetime. React uses [Moment.js](http://momentjs.com/) to format the dates, and the easiest way of changing the language of the calendar is [changing the Moment.js locale](http://momentjs.com/docs/#/i18n/changing-locale/).
|
76 |
|
77 | ```js
|
78 | var moment = require('moment');
|
79 | require('moment/locale/fr');
|
80 | // Now react-datetime will be in french
|
81 | ```
|
82 |
|
83 | If there are multiple locales loaded, you can use the prop `locale` to define what language shall be used by the instance.
|
84 | ```js
|
85 | <Datetime locale="fr-ca" />
|
86 | <Datetime locale="de" />
|
87 | ```
|
88 | [Here you can see the i18n example working](http://codepen.io/simeg/pen/yVVjdJ).
|
89 |
|
90 | ## Customize the Input Appearance
|
91 | It is possible to customize the way that the input is displayed. The simplest is to supply `inputProps` which get assigned to the default `<input />` element within the component.
|
92 |
|
93 | ```js
|
94 | <Datetime inputProps={{ placeholder: 'N/A', disabled: true }}>
|
95 | ```
|
96 |
|
97 | Alternatively, if you need to render different content than an `<input />` element, you may supply a `renderInput` function which is called instead.
|
98 |
|
99 | ```js
|
100 | var MyDTPicker = React.createClass({
|
101 | render: function(){
|
102 | return <Datetime renderInput={ this.renderInput } />;
|
103 | },
|
104 | renderInput: function( props, openCalendar, closeCalendar ){
|
105 | function clear(){
|
106 | props.onChange({target: {value: ''}});
|
107 | }
|
108 | return (
|
109 | <div>
|
110 | <input {...props} />
|
111 | <button onClick={openCalendar}>open calendar</button>
|
112 | <button onClick={closeCalendar}>close calendar</button>
|
113 | <button onClick={clear}>clear</button>
|
114 | </div>
|
115 | );
|
116 | },
|
117 | });
|
118 | ```
|
119 |
|
120 | ## Customize the Datepicker Appearance
|
121 | It is possible to customize the way that the datepicker display the days, months and years in the calendar. To adapt the calendar for every need it is possible to use the props `renderDay(props, currentDate, selectedDate)`, `renderMonth(props, month, year, selectedDate)` and `renderYear(props, year, selectedDate)` to customize the output of each rendering method.
|
122 |
|
123 | ```js
|
124 | var MyDTPicker = React.createClass({
|
125 | render: function(){
|
126 | return <Datetime
|
127 | renderDay={ this.renderDay }
|
128 | renderMonth={ this.renderMonth }
|
129 | renderYear={ this.renderYear }
|
130 | />;
|
131 | },
|
132 | renderDay: function( props, currentDate, selectedDate ){
|
133 | return <td {...props}>{ '0' + currentDate.date() }</td>;
|
134 | },
|
135 | renderMonth: function( props, month, year, selectedDate ){
|
136 | return <td {...props}>{ month }</td>;
|
137 | },
|
138 | renderYear: function( props, year, selectedDate ){
|
139 | return <td {...props}>{ year % 100 }</td>;
|
140 | }
|
141 | });
|
142 | ```
|
143 | [You can see a customized calendar here.](http://codepen.io/simeg/pen/YppLmO)
|
144 |
|
145 | #### Method Parameters
|
146 | * `props` is the object that the datepicker has calculated for this object. It is convenient to use this object as the `props` for your custom component, since it knows how to handle the click event and its `className` attribute is used by the default styles.
|
147 | * `selectedDate` and `currentDate` are [moment objects](http://momentjs.com) and can be used to change the output depending on the selected date, or the date for the current day.
|
148 | * `month` and `year` are the numeric representation of the current month and year to be displayed. Notice that the possible `month` values range from `0` to `11`.
|
149 |
|
150 | ## Specify Available Units
|
151 | You can filter out what you want the user to be able to pick by using `dateFormat` and `timeFormat`, e.g. to create a timepicker, yearpicker etc.
|
152 |
|
153 | In this example the component is being used as a *timepicker* and can *only be used for selecting a time*.
|
154 | ```js
|
155 | <Datetime dateFormat={false} />
|
156 | ```
|
157 | [Working example of a timepicker here.](http://codepen.io/simeg/pen/mRQBrp)
|
158 |
|
159 | In this example you can *only select a year and month*.
|
160 | ```js
|
161 | <Datetime dateFormat="YYYY-MM" timeFormat={false} />
|
162 | ```
|
163 | [Working example of only selecting year and month here.](http://codepen.io/simeg/pen/apQLdd)
|
164 |
|
165 | ## Selectable Dates
|
166 | It is possible to disable dates in the calendar if the user are not allowed to select them, e.g. dates in the past. This is done using the prop `isValidDate`, which admits a function in the form `function(currentDate, selectedDate)` where both arguments are [moment objects](http://momentjs.com). The function shall return `true` for selectable dates, and `false` for disabled ones.
|
167 |
|
168 | In the example below are *all dates before today* disabled.
|
169 |
|
170 | ```js
|
171 | // Let's use the static moment reference in the Datetime component
|
172 | var yesterday = Datetime.moment().subtract( 1, 'day' );
|
173 | var valid = function( current ){
|
174 | return current.isAfter( yesterday );
|
175 | };
|
176 | <Datetime isValidDate={ valid } />
|
177 | ```
|
178 | [Working example of disabled days here.](http://codepen.io/simeg/pen/XNNYJg)
|
179 |
|
180 | It's also possible to disable *the weekends*, as shown in the example below.
|
181 | ```js
|
182 | var valid = function( current ){
|
183 | return current.day() !== 0 && current.day() !== 6;
|
184 | };
|
185 | <Datetime isValidDate={ valid } />
|
186 | ```
|
187 | [Working example of disabled weekends here.](http://codepen.io/simeg/pen/jVVKWq)
|
188 |
|
189 | ## Usage with TypeScript
|
190 |
|
191 | This project includes typings for TypeScript versions 1.8 and 2.0. Additional typings are not
|
192 | required.
|
193 |
|
194 | Typings for 1.8 are found in `react-datetime.d.ts` and typings for 2.0 are found in `typings/index.d.ts`.
|
195 |
|
196 | ```js
|
197 | import * as Datetime from 'react-datetime';
|
198 |
|
199 | class MyDTPicker extends React.Component<MyDTPickerProps, MyDTPickerState> {
|
200 | render() JSX.Element {
|
201 | return <Datetime />;
|
202 | }
|
203 | }
|
204 | ```
|
205 |
|
206 | ## Contributions
|
207 | For information about how to contribute, see the [CONTRIBUTING](.github/CONTRIBUTING.md) file.
|
208 |
|
209 | ## Development
|
210 | ```sh
|
211 | npm run dev
|
212 | ```
|
213 | This will start a local `webpack-dev-server` based on `example/example.js` where most development can be done.
|
214 |
|
215 | If you want to develop using the component inside a React application, we recommend that you use [react-datetime-playground](https://github.com/arqex/react-datetime-playground).
|
216 |
|
217 | ### [Changelog](CHANGELOG.md)
|
218 |
|
219 | ### [MIT Licensed](LICENSE.md)
|