Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow optionsFromProps to be passed at init #2

Merged
merged 1 commit into from
Oct 17, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 19 additions & 14 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
'use strict';
/**
* react-bootstrap-daterangepicker.js
*
*
* A slightly modified version of bootstrap-daterangepicker.js for use in react and npm.
* Original copyright in: ./src/daterangepicker.js
*/
Expand All @@ -17,6 +17,7 @@ module.exports = React.createClass({
'timePicker','timePickerIncrement','timePicker12Hour','ranges','opens','buttonClasses',
'applyClass','cancelClass','format','separator','locale','singleDatePicker','parentEl'
],

makeEventHandler: function (eventType) {
return function (event, picker) {
if (typeof this.props.onEvent === 'function') {
Expand All @@ -27,32 +28,36 @@ module.exports = React.createClass({
}
}.bind(this);
},

getOptionsFromProps: function() {
var options, props = this.props;
this.options.forEach(function(option) {
if (props.hasOwnProperty(option)) {
options = options || {};
options[option] = props[option];
}
});
return options;
},

setOptionsFromProps: function () {
var currentOptions = {}, needToInit = false, $this = this;
if ($this.$picker) {
$this.options.forEach(function (option) {
if ($this.props.hasOwnProperty(option)) {
currentOptions[option] = $this.props[option];
needToInit = true;
}
});
if (needToInit) {
$this.$picker.data('daterangepicker').setOptions(currentOptions);
var currentOptions = this.getOptionsFromProps();
if (this.$picker) {
if (currentOptions) {
this.$picker.data('daterangepicker').setOptions(currentOptions);
}
}
},
componentDidMount: function () {
var $this = this;
$this.$picker = $(this.refs.picker.getDOMNode());
// initialize
$this.$picker.daterangepicker();
$this.$picker.daterangepicker(this.getOptionsFromProps());
// attach event listeners
['Show','Hide','Apply','Cancel'].forEach(function (event) {
var lcase = event.toLowerCase();
$this.$picker.on(lcase + '.daterangepicker', $this.makeEventHandler('on' + event));
});
// initial options from this.props
$this.setOptionsFromProps();
},
componentWillUnmount: function () {
this.$picker = null;
Expand Down