react-styled-date-time-picker
Version:
```javascript import React, { Component } from 'react'; import DateTimePicker from 'react-styled-date-time-picker'; import { DateTime } from 'luxon';
250 lines (199 loc) • 10.5 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 _Arrow = _interopRequireDefault(require("./Arrow"));
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 TimeInputContainer = _styledComponents["default"].div.withConfig({
componentId: "sc-15e30i6-0"
})(["text-align:center;"]);
var Label = _styledComponents["default"].span.withConfig({
componentId: "sc-15e30i6-1"
})(["display:", ";width:65px;height:65px;font-size:38px;line-height:65px;background-color:#00A15F;border-radius:3px;text-align:center;font-family:Roboto;cursor:pointer;"], function (props) {
return props.visible ? 'inline-block' : 'none';
});
var Input = _styledComponents["default"].input.withConfig({
componentId: "sc-15e30i6-2"
})(["display:", ";width:65px;height:65px;font-size:38px;line-height:65px;background-color:#00A15F;border-radius:3px;text-align:center;-webkit-appearance:none;color:#f8f8f8;border:0;padding:0;margin:0;vertical-align:baseline;outline:0;font-family:Roboto;"], function (props) {
return props.show ? 'inline-block' : 'none';
});
var Separator = _styledComponents["default"].span.withConfig({
componentId: "sc-15e30i6-3"
})(["display:inline-block;font-size:32px;font-weight:bold;color:#00A15F;width:32px;height:65px;line-height:65px;text-align:center;"]);
var FlexRow = _styledComponents["default"].div.withConfig({
componentId: "sc-15e30i6-4"
})(["flex:0 0 100%;"]);
var ArrowContainer = _styledComponents["default"].span.withConfig({
componentId: "sc-15e30i6-5"
})(["margin:0 16px;& svg{cursor:pointer;}"]);
var ArrowDownContainer = (0, _styledComponents["default"])(ArrowContainer).withConfig({
componentId: "sc-15e30i6-6"
})(["& svg{transform:rotate(180deg);}"]);
var addZeroInFront = function addZeroInFront(value) {
return value < 10 ? "0".concat(value) : "".concat(value);
};
var Time = /*#__PURE__*/function (_Component) {
_inherits(Time, _Component);
var _super = _createSuper(Time);
function Time() {
var _this;
_classCallCheck(this, Time);
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
_this = _super.call.apply(_super, [this].concat(args));
_defineProperty(_assertThisInitialized(_this), "state", {
editHours: false,
editMinutes: false
});
_defineProperty(_assertThisInitialized(_this), "hours", void 0);
_defineProperty(_assertThisInitialized(_this), "minutes", void 0);
_defineProperty(_assertThisInitialized(_this), "updateHours", function (e) {
var _this$props = _this.props,
value = _this$props.value,
onChange = _this$props.onChange;
var date = value.set({
hours: parseInt(e.target.value, 10)
});
onChange(date);
_this.editHours();
});
_defineProperty(_assertThisInitialized(_this), "updateMinutes", function (e) {
var _this$props2 = _this.props,
value = _this$props2.value,
onChange = _this$props2.onChange;
var date = value.set({
minutes: parseInt(e.target.value, 10)
});
onChange(date);
_this.editMinutes();
});
_defineProperty(_assertThisInitialized(_this), "editHours", function () {
_this.setState(function (state) {
return {
editHours: !state.editHours
};
});
setTimeout(function () {
if (_this.hours) _this.hours.focus();
}, 0);
});
_defineProperty(_assertThisInitialized(_this), "editMinutes", function () {
_this.setState(function (state) {
return {
editMinutes: !state.editMinutes
};
});
setTimeout(function () {
if (_this.minutes) _this.minutes.focus();
}, 0);
});
_defineProperty(_assertThisInitialized(_this), "hoursUp", function () {
var _this$props3 = _this.props,
value = _this$props3.value,
onChange = _this$props3.onChange;
onChange(value.plus({
hour: 1
}));
});
_defineProperty(_assertThisInitialized(_this), "hoursDown", function () {
var _this$props4 = _this.props,
value = _this$props4.value,
onChange = _this$props4.onChange;
onChange(value.minus({
hour: 1
}));
});
_defineProperty(_assertThisInitialized(_this), "minutesUp", function () {
var _this$props5 = _this.props,
value = _this$props5.value,
onChange = _this$props5.onChange;
onChange(value.plus({
minute: 5
}));
});
_defineProperty(_assertThisInitialized(_this), "minutesDown", function () {
var _this$props6 = _this.props,
value = _this$props6.value,
onChange = _this$props6.onChange;
onChange(value.minus({
minute: 5
}));
});
return _this;
}
_createClass(Time, [{
key: "render",
value: function render() {
var _this2 = this;
var _this$state = this.state,
editHours = _this$state.editHours,
editMinutes = _this$state.editMinutes;
var value = this.props.value;
return /*#__PURE__*/React.createElement(TimeInputContainer, null, /*#__PURE__*/React.createElement(FlexRow, null, /*#__PURE__*/React.createElement(ArrowContainer, null, /*#__PURE__*/React.createElement(_Arrow["default"], {
height: "65",
width: "65",
onClick: this.hoursUp
})), /*#__PURE__*/React.createElement(ArrowContainer, null, /*#__PURE__*/React.createElement(_Arrow["default"], {
height: "65",
width: "65",
onClick: this.minutesUp
}))), /*#__PURE__*/React.createElement(FlexRow, null, /*#__PURE__*/React.createElement(Input, {
type: "text",
key: 1,
defaultValue: value.hour,
onBlur: this.updateHours,
min: 0,
max: 23,
ref: function ref(node) {
_this2.hours = node;
},
show: editHours
}), /*#__PURE__*/React.createElement(Label, {
visible: !editHours,
onClick: this.editHours
}, value.hour), /*#__PURE__*/React.createElement(Separator, null, ":"), /*#__PURE__*/React.createElement(Input, {
type: "text",
key: 2,
className: "time",
defaultValue: value.minute,
onBlur: this.updateMinutes,
min: 0,
max: 23,
ref: function ref(node) {
_this2.minutes = node;
},
show: editMinutes
}), /*#__PURE__*/React.createElement(Label, {
visible: !editMinutes,
onClick: this.editMinutes
}, addZeroInFront(value.minute))), /*#__PURE__*/React.createElement(FlexRow, null, /*#__PURE__*/React.createElement(ArrowDownContainer, null, /*#__PURE__*/React.createElement(_Arrow["default"], {
height: "65",
width: "65",
onClick: this.hoursDown
})), /*#__PURE__*/React.createElement(ArrowDownContainer, null, /*#__PURE__*/React.createElement(_Arrow["default"], {
height: "65",
width: "65",
onClick: this.minutesDown
}))));
}
}]);
return Time;
}(_react.Component);
var _default = Time;
exports["default"] = _default;