Skip to content

Commit 96a64a9

Browse files
authored
Merge pull request 1stQuad#2 from daviidovich/master
Change widget positioning calculation;
2 parents 2fc61db + 805a169 commit 96a64a9

File tree

7 files changed

+16793
-2095
lines changed

7 files changed

+16793
-2095
lines changed

css/bootstrap-datetimepicker.css

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -284,20 +284,12 @@
284284
.bootstrap-datetimepicker-widget .btn-today{
285285
padding: 3px 0;
286286
}
287-
.bootstrap-datetimepicker-widget.bottom {
288-
top: 100%;
289-
bottom: unset;
290-
}
291287

292288
.bootstrap-datetimepicker-widget.bottom.dropdown-menu {
293289
margin-top: 6px;
294290
}
295291

296-
.bootstrap-datetimepicker-widget.top {
297-
top: unset;
298-
bottom: 100%;
299-
}
300-
301292
.bootstrap-datetimepicker-widget.top.dropdown-menu {
302293
margin-bottom: 6px;
294+
margin-top: 0;
303295
}

lib/DateTimeField.js

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ var DateTimeField = (function (_Component) {
358358
var classes = {};
359359
var styles = {
360360
display: 'block',
361-
position: 'absolute'
361+
position: 'fixed'
362362
};
363363

364364
if (options) {
@@ -368,11 +368,15 @@ var DateTimeField = (function (_Component) {
368368
classes['time'] = options.timeDisplayed;
369369
}
370370

371-
var wrapRect = _this.wrapper.getBoundingClientRect();
372-
var widgetRect = _this.widget.getBoundingClientRect();
373-
var widgetHeight = widgetRect.height;
374-
var fitTop = wrapRect.top - widgetHeight >= 0;
375-
var fitBottom = document.documentElement.clientHeight > wrapRect.bottom + widgetHeight;
371+
if (!_this.wrapperRef.current || !_this.widgetRef.current) {
372+
return;
373+
}
374+
var fieldRect = _this.wrapperRef.current.getBoundingClientRect();
375+
var widgetRect = _this.widgetRef.current.getBoundingClientRect();
376+
var arrowIndent = 6;
377+
var widgetHeight = widgetRect.height + arrowIndent;
378+
var fitTop = fieldRect.top - widgetHeight > 0;
379+
var fitBottom = fieldRect.bottom + widgetHeight < document.documentElement.clientHeight;
376380

377381
var placement = _this.props.direction === 'up' || _this.props.direction === 'auto' ? 'top' : 'bottom';
378382
var top = placement === 'top' && fitTop || placement === 'bottom' && !fitBottom;
@@ -381,10 +385,14 @@ var DateTimeField = (function (_Component) {
381385
if (top) {
382386
classes.top = true;
383387
classes.bottom = false;
388+
styles.top = fieldRect.top - widgetHeight + 'px';
389+
styles.left = fieldRect.left + 'px';
384390
}
385391
if (bottom) {
386392
classes.top = false;
387393
classes.bottom = true;
394+
styles.top = fieldRect.bottom + 'px';
395+
styles.left = fieldRect.left + 'px';
388396
}
389397

390398
return _this.setState({
@@ -460,6 +468,8 @@ var DateTimeField = (function (_Component) {
460468
dateTime = _moment2['default'].utc(props.dateTime);
461469
}
462470

471+
this.wrapperRef = _react2['default'].createRef();
472+
this.widgetRef = _react2['default'].createRef();
463473
this.state = {
464474
showDatePicker: props.mode !== _ConstantsJs2['default'].MODE_TIME && props.viewMode !== _ConstantsJs2['default'].MODE_TIME,
465475
showTimePicker: props.mode === _ConstantsJs2['default'].MODE_TIME || props.viewMode === _ConstantsJs2['default'].MODE_TIME,
@@ -533,10 +543,7 @@ var DateTimeField = (function (_Component) {
533543
}
534544
return _react2['default'].createElement(
535545
'div',
536-
{ className: pickerClass,
537-
ref: function (el) {
538-
_this2.wrapper = el;
539-
} },
546+
{ ref: this.wrapperRef, className: pickerClass },
540547
this.renderOverlay(),
541548
_react2['default'].createElement(_DateTimePickerJs2['default'], {
542549
addDecade: this.addDecade,
@@ -549,9 +556,7 @@ var DateTimeField = (function (_Component) {
549556
minDate: this.props.minDate,
550557
mode: this.props.mode,
551558
unlimited: this.props.unlimited,
552-
widgetRef: function (el) {
553-
return _this2.widget = el;
554-
},
559+
widgetRef: this.widgetRef,
555560
selectedDate: this.state.selectedDate,
556561
setSelectedMonth: this.setSelectedMonth,
557562
setSelectedDate: this.setSelectedDate,

lib/DateTimePicker.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ var DateTimePicker = (function (_Component) {
203203
widgetClasses: _propTypes2['default'].object,
204204
widgetStyle: _propTypes2['default'].object,
205205
togglePicker: _propTypes2['default'].func,
206-
widgetRef: _propTypes2['default'].func,
206+
widgetRef: _propTypes2['default'].object,
207207
setSelectedHour: _propTypes2['default'].func,
208208
setSelectedMinute: _propTypes2['default'].func,
209209
setToday: _propTypes2['default'].func,

0 commit comments

Comments
 (0)