Skip to content

Commit 70bac18

Browse files
committed
changing the api and updating examples
all the date range picker options now go in the `initialSettings` prop
1 parent 66622f9 commit 70bac18

24 files changed

+417
-546
lines changed

README.md

Lines changed: 57 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class MyComponent {
5757
For in depth documentation, see the original
5858
[bootstrap-daterangepicker](https://github.com/dangrossman/bootstrap-daterangepicker) project page.
5959

60-
You can pass all the same props as the original plugin:
60+
You can pass all the settings from the original plugin to the `initialSettings` prop:
6161

6262
- **<input>, alwaysShowCalendars, applyButtonClasses, applyClass,
6363
autoApply, autoUpdateInput, buttonClasses, cancelButtonClasses, cancelClass,
@@ -67,17 +67,57 @@ You can pass all the same props as the original plugin:
6767
showWeekNumbers, singleDatePicker, startDate, template, timePicker,
6868
timePicker24Hour, timePickerIncrement, timePickerSeconds**
6969

70-
You can listen to the following 7 events:
70+
You can listen to the following 8 events:
7171

72-
- **onShow**: thrown when the widget is shown
73-
- **onHide**: thrown when the widget is hidden
74-
- **onShowCalendar**: thrown when the calendar is shown
75-
- **onHideCalendar**: thrown when the calendar is hidden
76-
- **onApply**: thrown when the apply button is clicked
77-
- **onCancel**: thrown when the cancel button is clicked
78-
- **onEvent**: thrown when any of the 4 events above are triggered
72+
- **onShow**: `callback(event, picker)` thrown when the widget is shown
73+
- **onHide**: `callback(event, picker)` thrown when the widget is hidden
74+
- **onShowCalendar**: `callback(event, picker)` thrown when the calendar is shown
75+
- **onHideCalendar**: `callback(event, picker)` thrown when the calendar is hidden
76+
- **onApply**: `callback(event, picker)` thrown when the apply button is clicked
77+
- **onCancel**: `callback(event, picker)` thrown when the cancel button is clicked
78+
- **onEvent**: `callback(event, picker)` thrown when any of the 6 events above are triggered
79+
- **onCallback**: `callback(start, end, label)` thrown when the start/end dates change
7980

80-
All 7 of the events above should take a handler that is passed 2 arguments: **event** and **picker**
81+
You MUST pass a single child element to the `<DateRangePicker />` component.
82+
83+
NOTE: This component should be used as an [Uncontrolled Component](https://reactjs.org/docs/uncontrolled-components.html). If you try
84+
to control the value of your child `<input />`, the you will probably encounter issues.
85+
86+
There are 2 methods from the upstream lib that can be called: `setStartDate` and `setEndDate`, but you need to use refs when doing so.
87+
Please view the storybook for an example of this.
88+
89+
### Examples
90+
91+
For more usage examples, please view the storybook:
92+
https://projects.skratchdot.com/react-bootstrap-daterangepicker/
93+
94+
#### Simple button example
95+
96+
```javascript
97+
<DateRangePicker>
98+
<button type="button" class="btn btn-primary">
99+
click to open
100+
</button>
101+
</DateRangePicker>
102+
```
103+
104+
#### Simple input example
105+
106+
```javascript
107+
<DateRangePicker>
108+
<input type="text" class="form-control" />
109+
</DateRangePicker>
110+
```
111+
112+
#### Initialize with a startDate and endDate
113+
114+
```javascript
115+
<DateRangePicker
116+
initialSettings={{ startDate: '01/01/2020', endDate: '01/15/2020' }}
117+
>
118+
<input type="text" class="form-control" />
119+
</DateRangePicker>
120+
```
81121

82122
#### Example event handler:
83123

@@ -86,30 +126,18 @@ class SomeReactComponent extends React.Component {
86126
handleEvent(event, picker) {
87127
console.log(picker.startDate);
88128
}
129+
handleCallback(start, end, label) {
130+
console.log(start, end, label);
131+
}
89132
render() {
90-
return <DateRangePicker onEvent={this.handleEvent} />;
133+
return (
134+
<DateRangePicker onEvent={this.handleEvent} onCallback={this.handleCallback}>
135+
<input />
136+
</DateRangePicker>;
91137
}
92138
}
93139
```
94140
95-
There are 2 additional props you can pass, that are not part of the wrapped
96-
[bootstrap-daterangepicker](https://github.com/dangrossman/bootstrap-daterangepicker) project.
97-
Every `<DateRangePicker />` element emits a div element for the wrapper project to initialize itself against.
98-
99-
The emitted div looks like this by default:
100-
101-
```html
102-
<div
103-
class="react-bootstrap-daterangepicker-container"
104-
style="display:inline-block"
105-
></div>
106-
```
107-
108-
The 2 props you can pass to modify this behavior are:
109-
110-
- **containerStyles [object]**: the styles of the container `<div />` (default: `{ display: 'inline-block' }`)
111-
- **containerClass [string]**: the class of the container `<div />` (default: `'react-bootstrap-daterangepicker-container'`)
112-
113141
## Release Notes
114142
115143
Release notes can be found in the

dist/bundle.js

Lines changed: 71 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,6 @@ function _createSuper(Derived) {
111111
};
112112
}
113113

114-
/* generated by scripts/build-options.js */
115-
116-
var getOptions = (function () {
117-
return ['<input>', 'alwaysShowCalendars', 'applyButtonClasses', 'applyClass', 'autoApply', 'autoUpdateInput', 'buttonClasses', 'cancelButtonClasses', 'cancelClass', 'dateLimit', 'drops', 'endDate', 'isCustomDate', 'isInvalidDate', 'linkedCalendars', 'locale', 'maxDate', 'maxSpan', 'maxYear', 'minDate', 'minYear', 'moment', 'opens', 'parentEl', 'ranges', 'showCustomRangeLabel', 'showDropdowns', 'showISOWeekNumbers', 'showWeekNumbers', 'singleDatePicker', 'startDate', 'template', 'timePicker', 'timePicker24Hour', 'timePickerIncrement', 'timePickerSeconds'];
118-
});
119-
120114
var DateRangePicker = /*#__PURE__*/function (_Component) {
121115
_inherits(DateRangePicker, _Component);
122116

@@ -128,8 +122,8 @@ var DateRangePicker = /*#__PURE__*/function (_Component) {
128122
_classCallCheck(this, DateRangePicker);
129123

130124
_this = _super.call(this, props);
125+
_this.ref = null;
131126
_this.$picker = null;
132-
_this.options = getOptions();
133127
return _this;
134128
}
135129

@@ -138,35 +132,32 @@ var DateRangePicker = /*#__PURE__*/function (_Component) {
138132
value: function componentDidMount() {
139133
var _this2 = this;
140134

141-
// initialize
142-
this.$picker.daterangepicker(this.getOptionsFromProps()); // attach event listeners
135+
// initialize daterangepicker
136+
this.$picker = $(this.ref);
137+
this.$picker.daterangepicker(this.props.initialSettings, this.handleCallback.bind(this)); // attach event listeners
143138

144139
['Show', 'Hide', 'ShowCalendar', 'HideCalendar', 'Apply', 'Cancel'].forEach(function (event) {
145140
var lcase = event.toLowerCase();
146141

147142
_this2.$picker.on(lcase + '.daterangepicker', _this2.makeEventHandler('on' + event));
148143
});
149144
}
150-
}, {
151-
key: "UNSAFE_componentWillReceiveProps",
152-
value: function UNSAFE_componentWillReceiveProps(nextProps) {
153-
var currentOptions = this.getOptionsFromProps();
154-
var nextOptions = this.getOptionsFromProps(nextProps);
155-
var changedOptions = {};
156-
this.options.forEach(function (option) {
157-
if (currentOptions[option] !== nextOptions[option]) {
158-
changedOptions[option] = nextOptions[option];
159-
}
160-
});
161-
this.setOptionsFromProps(changedOptions);
162-
}
163145
}, {
164146
key: "componentWillUnmount",
165147
value: function componentWillUnmount() {
166-
if (this.$picker && this.$picker.data('daterangepicker')) {
148+
if (this.$picker && this.$picker.data && this.$picker.data('daterangepicker')) {
167149
this.$picker.data('daterangepicker').remove();
168150
}
169151
}
152+
}, {
153+
key: "handleCallback",
154+
value: function handleCallback() {
155+
if (typeof this.props.onCallback === 'function') {
156+
var _this$props;
157+
158+
(_this$props = this.props).onCallback.apply(_this$props, arguments);
159+
}
160+
}
170161
}, {
171162
key: "makeEventHandler",
172163
value: function makeEventHandler(eventType) {
@@ -184,102 +175,82 @@ var DateRangePicker = /*#__PURE__*/function (_Component) {
184175
};
185176
}
186177
}, {
187-
key: "getOptionsFromProps",
188-
value: function getOptionsFromProps(props) {
189-
var options;
190-
props = props || this.props;
191-
this.options.forEach(function (option) {
192-
if (Object.prototype.hasOwnProperty.call(props, option)) {
193-
options = options || {};
194-
options[option] = props[option];
195-
}
196-
});
197-
return options || {};
178+
key: "setStartDate",
179+
value: function setStartDate(dateOrString) {
180+
this.$picker.data('daterangepicker').setStartDate(dateOrString);
198181
}
199182
}, {
200-
key: "setOptionsFromProps",
201-
value: function setOptionsFromProps(currentOptions) {
202-
var _this4 = this;
203-
204-
var keys = Object.keys(currentOptions);
205-
keys.forEach(function (key) {
206-
if (key === 'startDate') {
207-
_this4.$picker.data('daterangepicker').setStartDate(currentOptions[key]);
208-
} else if (key === 'endDate') {
209-
_this4.$picker.data('daterangepicker').setEndDate(currentOptions[key]);
210-
} else {
211-
_this4.$picker.data('daterangepicker')[key] = currentOptions[key];
212-
}
213-
});
183+
key: "setEndDate",
184+
value: function setEndDate(dateOrString) {
185+
this.$picker.data('daterangepicker').setEndDate(dateOrString);
214186
}
215187
}, {
216188
key: "render",
217189
value: function render() {
218-
var _this5 = this;
219-
220-
var _this$props = this.props,
221-
children = _this$props.children,
222-
containerStyles = _this$props.containerStyles,
223-
containerClass = _this$props.containerClass;
224-
return /*#__PURE__*/React__default.createElement("div", {
225-
ref: function ref(picker) {
226-
_this5.$picker = $(picker);
227-
},
228-
className: containerClass,
229-
style: containerStyles
230-
}, children);
190+
var _this4 = this;
191+
192+
var childElement = React__default.Children.only(this.props.children);
193+
return /*#__PURE__*/React__default.cloneElement(childElement, {
194+
ref: function ref(el) {
195+
return _this4.ref = el;
196+
}
197+
});
231198
}
232199
}]);
233200

234201
return DateRangePicker;
235202
}(React.Component);
236-
DateRangePicker.defaultProps = {
237-
containerClass: 'react-bootstrap-daterangepicker-container',
238-
containerStyles: {
239-
display: 'inline-block'
240-
}
241-
};
242203
DateRangePicker.propTypes = {
243-
'<input>': PropTypes.any,
244-
alwaysShowCalendars: PropTypes.bool,
245-
applyClass: PropTypes.string,
246-
autoApply: PropTypes.bool,
247-
autoUpdateInput: PropTypes.bool,
248-
buttonClasses: PropTypes.array,
249-
cancelClass: PropTypes.string,
204+
initialSettings: PropTypes.shape({
205+
'<input>': PropTypes.any,
206+
alwaysShowCalendars: PropTypes.bool,
207+
applyButtonClasses: PropTypes.array,
208+
applyClass: PropTypes.string,
209+
autoApply: PropTypes.bool,
210+
autoUpdateInput: PropTypes.bool,
211+
buttonClasses: PropTypes.array,
212+
cancelButtonClasses: PropTypes.array,
213+
cancelClass: PropTypes.string,
214+
dateLimit: PropTypes.object,
215+
drops: PropTypes.oneOf(['down', 'up']),
216+
endDate: PropTypes.oneOfType([PropTypes.object, PropTypes.string]),
217+
isCustomDate: PropTypes.func,
218+
isInvalidDate: PropTypes.func,
219+
linkedCalendars: PropTypes.bool,
220+
locale: PropTypes.object,
221+
maxDate: PropTypes.oneOfType([PropTypes.object, PropTypes.string]),
222+
maxSpan: PropTypes.any,
223+
maxYear: PropTypes.any,
224+
minDate: PropTypes.oneOfType([PropTypes.object, PropTypes.string]),
225+
minYear: PropTypes.any,
226+
moment: PropTypes.any,
227+
opens: PropTypes.oneOf(['left', 'right', 'center']),
228+
parentEl: PropTypes.any,
229+
ranges: PropTypes.object,
230+
showCustomRangeLabel: PropTypes.bool,
231+
showDropdowns: PropTypes.bool,
232+
showISOWeekNumbers: PropTypes.bool,
233+
showWeekNumbers: PropTypes.bool,
234+
singleDatePicker: PropTypes.bool,
235+
startDate: PropTypes.oneOfType([PropTypes.object, PropTypes.string]),
236+
template: PropTypes.any,
237+
timePicker: PropTypes.bool,
238+
timePicker24Hour: PropTypes.bool,
239+
timePickerIncrement: PropTypes.number,
240+
timePickerSeconds: PropTypes.bool
241+
}),
242+
// you must pass a single element here
250243
children: PropTypes.node.isRequired,
251-
containerClass: PropTypes.string,
252-
containerStyles: PropTypes.object,
253-
dateLimit: PropTypes.object,
254-
drops: PropTypes.oneOf(['down', 'up']),
255-
endDate: PropTypes.oneOfType([PropTypes.object, PropTypes.string]),
256-
isCustomDate: PropTypes.func,
257-
isInvalidDate: PropTypes.func,
258-
linkedCalendars: PropTypes.bool,
259-
locale: PropTypes.object,
260-
maxDate: PropTypes.oneOfType([PropTypes.object, PropTypes.string]),
261-
minDate: PropTypes.oneOfType([PropTypes.object, PropTypes.string]),
244+
// events supported by the upstream lib
262245
onApply: PropTypes.func,
263246
onCancel: PropTypes.func,
264-
onEvent: PropTypes.func,
265247
onHide: PropTypes.func,
266248
onHideCalendar: PropTypes.func,
267249
onShow: PropTypes.func,
268250
onShowCalendar: PropTypes.func,
269-
opens: PropTypes.oneOf(['left', 'right', 'center']),
270-
parentEl: PropTypes.any,
271-
ranges: PropTypes.object,
272-
showCustomRangeLabel: PropTypes.bool,
273-
showDropdowns: PropTypes.bool,
274-
showISOWeekNumbers: PropTypes.bool,
275-
showWeekNumbers: PropTypes.bool,
276-
singleDatePicker: PropTypes.bool,
277-
startDate: PropTypes.oneOfType([PropTypes.object, PropTypes.string]),
278-
template: PropTypes.any,
279-
timePicker: PropTypes.bool,
280-
timePickerIncrement: PropTypes.number,
281-
timePicker24Hour: PropTypes.bool,
282-
timePickerSeconds: PropTypes.bool
251+
// custom events in this lib
252+
onEvent: PropTypes.func,
253+
onCallback: PropTypes.func
283254
};
284255

285256
exports.DateRangePicker = DateRangePicker;

docs/iframe.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,4 @@
7171
}</script><style>#root[hidden],
7272
#docs-root[hidden] {
7373
display: none !important;
74-
}</style></head><body><div class="sb-nopreview sb-wrapper"><div class="sb-nopreview_main"><h1 class="sb-nopreview_heading sb-heading">No Preview</h1><p>Sorry, but you either have no stories or none are selected somehow.</p><ul><li>Please check the Storybook config.</li><li>Try reloading the page.</li></ul><p>If the problem persists, check the browser console, or the terminal you've run Storybook from.</p></div></div><div class="sb-errordisplay sb-wrapper"><pre id="error-message" class="sb-heading"></pre><pre class="sb-errordisplay_code"><code id="error-stack"></code></pre></div><div id="root"></div><div id="docs-root"></div><script src="runtime~main.8269748cbacfd65a79c3.bundle.js"></script><script src="vendors~main.8269748cbacfd65a79c3.bundle.js"></script><script src="main.8269748cbacfd65a79c3.bundle.js"></script></body></html>
74+
}</style></head><body><div class="sb-nopreview sb-wrapper"><div class="sb-nopreview_main"><h1 class="sb-nopreview_heading sb-heading">No Preview</h1><p>Sorry, but you either have no stories or none are selected somehow.</p><ul><li>Please check the Storybook config.</li><li>Try reloading the page.</li></ul><p>If the problem persists, check the browser console, or the terminal you've run Storybook from.</p></div></div><div class="sb-errordisplay sb-wrapper"><pre id="error-message" class="sb-heading"></pre><pre class="sb-errordisplay_code"><code id="error-stack"></code></pre></div><div id="root"></div><div id="docs-root"></div><script src="runtime~main.df7d7ea10ee787c1a6fb.bundle.js"></script><script src="vendors~main.df7d7ea10ee787c1a6fb.bundle.js"></script><script src="main.df7d7ea10ee787c1a6fb.bundle.js"></script></body></html>

docs/main.8269748cbacfd65a79c3.bundle.js

Lines changed: 0 additions & 2 deletions
This file was deleted.

docs/main.8269748cbacfd65a79c3.bundle.js.map

Lines changed: 0 additions & 1 deletion
This file was deleted.

docs/main.df7d7ea10ee787c1a6fb.bundle.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/main.df7d7ea10ee787c1a6fb.bundle.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/runtime~main.8269748cbacfd65a79c3.bundle.js.map

Lines changed: 0 additions & 1 deletion
This file was deleted.

docs/runtime~main.8269748cbacfd65a79c3.bundle.js renamed to docs/runtime~main.df7d7ea10ee787c1a6fb.bundle.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/runtime~main.df7d7ea10ee787c1a6fb.bundle.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/vendors~main.8269748cbacfd65a79c3.bundle.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

docs/vendors~main.8269748cbacfd65a79c3.bundle.js.map

Lines changed: 0 additions & 1 deletion
This file was deleted.

docs/vendors~main.df7d7ea10ee787c1a6fb.bundle.js

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/vendors~main.df7d7ea10ee787c1a6fb.bundle.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-bootstrap-daterangepicker",
3-
"version": "5.0.1",
3+
"version": "6.0.0",
44
"description": "A date/time picker for react (using bootstrap). This is a react port of: https://github.com/dangrossman/bootstrap-daterangepicker",
55
"main": "./dist/bundle.js",
66
"files": [

0 commit comments

Comments
 (0)