137 | );
138 | }
139 |
140 | }
141 |
142 | export default DatetimeRangePicker;
143 |
--------------------------------------------------------------------------------
/lib/index.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 |
3 | Object.defineProperty(exports, "__esModule", {
4 | value: true
5 | });
6 |
7 | var _typeof2 = require('babel-runtime/helpers/typeof');
8 |
9 | var _typeof3 = _interopRequireDefault(_typeof2);
10 |
11 | var _keys = require('babel-runtime/core-js/object/keys');
12 |
13 | var _keys2 = _interopRequireDefault(_keys);
14 |
15 | var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');
16 |
17 | var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);
18 |
19 | var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
20 |
21 | var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
22 |
23 | var _createClass2 = require('babel-runtime/helpers/createClass');
24 |
25 | var _createClass3 = _interopRequireDefault(_createClass2);
26 |
27 | var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
28 |
29 | var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
30 |
31 | var _inherits2 = require('babel-runtime/helpers/inherits');
32 |
33 | var _inherits3 = _interopRequireDefault(_inherits2);
34 |
35 | var _react = require('react');
36 |
37 | var _react2 = _interopRequireDefault(_react);
38 |
39 | var _propTypes = require('prop-types');
40 |
41 | var _propTypes2 = _interopRequireDefault(_propTypes);
42 |
43 | var _jquery = require('jquery');
44 |
45 | var _jquery2 = _interopRequireDefault(_jquery);
46 |
47 | require('bootstrap-daterangepicker');
48 |
49 | var _getOptions = require('./getOptions');
50 |
51 | var _getOptions2 = _interopRequireDefault(_getOptions);
52 |
53 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
54 |
55 | var events = ['Show', 'Hide', 'ShowCalendar', 'HideCalendar', 'Apply', 'Cancel'];
56 |
57 | var DatetimeRangePicker = function (_React$Component) {
58 | (0, _inherits3.default)(DatetimeRangePicker, _React$Component);
59 |
60 | function DatetimeRangePicker(props) {
61 | (0, _classCallCheck3.default)(this, DatetimeRangePicker);
62 |
63 | var _this = (0, _possibleConstructorReturn3.default)(this, (DatetimeRangePicker.__proto__ || (0, _getPrototypeOf2.default)(DatetimeRangePicker)).call(this, props));
64 |
65 | _this.state = {};
66 |
67 | _this.$picker = undefined;
68 | _this.picker = undefined;
69 | _this.options = (0, _getOptions2.default)();
70 |
71 | _this.handleCallback = _this.handleCallback.bind(_this);
72 | return _this;
73 | }
74 |
75 | (0, _createClass3.default)(DatetimeRangePicker, [{
76 | key: 'componentDidMount',
77 | value: function componentDidMount() {
78 | var _this2 = this;
79 |
80 | this.$picker = (0, _jquery2.default)(this.refs.picker);
81 | // initialize
82 | this.$picker.daterangepicker(this.getOptionsFromProps(), this.handleCallback);
83 | // attach event listeners
84 | events.forEach(function (event) {
85 | var cCase = event.charAt(0).toLowerCase() + event.slice(1);
86 | _this2.$picker.on(cCase + '.daterangepicker', _this2.makeEventHandler('on' + event));
87 | });
88 | }
89 | }, {
90 | key: 'componentWillUnmount',
91 | value: function componentWillUnmount() {
92 | this.getPicker().remove();
93 | }
94 | }, {
95 | key: 'setOptionsFromProps',
96 | value: function setOptionsFromProps() {
97 | var _this3 = this;
98 |
99 | var currentOptions = this.getOptionsFromProps();
100 | var keys = (0, _keys2.default)(currentOptions);
101 | if (this.$picker) {
102 | if (currentOptions) {
103 | keys.forEach(function (key) {
104 | _this3.applyOptionToPicker(key, currentOptions[key]);
105 | });
106 | }
107 | }
108 | }
109 | }, {
110 | key: 'getPicker',
111 | value: function getPicker() {
112 | return this.$picker && this.$picker.data('daterangepicker');
113 | }
114 | }, {
115 | key: 'getOptionsFromProps',
116 | value: function getOptionsFromProps() {
117 | var _this4 = this;
118 |
119 | var options = {};
120 | var props = this.props;
121 | var value = void 0;
122 | this.options.forEach(function (name) {
123 | if (props.hasOwnProperty(name)) {
124 | value = props[name];
125 |
126 | switch (name) {
127 | case 'startDate':
128 | case 'endDate':
129 | if (value) {
130 | options[name] = value;
131 | }
132 | break;
133 |
134 | case 'locale':
135 | if (value && (typeof value === 'undefined' ? 'undefined' : (0, _typeof3.default)(value)) === 'object') {
136 | var picker = _this4.getPicker();
137 | if (picker) {
138 | value = _jquery2.default.extend({}, value, picker.locale);
139 | }
140 | }
141 | options[name] = value;
142 | break;
143 |
144 | default:
145 | options[name] = value;
146 | }
147 | }
148 | });
149 | return options;
150 | }
151 | }, {
152 | key: 'applyOptionToPicker',
153 | value: function applyOptionToPicker(key, value) {
154 | if (this.$picker) {
155 | this.$picker.data('daterangepicker')[key] = value;
156 | }
157 | }
158 | }, {
159 | key: 'handleCallback',
160 | value: function handleCallback(start, end) {
161 | if (typeof this.props.callback === 'function') {
162 | this.props.callback(start, end);
163 | }
164 | }
165 | }, {
166 | key: 'makeEventHandler',
167 | value: function makeEventHandler(eventType) {
168 | var _this5 = this;
169 |
170 | return function (event, picker) {
171 | if (_this5.props.onEvent) {
172 | _this5.props.onEvent(event, picker);
173 | }
174 | if (typeof _this5.props[eventType] === 'function') {
175 | _this5.props[eventType](event, picker);
176 | }
177 | };
178 | }
179 | }, {
180 | key: 'render',
181 | value: function render() {
182 | this.setOptionsFromProps();
183 |
184 | return _react2.default.createElement(
185 | 'div',
186 | { ref: 'picker', style: this.props.style, className: this.props.className },
187 | this.props.children
188 | );
189 | }
190 | }]);
191 | return DatetimeRangePicker;
192 | }(_react2.default.Component);
193 |
194 | DatetimeRangePicker.propTypes = {
195 |
196 | startDate: _propTypes2.default.any,
197 | endDate: _propTypes2.default.any,
198 | children: _propTypes2.default.any,
199 | className: _propTypes2.default.string,
200 | style: _propTypes2.default.object,
201 |
202 | callback: _propTypes2.default.func,
203 | onEvent: _propTypes2.default.func,
204 | onShow: _propTypes2.default.func,
205 | onHide: _propTypes2.default.func,
206 | onShowCalendar: _propTypes2.default.func,
207 | onHideCalendar: _propTypes2.default.func,
208 | onApply: _propTypes2.default.func,
209 | onCancel: _propTypes2.default.func
210 | };
211 | DatetimeRangePicker.defaultProps = {};
212 | exports.default = DatetimeRangePicker;
213 | module.exports = exports['default'];
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # React Date&Time Range Picker for Bootstrap
2 |
3 | [![NPM version][npm-badge]][npm] [![Build Status][travis-ci-image]][travis-ci-url]
4 |
5 | [![Dependency Status][deps-badge]][deps]
6 | [![devDependency Status][dev-deps-badge]][dev-deps]
7 | [![peerDependency Status][peer-deps-badge]][peer-deps]
8 |
9 | 
10 |
11 | This date range picker component for Bootstrap creates a dropdown menu from which a user can select a range of dates.
12 |
13 | Base on [bootstrap-daterangepicker](https://github.com/dangrossman/bootstrap-daterangepicker)
14 |
15 | Online demo: http://luqin.github.io/react-bootstrap-datetimerangepicker
16 |
17 | ## Features
18 |
19 | * limiting the selectable date range
20 | * localizable strings and date formats
21 | * a single date picker mode
22 | * optional time picker (for e.g. making appointments or reservations)
23 | * styles that match the default Bootstrap 3 theme
24 |
25 |
26 |
27 | ## Upgrade guide
28 |
29 | **<2.0 to 2.x**
30 |
31 | Using official `bootstrap-daterangepicker`
32 |
33 | ```sh
34 | # <2.0
35 | npm install react-bootstrap-datetimerangepicker onefe-bootstrap-daterangepicker --save
36 |
37 | # 2.x
38 | npm install react-bootstrap-datetimerangepicker bootstrap-daterangepicker --save
39 | ```
40 |
41 | ```js
42 | // <2.0
43 | import 'bootstrap/dist/css/bootstrap.css';
44 | import 'onefe-bootstrap-daterangepicker/daterangepicker.css';
45 |
46 | // 2.x
47 | import 'bootstrap/dist/css/bootstrap.css';
48 | import 'bootstrap-daterangepicker/daterangepicker.css';
49 | ```
50 |
51 | ## Installation
52 |
53 | ```
54 | npm install react-bootstrap-datetimerangepicker bootstrap-daterangepicker --save
55 | ```
56 |
57 | ## Usage
58 |
59 | Date Range Picker relies on [Bootstrap](http://getbootstrap.com/), [jQuery](http://www.jquery.com/) and [Moment.js](http://momentjs.com/). Include the required stylesheet in your page:
60 |
61 | ```js
62 | import 'bootstrap/dist/css/bootstrap.css';
63 | import 'bootstrap-daterangepicker/daterangepicker.css';
64 | ```
65 |
66 | ```js
67 | import DatetimeRangePicker from 'react-bootstrap-datetimerangepicker';
68 |
69 |
74 |
75 |
76 |
77 |
87 |
92 |
93 | ```
94 |
95 | More examples: [Online demo](http://luqin.github.io/react-bootstrap-datetimerangepicker), [Source](https://github.com/luqin/react-bootstrap-datetimerangepicker/tree/master/examples)
96 |
97 | ## Documentation
98 |
99 | For in depth documentation, see the original
100 | [bootstrap-daterangepicker](https://github.com/dangrossman/bootstrap-daterangepicker) project page.
101 |
102 | ### Options
103 |
104 | - **startDate**: (Date object, moment object or string) The start of the initially selected date range
105 | - **endDate**: (Date object, moment object or string) The end of the initially selected date range
106 | - **minDate**: (Date object, moment object or string) The earliest date a user may select
107 | - **maxDate**: (Date object, moment object or string) The latest date a user may select
108 | - **dateLimit**: (object) The maximum span between the selected start and end dates. Can have any property you can add to a moment object (i.e. days, months)
109 | - **showDropdowns**: (boolean) Show year and month select boxes above calendars to jump to a specific month and year
110 | - **showWeekNumbers**: (boolean) Show week numbers at the start of each week on the calendars
111 | - **timePicker**: (boolean) Allow selection of dates with times, not just dates
112 | - **timePickerIncrement**: (number) Increment of the minutes selection list for times (i.e. 30 to allow only selection of times ending in 0 or 30)
113 | - **timePicker24Hour**: (boolean) Use 24-hour instead of 12-hour times, removing the AM/PM selection
114 | - **timePickerSeconds**: (boolean) Show seconds in the timePicker
115 | - **ranges**: (object) Set predefined date ranges the user can select from. Each key is the label for the range, and its value an array with two dates representing the bounds of the range
116 | - **opens**: (string: 'left'/'right'/'center') Whether the picker appears aligned to the left, to the right, or centered under the HTML element it's attached to
117 | - **drops**: (string: 'down' or 'up') Whether the picker appears below (default) or above the HTML element it's attached to
118 | - **buttonClasses**: (array) CSS class names that will be added to all buttons in the picker
119 | - **applyClass**: (string) CSS class string that will be added to the apply button
120 | - **cancelClass**: (string) CSS class string that will be added to the cancel button
121 | - **locale**: (object) Allows you to provide localized strings for buttons and labels, customize the date display format, and change the first day of week for the calendars
122 | - **singleDatePicker**: (boolean) Show only a single calendar to choose one date, instead of a range picker with two calendars; the start and end dates provided to your callback will be the same single date chosen
123 | - **autoApply**: (boolean) Hide the apply and cancel buttons, and automatically apply a new date range as soon as two dates or a predefined range is selected
124 | - **linkedCalendars**: (boolean) When enabled, the two calendars displayed will always be for two sequential months (i.e. January and February), and both will be advanced when clicking the left or right arrows above the calendars. When disabled, the two calendars can be individually advanced and display any month/year.
125 | - **parentEl**: (string) jQuery selector of the parent element that the date range picker will be added to, if not provided this will be 'body'
126 | - **isInvalidDate**: (function) A function that is passed each date in the two calendars before they are displayed, and may return true or false to indicate whether that date should be available for selection or not.
127 | - **autoUpdateInput**: (boolean) Indicates whether the date range picker should automatically update the value of an element it's attached to at initialization and when the selected dates change.
128 |
129 | ### Events
130 |
131 | - **onShow**: Triggered when the picker is shown
132 | - **onHide**: Triggered when the picker is hidden
133 | - **onHideCalendar**: Triggered when the calendar(s) are shown
134 | - **onApply**: Triggered when the calendar(s) are hidden
135 | - **onCancel**: Triggered when the apply button is clicked, or when a predefined range is clicked
136 | - **onEvent**: Triggered when the cancel button is clicked
137 |
138 | All of the events above should take a handler that is passed 2 arguments: **event** and **picker**
139 |
140 | #### Example event handler:
141 |
142 | ```js
143 | var SomeReactComponent = React.createClass({
144 | handleEvent: function (event, picker) {
145 | console.log(picker.startDate);
146 | },
147 | render: function () {
148 | return (
149 |
150 | );
151 | }
152 | });
153 | ```
154 |
155 | ## Browser support
156 |
157 | * Google Chrome
158 | * Firefox (2+)
159 | * IE (9+)
160 | * Opera (11.6+)
161 | * Safari (6+)
162 |
163 | ## Local Setup
164 |
165 | * Install the dependencies with `npm install`
166 | * Run the docs site in development mode with `npm start`. This will watch for file changes as you work. And auto refresh the page to see the updates.
167 |
168 | [npm-badge]: http://badge.fury.io/js/react-bootstrap-datetimerangepicker.svg
169 | [npm]: https://www.npmjs.com/package/react-bootstrap-datetimerangepicker
170 |
171 | [deps-badge]: https://david-dm.org/luqin/react-bootstrap-datetimerangepicker.svg
172 | [deps]: https://david-dm.org/luqin/react-bootstrap-datetimerangepicker
173 |
174 | [dev-deps-badge]: https://david-dm.org/luqin/react-bootstrap-datetimerangepicker/dev-status.svg
175 | [dev-deps]: https://david-dm.org/luqin/react-bootstrap-datetimerangepicker#info=devDependencies
176 |
177 | [peer-deps-badge]: https://david-dm.org/luqin/react-bootstrap-datetimerangepicker/peer-status.svg
178 | [peer-deps]: https://david-dm.org/luqin/react-bootstrap-datetimerangepicker#info=peerDependencies
179 |
180 | [travis-ci-image]: https://travis-ci.org/luqin/react-bootstrap-datetimerangepicker.svg
181 | [travis-ci-url]: https://travis-ci.org/luqin/react-bootstrap-datetimerangepicker
182 |
--------------------------------------------------------------------------------