function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
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, _toPropertyKey(descriptor.key), descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
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 } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : 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; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } 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 { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
import React from 'react';
import TextField from '../TextField/TextField';
import classNames from 'classnames';
import { t } from '../i18n';
import uniqueId from 'lodash/uniqueId';
export var DateInput = /*#__PURE__*/function (_React$PureComponent) {
  _inherits(DateInput, _React$PureComponent);
  var _super = _createSuper(DateInput);
  function DateInput(props) {
    var _props$id;
    var _this;
    _classCallCheck(this, DateInput);
    _this = _super.call(this, props);
    _this.handleBlur = _this.handleBlur.bind(_assertThisInitialized(_this));
    _this.handleChange = _this.handleChange.bind(_assertThisInitialized(_this));
    _this.id = (_props$id = props.id) !== null && _props$id !== void 0 ? _props$id : uniqueId('date-input--');
    return _this;
  }
  _createClass(DateInput, [{
    key: "formatDate",
    value: function formatDate() {
      if (this.props.dateFormatter && this.monthInput && this.dayInput && this.yearInput) {
        var values = {
          month: this.monthInput.value,
          day: this.dayInput.value,
          year: this.yearInput.value
        };
        return this.props.dateFormatter(values);
      }
    }
  }, {
    key: "handleBlur",
    value: function handleBlur(evt) {
      if (this.props.onBlur) {
        this.props.onBlur(evt, this.formatDate());
      }
      if (this.props.onComponentBlur) {
        this.handleComponentBlur(evt);
      }
    }
  }, {
    key: "handleChange",
    value: function handleChange(evt) {
      this.props.onChange(evt, this.formatDate());
    }
  }, {
    key: "handleComponentBlur",
    value: function handleComponentBlur(evt) {
      var _this2 = this;
      // The active element is always the document body during a focus
      // transition, so in order to check if the newly focused element
      // is one of our other date inputs, we're going to have to wait
      // a bit.
      setTimeout(function () {
        if (document.activeElement !== _this2.dayInput && document.activeElement !== _this2.monthInput && document.activeElement !== _this2.yearInput) {
          _this2.props.onComponentBlur(evt, _this2.formatDate());
        }
      }, 20);
    }
  }, {
    key: "renderField",
    value: function renderField(type, maxLength) {
      var _this$props,
        _this3 = this;
      var sharedTextFieldProps = {
        className: 'ds-l-col--auto',
        labelClassName: 'ds-c-datefield__label',
        disabled: this.props.disabled,
        inversed: this.props.inversed,
        onBlur: (this.props.onBlur || this.props.onComponentBlur) && this.handleBlur,
        onChange: this.props.onChange && this.handleChange,
        numeric: true
      };
      return /*#__PURE__*/React.createElement(TextField, _extends({}, sharedTextFieldProps, {
        defaultValue: this.props["".concat(type, "DefaultValue")],
        value: this.props["".concat(type, "Value")],
        label: (_this$props = this.props["".concat(type, "Label")]) !== null && _this$props !== void 0 ? _this$props : t("dateField.".concat(type, "Label")),
        name: this.props["".concat(type, "Name")],
        maxLength: maxLength,
        fieldClassName: classNames("ds-c-field--".concat(type), {
          'ds-c-field--error': this.props["".concat(type, "Invalid")]
        }),
        id: "".concat(this.id, "__").concat(type),
        inputRef: function inputRef(el) {
          _this3["".concat(type, "Input")] = el;
          var ref = _this3.props["".concat(type, "FieldRef")];
          if (typeof ref === 'function') {
            ref(el);
          } else if (ref) {
            ref.current = el;
          }
        },
        autoComplete: this.props.autoComplete && "bday-".concat(type),
        "aria-invalid": this.props["".concat(type, "Invalid")]
      }));
    }
  }, {
    key: "render",
    value: function render() {
      return /*#__PURE__*/React.createElement("div", {
        className: "ds-c-datefield__container ds-l-form-row"
      }, this.renderField('month', 2), /*#__PURE__*/React.createElement("span", {
        className: "ds-c-datefield__separator"
      }, "/"), this.renderField('day', 2), /*#__PURE__*/React.createElement("span", {
        className: "ds-c-datefield__separator"
      }, "/"), this.renderField('year', 4));
    }
  }]);
  return DateInput;
}(React.PureComponent);
export default DateInput;