@gigwalk/react-bootstrap-datetimepicker
Version:
A bootstrap datetime picker component for React.js
474 lines (417 loc) • 16.9 kB
JavaScript
"use strict";
var _get = require("babel-runtime/helpers/get")["default"];
var _inherits = require("babel-runtime/helpers/inherits")["default"];
var _createClass = require("babel-runtime/helpers/create-class")["default"];
var _classCallCheck = require("babel-runtime/helpers/class-call-check")["default"];
var _extends = require("babel-runtime/helpers/extends")["default"];
var _interopRequireDefault = require("babel-runtime/helpers/interop-require-default")["default"];
Object.defineProperty(exports, "__esModule", {
value: true
});
var _react = require("react");
var _react2 = _interopRequireDefault(_react);
var _moment = require("moment");
var _moment2 = _interopRequireDefault(_moment);
var _DateTimePickerJs = require("./DateTimePicker.js");
var _DateTimePickerJs2 = _interopRequireDefault(_DateTimePickerJs);
var _ConstantsJs = require("./Constants.js");
var _ConstantsJs2 = _interopRequireDefault(_ConstantsJs);
var DateTimeField = (function (_Component) {
_inherits(DateTimeField, _Component);
function DateTimeField() {
var _this = this;
_classCallCheck(this, DateTimeField);
_get(Object.getPrototypeOf(DateTimeField.prototype), "constructor", this).apply(this, arguments);
this.resolvePropsInputFormat = function () {
if (_this.props.inputFormat) {
return _this.props.inputFormat;
}
switch (_this.props.mode) {
case _ConstantsJs2["default"].MODE_TIME:
return "h:mm A";
case _ConstantsJs2["default"].MODE_DATE:
return "MM/DD/YY";
default:
return "MM/DD/YY h:mm A";
}
};
this.state = {
showDatePicker: this.props.mode !== _ConstantsJs2["default"].MODE_TIME,
showTimePicker: this.props.mode === _ConstantsJs2["default"].MODE_TIME,
inputFormat: this.resolvePropsInputFormat(),
buttonIcon: this.props.mode === _ConstantsJs2["default"].MODE_TIME ? "glyphicon-time" : "glyphicon-calendar",
widgetStyle: {
display: "block",
position: "absolute",
left: -9999,
zIndex: "9999 !important"
},
viewDate: (0, _moment2["default"])(this.props.dateTime, this.props.format, true).startOf("month"),
selectedDate: (0, _moment2["default"])(this.props.dateTime, this.props.format, true),
inputValue: typeof this.props.defaultText !== "undefined" ? this.props.defaultText : (0, _moment2["default"])(this.props.dateTime, this.props.format, true).format(this.resolvePropsInputFormat())
};
this.componentWillReceiveProps = function (nextProps) {
var state = {};
if (nextProps.inputFormat !== _this.props.inputFormat) {
state.inputFormat = nextProps.inputFormat;
state.inputValue = (0, _moment2["default"])(nextProps.dateTime, nextProps.format, true).format(nextProps.inputFormat);
}
if (nextProps.dateTime !== _this.props.dateTime && (0, _moment2["default"])(nextProps.dateTime, nextProps.format, true).isValid()) {
state.viewDate = (0, _moment2["default"])(nextProps.dateTime, nextProps.format, true).startOf("month");
state.selectedDate = (0, _moment2["default"])(nextProps.dateTime, nextProps.format, true);
state.inputValue = (0, _moment2["default"])(nextProps.dateTime, nextProps.format, true).format(nextProps.inputFormat ? nextProps.inputFormat : _this.state.inputFormat);
}
return _this.setState(state);
};
this.onChange = function (event) {
var value = event.target == null ? event : event.target.value;
if ((0, _moment2["default"])(value, _this.state.inputFormat, true).isValid()) {
_this.setState({
selectedDate: (0, _moment2["default"])(value, _this.state.inputFormat, true),
viewDate: (0, _moment2["default"])(value, _this.state.inputFormat, true).startOf("month")
});
}
return _this.setState({
inputValue: value
}, function () {
return this.props.onChange((0, _moment2["default"])(this.state.inputValue, this.state.inputFormat, true).format(this.props.format), value);
});
};
this.getValue = function () {
return (0, _moment2["default"])(_this.state.inputValue, _this.props.inputFormat, true).format(_this.props.format);
};
this.setSelectedDate = function (e) {
var target = e.target;
if (target.className && !target.className.match(/disabled/g)) {
var month = undefined;
if (target.className.indexOf("new") >= 0) month = _this.state.viewDate.month() + 1;else if (target.className.indexOf("old") >= 0) month = _this.state.viewDate.month() - 1;else month = _this.state.viewDate.month();
return _this.setState({
selectedDate: _this.state.viewDate.clone().month(month).date(parseInt(e.target.innerHTML)).hour(_this.state.selectedDate.hours()).minute(_this.state.selectedDate.minutes())
}, function () {
this.closePicker();
this.props.onChange(this.state.selectedDate.format(this.props.format));
return this.setState({
inputValue: this.state.selectedDate.format(this.state.inputFormat)
});
});
}
};
this.setSelectedHour = function (e) {
return _this.setState({
selectedDate: _this.state.selectedDate.clone().hour(parseInt(e.target.innerHTML)).minute(_this.state.selectedDate.minutes())
}, function () {
this.closePicker();
this.props.onChange(this.state.selectedDate.format(this.props.format));
return this.setState({
inputValue: this.state.selectedDate.format(this.state.inputFormat)
});
});
};
this.setSelectedMinute = function (e) {
return _this.setState({
selectedDate: _this.state.selectedDate.clone().hour(_this.state.selectedDate.hours()).minute(parseInt(e.target.innerHTML))
}, function () {
this.closePicker();
this.props.onChange(this.state.selectedDate.format(this.props.format));
return this.setState({
inputValue: this.state.selectedDate.format(this.state.inputFormat)
});
});
};
this.setViewMonth = function (month) {
return _this.setState({
viewDate: _this.state.viewDate.clone().month(month)
});
};
this.setViewYear = function (year) {
return _this.setState({
viewDate: _this.state.viewDate.clone().year(year)
});
};
this.addMinute = function () {
return _this.setState({
selectedDate: _this.state.selectedDate.clone().add(1, "minutes")
}, function () {
this.props.onChange(this.state.selectedDate.format(this.props.format));
return this.setState({
inputValue: this.state.selectedDate.format(this.resolvePropsInputFormat())
});
});
};
this.addHour = function () {
return _this.setState({
selectedDate: _this.state.selectedDate.clone().add(1, "hours")
}, function () {
this.props.onChange(this.state.selectedDate.format(this.props.format));
return this.setState({
inputValue: this.state.selectedDate.format(this.resolvePropsInputFormat())
});
});
};
this.addMonth = function () {
return _this.setState({
viewDate: _this.state.viewDate.add(1, "months")
});
};
this.addYear = function () {
return _this.setState({
viewDate: _this.state.viewDate.add(1, "years")
});
};
this.addDecade = function () {
return _this.setState({
viewDate: _this.state.viewDate.add(10, "years")
});
};
this.subtractMinute = function () {
return _this.setState({
selectedDate: _this.state.selectedDate.clone().subtract(1, "minutes")
}, function () {
_this.props.onChange(_this.state.selectedDate.format(_this.props.format));
return _this.setState({
inputValue: _this.state.selectedDate.format(_this.resolvePropsInputFormat())
});
});
};
this.subtractHour = function () {
return _this.setState({
selectedDate: _this.state.selectedDate.clone().subtract(1, "hours")
}, function () {
_this.props.onChange(_this.state.selectedDate.format(_this.props.format));
return _this.setState({
inputValue: _this.state.selectedDate.format(_this.resolvePropsInputFormat())
});
});
};
this.subtractMonth = function () {
return _this.setState({
viewDate: _this.state.viewDate.subtract(1, "months")
});
};
this.subtractYear = function () {
return _this.setState({
viewDate: _this.state.viewDate.subtract(1, "years")
});
};
this.subtractDecade = function () {
return _this.setState({
viewDate: _this.state.viewDate.subtract(10, "years")
});
};
this.togglePeriod = function () {
if (_this.state.selectedDate.hour() > 12) {
return _this.onChange(_this.state.selectedDate.clone().subtract(12, "hours").format(_this.state.inputFormat));
} else {
return _this.onChange(_this.state.selectedDate.clone().add(12, "hours").format(_this.state.inputFormat));
}
};
this.togglePicker = function () {
return _this.setState({
showDatePicker: !_this.state.showDatePicker,
showTimePicker: !_this.state.showTimePicker
});
};
this.showPicker = function () {
_this.setState(_extends({}, _this.state, {
showPicker: true
}));
};
this.onClick = function () {
if (_this.state.showPicker) {
return _this.closePicker();
} else {
return _this.showPicker();
}
};
this.closePicker = function () {
return _this.setState({
showPicker: false
});
};
this.size = function () {
switch (_this.props.size) {
case _ConstantsJs2["default"].SIZE_SMALL:
return "form-group-sm";
case _ConstantsJs2["default"].SIZE_LARGE:
return "form-group-lg";
}
return "";
};
this.renderOverlay = function () {
var styles = {
position: "fixed",
top: 0,
bottom: 0,
left: 0,
right: 0,
zIndex: "999"
};
if (_this.state.showPicker) {
return _react2["default"].createElement("div", { onClick: _this.closePicker, style: styles });
} else {
return _react2["default"].createElement("span", null);
}
};
this.canCalculateStyes = function () {
if (_this.refs.hasOwnProperty("dateField")) {
if (_this.isControlledPicker && _this.props.showPicker || _this.state.showPicker) {
return true;
}
}
return false;
};
this.getWidgetStyles = function () {
if (_this.canCalculateStyes()) {
var gBCR = _this.refs.dateField.getBoundingClientRect();
var widgetClasses = {
"bootstrap-datetimepicker-widget": true,
"dropdown-menu": true
};
var offset = {
top: gBCR.top + window.pageYOffset - document.documentElement.clientTop,
left: gBCR.left + window.pageXOffset - document.documentElement.clientLeft
};
offset.top = offset.top + _this.refs.datetimepicker.offsetHeight;
var scrollTop = window.pageYOffset !== undefined ? window.pageYOffset : (document.documentElement || document.body.parentNode || document.body).scrollTop;
var placePosition = _this.props.direction === "up" ? "top" : _this.props.direction === "bottom" ? "bottom" : _this.props.direction === "auto" ? offset.top + _this.refs.widget.offsetHeight > window.offsetHeight + scrollTop && _this.refs.widget.offsetHeight + _this.refs.datetimepicker.offsetHeight > offset.top ? "top" : "bottom" : void 0;
if (placePosition === "top") {
offset.top = -_this.refs.widget.offsetHeight - _this.clientHeight - 2;
widgetClasses.top = true;
widgetClasses.bottom = false;
widgetClasses["pull-right"] = true;
} else {
offset.top = 40;
widgetClasses.top = false;
widgetClasses.bottom = true;
widgetClasses["pull-right"] = true;
}
var widgetStyle = {
display: "block",
position: "absolute",
top: offset.top,
left: "auto",
right: 40
};
return {
widgetStyle: widgetStyle,
widgetClasses: widgetClasses
};
} else {
return {
widgetClasses: {},
widgetStyle: {
left: -9999,
display: "none"
}
};
}
};
}
_createClass(DateTimeField, [{
key: "componentDidMount",
value: function componentDidMount() {
if (this.isControlledPicker && this.props.showPicker) {
this.forceUpdate();
}
}
}, {
key: "render",
value: function render() {
var isControlledPicker = this.isControlledPicker;
var overlay = isControlledPicker ? null : this.renderOverlay();
return _react2["default"].createElement(
"div",
{ style: { position: "relative" } },
overlay,
_react2["default"].createElement(_DateTimePickerJs2["default"], _extends({
addDecade: this.addDecade,
addHour: this.addHour,
addMinute: this.addMinute,
addMonth: this.addMonth,
addYear: this.addYear,
daysOfWeekDisabled: this.props.daysOfWeekDisabled,
maxDate: this.props.maxDate,
minDate: this.props.minDate,
mode: this.props.mode,
ref: "widget",
selectedDate: this.state.selectedDate,
setSelectedDate: this.setSelectedDate,
setSelectedHour: this.setSelectedHour,
setSelectedMinute: this.setSelectedMinute,
setViewMonth: this.setViewMonth,
setViewYear: this.setViewYear,
showDatePicker: this.state.showDatePicker,
showTimePicker: this.state.showTimePicker,
showToday: this.props.showToday,
subtractDecade: this.subtractDecade,
subtractHour: this.subtractHour,
subtractMinute: this.subtractMinute,
subtractMonth: this.subtractMonth,
subtractYear: this.subtractYear,
togglePeriod: this.togglePeriod,
togglePicker: this.togglePicker,
viewDate: this.state.viewDate,
viewMode: this.props.viewMode
}, this.getWidgetStyles())),
_react2["default"].createElement(
"div",
{ className: this.props.containerClassName + this.size(), ref: "datetimepicker" },
_react2["default"].createElement(
"label",
_extends({ htmlFor: this.props.id }, this.props.labelProps),
this.props.label
),
_react2["default"].createElement("input", _extends({ onChange: this.onChange, onFocus: this.showPicker, ref: "dateField", type: "text", value: this.state.inputValue, id: this.props.id }, this.props.inputProps))
)
);
}
}, {
key: "isControlledPicker",
get: function get() {
return this.props.hasOwnProperty("showPicker");
}
}], [{
key: "defaultProps",
value: {
dateTime: (0, _moment2["default"])().format("x"),
format: "x",
showToday: true,
viewMode: "days",
daysOfWeekDisabled: [],
size: _ConstantsJs2["default"].SIZE_MEDIUM,
mode: _ConstantsJs2["default"].MODE_DATETIME,
onChange: function onChange(x) {
console.log(x);
},
containerClassName: "c_form-group"
},
enumerable: true
}, {
key: "propTypes",
value: {
dateTime: _react.PropTypes.oneOfType([_react.PropTypes.string, _react.PropTypes.number]),
onChange: _react.PropTypes.func,
format: _react.PropTypes.string,
inputProps: _react.PropTypes.object,
inputFormat: _react.PropTypes.string,
defaultText: _react.PropTypes.string,
mode: _react.PropTypes.oneOf([_ConstantsJs2["default"].MODE_DATE, _ConstantsJs2["default"].MODE_DATETIME, _ConstantsJs2["default"].MODE_TIME]),
minDate: _react.PropTypes.object,
maxDate: _react.PropTypes.object,
showPicker: _react.PropTypes.bool,
direction: _react.PropTypes.string,
showToday: _react.PropTypes.bool,
viewMode: _react.PropTypes.string,
size: _react.PropTypes.oneOf([_ConstantsJs2["default"].SIZE_SMALL, _ConstantsJs2["default"].SIZE_MEDIUM, _ConstantsJs2["default"].SIZE_LARGE]),
daysOfWeekDisabled: _react.PropTypes.arrayOf(_react.PropTypes.number),
containerClassName: _react.PropTypes.string,
label: _react.PropTypes.string.isRequired,
id: _react.PropTypes.string.isRequired,
labelProps: _react.PropTypes.object
},
enumerable: true
}]);
return DateTimeField;
})(_react.Component);
exports["default"] = DateTimeField;
module.exports = exports["default"];