Skip to content

Commit d66837f

Browse files
author
Dzmitry Verasau
committed
2 parents 74531a7 + d4c4095 commit d66837f

File tree

5 files changed

+73
-26
lines changed

5 files changed

+73
-26
lines changed

css/bootstrap-datetimepicker.css

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Datetimepicker for Bootstrap v3
33
* https://github.com/Eonasdan/bootstrap-datetimepicker/
44
*/
5-
.bootstrap-datetimepicker-widget {
5+
.bootstrap-datetimepicker-widget {
66
top: 0;
77
left: 0;
88
width: 250px;
@@ -225,6 +225,14 @@
225225
position: relative;
226226
}
227227

228+
.bootstrap-datetimepicker-wrap .input-group-addon {
229+
position: absolute;
230+
right: 5px;
231+
top: 4px;
232+
bottom: 4px;
233+
opacity: 0.5;
234+
cursor: pointer;
235+
}
228236
.bootstrap-datetimepicker-wrap.is-invalid ~ .invalid-feedback{
229237
display: block;
230238
}
@@ -275,4 +283,21 @@
275283

276284
.bootstrap-datetimepicker-widget .btn-today{
277285
padding: 3px 0;
286+
}
287+
.bootstrap-datetimepicker-widget.bottom {
288+
top: 100%;
289+
bottom: unset;
290+
}
291+
292+
.bootstrap-datetimepicker-widget.bottom.dropdown-menu {
293+
margin-top: 6px;
294+
}
295+
296+
.bootstrap-datetimepicker-widget.top {
297+
top: unset;
298+
bottom: 100%;
299+
}
300+
301+
.bootstrap-datetimepicker-widget.top.dropdown-menu {
302+
margin-bottom: 6px;
278303
}

lib/DateTimeField.js

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -355,27 +355,38 @@ var DateTimeField = (function (_Component) {
355355
};
356356

357357
this.calculatePosition = function (options) {
358-
var classes = undefined,
359-
styles = undefined;
358+
var classes = {};
359+
var styles = {
360+
display: 'block',
361+
position: 'absolute'
362+
};
360363

361-
classes = {};
362364
if (options) {
363365
classes['months'] = options.monthsDisplayed;
364366
classes['years'] = options.yearsDisplayed;
365367
classes['days'] = options.daysDisplayed;
366368
classes['time'] = options.timeDisplayed;
367369
}
368-
if (_this.props.direction === 'up' || _this.props.direction === 'auto') {
370+
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;
376+
377+
var placement = _this.props.direction === 'up' || _this.props.direction === 'auto' ? 'top' : 'bottom';
378+
var top = placement === 'top' && fitTop || placement === 'bottom' && !fitBottom;
379+
var bottom = placement === 'bottom' && fitBottom || placement === 'top' && !fitTop;
380+
381+
if (top) {
369382
classes.top = true;
370383
classes.bottom = false;
371-
} else {
384+
}
385+
if (bottom) {
372386
classes.top = false;
373387
classes.bottom = true;
374388
}
375-
styles = {
376-
display: 'block',
377-
position: 'absolute'
378-
};
389+
379390
return _this.setState({
380391
widgetStyle: styles,
381392
widgetClasses: classes
@@ -538,8 +549,8 @@ var DateTimeField = (function (_Component) {
538549
minDate: this.props.minDate,
539550
mode: this.props.mode,
540551
unlimited: this.props.unlimited,
541-
ref: function (el) {
542-
_this2.widget = el;
552+
widgetRef: function (el) {
553+
return _this2.widget = el;
543554
},
544555
selectedDate: this.state.selectedDate,
545556
setSelectedMonth: this.setSelectedMonth,

lib/DateTimePicker.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ var DateTimePicker = (function (_Component) {
158158

159159
return _react2['default'].createElement(
160160
'div',
161-
{ className: widgetClass, style: this.props.widgetStyle },
161+
{ ref: this.props.widgetRef, className: widgetClass, style: this.props.widgetStyle },
162162
_react2['default'].createElement(
163163
'ul',
164164
{ className: 'list-unstyled' },
@@ -203,6 +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,
206207
setSelectedHour: _propTypes2['default'].func,
207208
setSelectedMinute: _propTypes2['default'].func,
208209
setToday: _propTypes2['default'].func,

src/DateTimeField.js

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -520,27 +520,38 @@ export default class DateTimeField extends Component {
520520
};
521521

522522
calculatePosition = options => {
523-
let classes,
524-
styles;
523+
let classes = {};
524+
let styles = {
525+
display: 'block',
526+
position: 'absolute'
527+
};
525528

526-
classes = {};
527529
if (options) {
528530
classes['months'] = options.monthsDisplayed;
529531
classes['years'] = options.yearsDisplayed;
530532
classes['days'] = options.daysDisplayed;
531533
classes['time'] = options.timeDisplayed;
532534
}
533-
if (this.props.direction === 'up' || this.props.direction === 'auto') {
535+
536+
const wrapRect = this.wrapper.getBoundingClientRect();
537+
const widgetRect = this.widget.getBoundingClientRect();
538+
const widgetHeight = widgetRect.height;
539+
const fitTop = wrapRect.top - widgetHeight >= 0;
540+
const fitBottom = document.documentElement.clientHeight > wrapRect.bottom + widgetHeight;
541+
542+
const placement = this.props.direction === 'up' || this.props.direction === 'auto' ? 'top' : 'bottom';
543+
const top = placement === 'top' && fitTop || placement === 'bottom' && !fitBottom;
544+
const bottom = placement === 'bottom' && fitBottom || placement === 'top' && !fitTop;
545+
546+
if (top) {
534547
classes.top = true;
535548
classes.bottom = false;
536-
} else {
549+
}
550+
if (bottom) {
537551
classes.top = false;
538552
classes.bottom = true;
539553
}
540-
styles = {
541-
display: 'block',
542-
position: 'absolute',
543-
};
554+
544555
return this.setState({
545556
widgetStyle: styles,
546557
widgetClasses: classes,
@@ -633,9 +644,7 @@ export default class DateTimeField extends Component {
633644
minDate={this.props.minDate}
634645
mode={this.props.mode}
635646
unlimited={this.props.unlimited}
636-
ref={el => {
637-
this.widget = el;
638-
}}
647+
widgetRef={el => this.widget = el}
639648
selectedDate={this.state.selectedDate}
640649
setSelectedMonth={this.setSelectedMonth}
641650
setSelectedDate={this.setSelectedDate}

src/DateTimePicker.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export default class DateTimePicker extends Component {
4444
widgetClasses: PropTypes.object,
4545
widgetStyle: PropTypes.object,
4646
togglePicker: PropTypes.func,
47+
widgetRef: PropTypes.func,
4748
setSelectedHour: PropTypes.func,
4849
setSelectedMinute: PropTypes.func,
4950
setToday: PropTypes.func,
@@ -163,7 +164,7 @@ export default class DateTimePicker extends Component {
163164
if (this.props.disabled) return ('');
164165

165166
return (
166-
<div className={widgetClass} style={this.props.widgetStyle}>
167+
<div ref={this.props.widgetRef} className={widgetClass} style={this.props.widgetStyle}>
167168
<ul className="list-unstyled">
168169
{this.renderDatePicker()}
169170

0 commit comments

Comments
 (0)