"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = exports.TagsInput = void 0;

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));

var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));

var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));

var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));

var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _react = _interopRequireDefault(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _styles = require("@material-ui/core/styles");

var _uniq = _interopRequireDefault(require("lodash/uniq"));

var _Chip = _interopRequireDefault(require("@material-ui/core/Chip"));

var _muiBox = _interopRequireDefault(require("../mui-box"));

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2["default"])(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2["default"])(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2["default"])(this, result); }; }

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; } }

var ENTER = 13;
var Tag = (0, _styles.withStyles)(function () {
  return {
    tag: {
      padding: '0px',
      margin: '1px'
    }
  };
})(function (_ref) {
  var classes = _ref.classes,
      label = _ref.label,
      onDelete = _ref.onDelete;
  return /*#__PURE__*/_react["default"].createElement(_Chip["default"], {
    className: classes.tag,
    label: label,
    onDelete: onDelete
  });
});
Tag.propTypes = {
  label: _propTypes["default"].string.isRequired,
  onDelete: _propTypes["default"].func.isRequired
};

var TagsInput = /*#__PURE__*/function (_React$Component) {
  (0, _inherits2["default"])(TagsInput, _React$Component);

  var _super = _createSuper(TagsInput);

  function TagsInput(props) {
    var _this;

    (0, _classCallCheck2["default"])(this, TagsInput);
    _this = _super.call(this, props);
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "onFocus", function () {
      _this.setState({
        focused: true
      });
    });
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "onBlur", function () {
      _this.setState({
        focused: false
      });
    });
    _this.state = {
      value: '',
      focused: false
    };

    _this.onKeyDown = function (event) {
      if (event.keyCode === ENTER && _this.state.value !== '') {
        var tag = _this.state.value.trim();

        var newTags = (0, _uniq["default"])(_this.props.tags.concat([tag]));

        if (newTags.length !== _this.props.tags.length) {
          _this.props.onChange(newTags);

          _this.setState({
            value: ''
          });
        }
      }
    };

    _this.onChange = function (event) {
      _this.setState({
        value: event.target.value
      });
    };

    _this.deleteTag = function (tag) {
      var tags = _this.props.tags;
      var tagIndex = tags.indexOf(tag);

      if (tagIndex !== -1) {
        tags.splice(tagIndex, 1);

        _this.props.onChange(tags);

        _this.input.focus();
      }
    };

    return _this;
  }

  (0, _createClass2["default"])(TagsInput, [{
    key: "render",
    value: function render() {
      var _this2 = this;

      var _this$props = this.props,
          classes = _this$props.classes,
          tags = _this$props.tags;
      return /*#__PURE__*/_react["default"].createElement(_muiBox["default"], {
        focused: this.state.focused
      }, /*#__PURE__*/_react["default"].createElement("div", {
        className: classes.tagsInput
      }, (tags || []).map(function (t, index) {
        return /*#__PURE__*/_react["default"].createElement(Tag, {
          key: index,
          label: t,
          onDelete: function onDelete() {
            return _this2.deleteTag(t);
          }
        });
      }), /*#__PURE__*/_react["default"].createElement("input", {
        ref: function ref(r) {
          return _this2.input = r;
        },
        onKeyDown: this.onKeyDown,
        onChange: this.onChange,
        className: classes.input,
        value: this.state.value,
        onFocus: this.onFocus,
        onBlur: this.onBlur,
        type: "text"
      })));
    }
  }]);
  return TagsInput;
}(_react["default"].Component);

exports.TagsInput = TagsInput;
(0, _defineProperty2["default"])(TagsInput, "propTypes", {
  classes: _propTypes["default"].object.isRequired,
  tags: _propTypes["default"].arrayOf(_propTypes["default"].string).isRequired,
  onChange: _propTypes["default"].func.isRequired
});

var styles = function styles(theme) {
  return {
    tagsInput: {
      border: "0px solid ".concat(theme.palette.background.paper),
      display: 'flex',
      flexWrap: 'wrap'
    },
    input: {
      padding: '2px',
      margin: '1px',
      minWidth: '30px',
      width: '100%',
      flex: '1',
      border: "0px solid ".concat(theme.palette.background.paper),
      height: '28px',
      fontSize: theme.typography.fontSize,
      fontFamily: theme.typography.fontFamily,
      outline: 'none',
      '&:focus': {
        outline: 'none'
      }
    }
  };
};

var _default = (0, _styles.withStyles)(styles)(TagsInput);

exports["default"] = _default;
//# sourceMappingURL=index.js.map