react-styled-date-time-picker
Version:
```javascript import React, { Component } from 'react'; import DateTimePicker from 'react-styled-date-time-picker'; import { DateTime } from 'luxon';
287 lines (235 loc) • 11.3 kB
JavaScript
;
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _react = require("react");
var _styledComponents = _interopRequireDefault(require("styled-components"));
var _Calendar = _interopRequireDefault(require("./Calendar"));
var _Time = _interopRequireDefault(require("./Time"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
var Container = _styledComponents["default"].div.withConfig({
componentId: "sc-1fzrgfi-0"
})(["display:inline-block;width:585px;padding:12px 15px;border-radius:3px;border:1px solid #dfe0e4;margin-top:20px;background:rgba(255,255,255,1);"]);
var TimeContainer = _styledComponents["default"].div.withConfig({
componentId: "sc-1fzrgfi-1"
})(["display:", ";align-items:center;justify-content:space-around;color:#f8f8f8;height:250px;& div{user-select:none;}"], function (props) {
return props.visible ? 'flex' : 'none';
});
var Options = _styledComponents["default"].div.withConfig({
componentId: "sc-1fzrgfi-2"
})(["width:100%;display:inline-block;margin-bottom:4px;"]);
var TabButton = _styledComponents["default"].button.withConfig({
componentId: "sc-1fzrgfi-3"
})(["float:left;width:50%;color:", ";background-color:", ";text-align:center;font-size:16px;padding:7px;border:1px solid #00A15F;border-radius:3px;cursor:pointer;user-select:none;&:first-child{border-top-right-radius:0;border-bottom-right-radius:0;}&:last-child{border-top-left-radius:0;border-bottom-left-radius:0;}&:focus{outline:none;}"], function (props) {
return props.active ? '#f8f8f8' : '#00A15F';
}, function (props) {
return props.active ? '#00A15F' : 'transparent';
});
var Tabs = _styledComponents["default"].div.withConfig({
componentId: "sc-1fzrgfi-4"
})(["margin-bottom:11px;"]);
var Label = _styledComponents["default"].div.withConfig({
componentId: "sc-1fzrgfi-5"
})(["margin:10px auto;margin-top:0;text-align:center;"]);
var Button = _styledComponents["default"].button.withConfig({
componentId: "sc-1fzrgfi-6"
})(["border:0;outline:0;cursor:pointer;line-height:1;display:block;margin-top:10px;width:100%;background-color:#00A15F;padding:12px 0;text-align:center;color:#f8f8f8;font-size:16px;border-radius:3px;flex:0 0 250px;&:before{margin-right:6px;}"]);
var messages = {
ru: {
date: 'Дата',
time: 'Время',
cancel: 'Отмена',
ok: 'Ок'
},
en: {
date: 'Date',
time: 'Time',
cancel: 'Cancel',
ok: 'OK'
},
it: {
date: 'Data',
time: 'Tempo',
cancel: 'Cancella',
ok: 'OK'
},
es: {
date: 'Fecha',
time: 'Tiempo',
cancel: 'Cancelar',
ok: 'OK'
},
pt: {
date: 'Data',
time: 'Hora',
cancel: 'Cancelar',
ok: 'OK'
}
};
var getLabels = function getLabels() {
var lang = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'en';
return messages[lang];
};
var Actions = _styledComponents["default"].div.withConfig({
componentId: "sc-1fzrgfi-7"
})(["display:flex;margin:0 -15px;"]);
var ButtonContainer = _styledComponents["default"].div.withConfig({
componentId: "sc-1fzrgfi-8"
})(["display:flex;justify-content:center;flex:0 0 50%;"]);
var DateTimePicker = /*#__PURE__*/function (_Component) {
_inherits(DateTimePicker, _Component);
var _super = _createSuper(DateTimePicker);
function DateTimePicker(props) {
var _this;
_classCallCheck(this, DateTimePicker);
_this = _super.call(this, props);
_defineProperty(_assertThisInitialized(_this), "selectDate", function () {
var _this$props = _this.props,
onSelect = _this$props.onSelect,
onClose = _this$props.onClose;
var _this$state = _this.state,
dateFrom = _this$state.dateFrom,
dateTo = _this$state.dateTo;
onSelect(dateFrom, dateTo);
onClose();
});
_defineProperty(_assertThisInitialized(_this), "switchTabOne", function () {
return _this.setState(function () {
return {
tab: 0
};
});
});
_defineProperty(_assertThisInitialized(_this), "switchTabTwo", function () {
return _this.setState(function () {
return {
tab: 1
};
});
});
_defineProperty(_assertThisInitialized(_this), "handleTimeFrom", function (dateFrom) {
var _this$props2 = _this.props,
onChange = _this$props2.onChange,
dateTo = _this$props2.dateTo;
if (onChange) {
onChange(dateFrom, dateTo);
}
_this.setState(function () {
return {
dateFrom: dateFrom
};
});
});
_defineProperty(_assertThisInitialized(_this), "handleTimeTo", function (dateTo) {
var _this$props3 = _this.props,
onChange = _this$props3.onChange,
dateFrom = _this$props3.dateFrom;
if (onChange) {
onChange(dateFrom, dateTo);
}
_this.setState(function () {
return {
dateTo: dateTo
};
});
});
_defineProperty(_assertThisInitialized(_this), "handleChange", function (dateFrom, dateTo) {
var onChange = _this.props.onChange;
if (onChange) {
onChange(dateFrom, dateTo);
}
_this.setState(function () {
return {
dateFrom: dateFrom,
dateTo: dateTo
};
});
});
_defineProperty(_assertThisInitialized(_this), "handleConfirmClick", function (dateFrom, dateTo) {
return _this.setState(function () {
return {
dateFrom: dateFrom,
dateTo: dateTo
};
});
});
var _dateFrom = props.dateFrom,
_dateTo = props.dateTo,
language = props.language;
_this.state = {
tab: 0,
dateFrom: _dateFrom.setLocale(language),
dateTo: _dateTo.setLocale(language)
};
return _this;
}
_createClass(DateTimePicker, [{
key: "render",
value: function render() {
var _this$state2 = this.state,
tab = _this$state2.tab,
dateFrom = _this$state2.dateFrom,
dateTo = _this$state2.dateTo;
var _this$props4 = this.props,
language = _this$props4.language,
label = _this$props4.label,
labelStyle = _this$props4.labelStyle,
onClose = _this$props4.onClose;
var labels = getLabels(language);
return /*#__PURE__*/React.createElement(Container, null, label && /*#__PURE__*/React.createElement(Label, {
style: labelStyle
}, label), /*#__PURE__*/React.createElement(Options, null, /*#__PURE__*/React.createElement(TabButton, {
type: "button",
onClick: this.switchTabOne,
active: tab === 0
}, labels.date), /*#__PURE__*/React.createElement(TabButton, {
type: "button",
onClick: this.switchTabTwo,
active: tab === 1
}, labels.time)), /*#__PURE__*/React.createElement(Tabs, null, /*#__PURE__*/React.createElement(_Calendar["default"], {
language: language,
visible: tab === 0,
dateFrom: dateFrom,
dateTo: dateTo,
onChange: this.handleChange,
switchTab: this.switchTabTwo,
onSelect: this.handleConfirmClick,
cancel: onClose
}), /*#__PURE__*/React.createElement(TimeContainer, {
visible: tab === 1
}, /*#__PURE__*/React.createElement(_Time["default"], {
value: dateFrom,
onChange: this.handleTimeFrom
}), /*#__PURE__*/React.createElement(_Time["default"], {
value: dateTo,
onChange: this.handleTimeTo
}))), /*#__PURE__*/React.createElement(Actions, null, /*#__PURE__*/React.createElement(ButtonContainer, null, /*#__PURE__*/React.createElement(Button, {
onClick: this.selectDate
}, labels.ok)), /*#__PURE__*/React.createElement(ButtonContainer, null, /*#__PURE__*/React.createElement(Button, {
onClick: onClose
}, labels.cancel))));
}
}]);
return DateTimePicker;
}(_react.Component);
_defineProperty(DateTimePicker, "defaultProps", {
language: 'en',
onChange: null,
label: '',
labelStyle: {}
});
var _default = DateTimePicker;
exports["default"] = _default;