Javier Marquez
2017-03-01 11612b608143c1607f33ff40c43c6f9db5e073ba
commit | author | age
1de42c 1 # react-datetime
4afe21 2
267a3a 3 [![Build Status](https://secure.travis-ci.org/YouCanBookMe/react-datetime.svg)](https://travis-ci.org/YouCanBookMe/react-datetime)
7d1067 4 [![npm version](https://badge.fury.io/js/react-datetime.svg)](http://badge.fury.io/js/react-datetime)
1de42c 5 [![npm](http://img.shields.io/npm/dm/react-datetime.svg)](https://npmjs.org/package/react-datetime)
889b6c 6
1de42c 7 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.
eba92b 8
18dc17 9 This project started as a fork of https://github.com/quri/react-bootstrap-datetimepicker but the code and the API has changed a lot.
795f65 10
1de42c 11 ## Usage
f13618 12
28160e 13 Install using npm:
8055e6 14 ```sh
70f4fb 15 npm install --save react-datetime
f13618 16 ```
LC 17
1de42c 18 [React.js](http://facebook.github.io/react/) and [Moment.js](http://momentjs.com/) are peer dependencies for react-datetime. 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.
f60b22 19
1de42c 20
SE 21 ```js
9fb8e8 22 require('react-datetime');
f13618 23
LC 24 ...
25
26 render: function() {
9fb8e8 27   return <Datetime />;
f13618 28 }
LC 29 ```
70f4fb 30 [See this example working](http://codepen.io/simeg/pen/mEmQmP).
f13618 31
8055e6 32 **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.**
70f4fb 33
1de42c 34 ## API
f13618 35
LC 36 | Name         | Type    | Default | Description |
37 | ------------ | ------- | ------- | ----------- |
1de42c 38 | **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. |
SE 39 | **defaultValue** | `Date` | `new Date()` | Represents the selected date for the component to use it as a [uncontrolled component](https://facebook.github.io/react/docs/forms.html#uncontrolled-components). This prop is parsed by Moment.js, so it is possible to use a date `string` or a `moment` object. |
8e5d74 40 | **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). |
SE 41 | **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). |
1de42c 42 | **input** | `boolean` | `true` | Whether to show an input field to edit the date manually. |
SE 43 | **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. |
44 | **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).
049c33 45 | **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.
fe4c5c 46 | **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). |
MV 47 | **onFocus** | `function` | empty function | Callback trigger for when the user opens the datepicker. |
48 | **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. |
1de42c 49 | **viewMode** | `string` or `number` | `'days'` | The default view to display when the picker is shown (`'years'`, `'months'`, `'days'`, `'time'`). |
SE 50 | **className** | `string` or `string array` | `''` | Extra class name for the outermost markup element. |
8055e6 51 | **inputProps** | `object` | `undefined` | Defines additional attributes for the input element of the component. For example: `placeholder`, `disabled`, `required`, `name` and `className` (`className` *sets* the class attribute for the input element). |
28160e 52 | **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).|
8e5d74 53 | **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 [appearance customization](#customize-the-appearance). |
SE 54 | **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 [appearance customization](#customize-the-appearance). |
55 | **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 [appearance customization](#customize-the-appearance). |
1de42c 56 | **strictParsing** | `boolean` | `false` | Whether to use Moment.js's [strict parsing](http://momentjs.com/docs/#/parsing/string-format/) when parsing input.
SE 57 | **closeOnSelect** | `boolean` | `false` | When `true`, once the day has been selected, the datepicker will be automatically closed.
58 | **closeOnTab** | `boolean` | `true` | When `true` and the input is focused, pressing the `tab` key will close the datepicker.
59 | **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`.
60 | **disableOnClickOutside** | `boolean` | `false` | When `true`, keep the datepicker open when click event is triggered outside of component. When `false`, close it.
f13618 61
c37f80 62 ## i18n
1de42c 63 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/).
c37f80 64
M 65 ```js
66 var moment = require('moment');
67 require('moment/locale/fr');
68 // Now react-datetime will be in french
69 ```
70
28160e 71 If there are multiple locales loaded, you can use the prop `locale` to define what language shall be used by the instance.
c37f80 72 ```js
182c2b 73 <Datetime locale="fr-ca" />
c37f80 74 <Datetime locale="de" />
M 75 ```
ccbc79 76 [Here you can see the i18n example working](http://codepen.io/simeg/pen/yVVjdJ).
c37f80 77
8055e6 78 ## Customize the Appearance
1de42c 79 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.
eba92b 80
M 81 ```js
82 var MyDTPicker = React.createClass({
83     render: function(){
84         return <Datetime
795f65 85             renderDay={ this.renderDay }
M 86             renderMonth={ this.renderMonth }
eba92b 87             renderYear={ this.renderYear }
M 88         />;
89     },
199ffb 90     renderDay: function( props, currentDate, selectedDate ){
M 91         return <td {...props}>{ '0' + currentDate.date() }</td>;
eba92b 92     },
8e5d74 93     renderMonth: function( props, month, year, selectedDate ){
eba92b 94         return <td {...props}>{ month }</td>;
M 95     },
199ffb 96     renderYear: function( props, year, selectedDate ){
M 97         return <td {...props}>{ year % 100 }</td>;
eba92b 98     }
M 99 });
100 ```
ccbc79 101 [You can see a customized calendar here.](http://codepen.io/simeg/pen/YppLmO)
eba92b 102
8055e6 103 #### Method Parameters
1de42c 104 * `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.
SE 105 * `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.
106 * `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`.
eba92b 107
8e5d74 108 ## Specify Available Units
SE 109 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.
110  
111 In this example the component is being used as a *timepicker* and can *only be used for selecting a time*.
112 ```js
113 <Datetime dateFormat={false} />
114 ```
115 [Working example of a timepicker here.](http://codepen.io/simeg/pen/mRQBrp)
116
117 In this example you can *only select a year and month*.
118 ```js
119 <Datetime dateFormat="YYYY-MM" timeFormat={false} />
120 ```
121 [Working example of only selecting year and month here.](http://codepen.io/simeg/pen/apQLdd)
122
8055e6 123 ## Selectable Dates
28160e 124 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.
baa9f1 125
1de42c 126 In the example below are *all dates before today* disabled.
SE 127
baa9f1 128 ```js
1de42c 129 // Let's use the static moment reference in the Datetime component
8e5d74 130 var yesterday = Datetime.moment().subtract( 1, 'day' );
baa9f1 131 var valid = function( current ){
199ffb 132     return current.isAfter( yesterday );
baa9f1 133 };
M 134 <Datetime isValidDate={ valid } />
135 ```
ccbc79 136 [Working example of disabled days here.](http://codepen.io/simeg/pen/XNNYJg)
baa9f1 137
1de42c 138 It's also possible to disable *the weekends*, as shown in the example below.
baa9f1 139 ```js
M 140 var valid = function( current ){
1de42c 141     return current.day() !== 0 && current.day() !== 6;
baa9f1 142 };
M 143 <Datetime isValidDate={ valid } />
144 ```
ccbc79 145 [Working example of disabled weekends here.](http://codepen.io/simeg/pen/jVVKWq)
baa9f1 146
0de4da 147 ## Usage with TypeScript
a20f58 148
0de4da 149 This project includes typings for TypeScript versions 1.8 and 2.0. Additional typings are not
a20f58 150 required.
AS 151
0de4da 152 Typings for 1.8 are found in `react-datetime.d.ts` and typings for 2.0 are found in `typings/index.d.ts`.
SE 153
a20f58 154 ```js
8055e6 155 import * as Datetime from 'react-datetime';
a20f58 156
AS 157 class MyDTPicker extends React.Component<MyDTPickerProps, MyDTPickerState> {
158     render() JSX.Element {
159         return <Datetime />;
160     }
161 }
162 ```
163
1de42c 164 ## Contributions
8055e6 165 * For information about how to contribute, see the [CONTRIBUTING](.github/CONTRIBUTING.md) file.
SE 166
167 ## Development
168 ```sh
169 npm run dev
170 ```
171 This will start a local `webpack-dev-server` based on `example/example.js` where most development can be done.
172
173 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).
9fb8e8 174
8abb28 175 ### [Changelog](CHANGELOG.md)
M 176
9fb8e8 177 ### [MIT Licensed](LICENSE)