'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _createClass = 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _reactDom = require('react-dom');

var _reactDom2 = _interopRequireDefault(_reactDom);

var _reactClickOutside = require('react-click-outside');

var _reactClickOutside2 = _interopRequireDefault(_reactClickOutside);

var _style = require('./style.scss');

var _style2 = _interopRequireDefault(_style);

var _bind = require('classnames/bind');

var _bind2 = _interopRequireDefault(_bind);

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 _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var Dropdown = function (_React$Component) {
  _inherits(Dropdown, _React$Component);

  function Dropdown(props) {
    _classCallCheck(this, Dropdown);

    var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(Dropdown).call(this, props));

    _this.state = {
      isOpened: _this.props.isOpened
    };

    _this.componentWillMount = function () {
      if (_this.props.isOpened === true) {
        _this.setState({ isOpened: true });
      }
    };

    _this.componentWillReceiveProps = function (nextProps) {
      if (nextProps.isOpened !== _this.state.isOpened) {
        _this.setState({ isOpened: !!nextProps.isOpened });
      }
    };

    _this.toggleDropdown = function (e) {
      e.preventDefault();
      _this.setState({ isOpened: !_this.state.isOpened }, function () {
        if (typeof _this.props.changeCallback === 'function') {
          _this.props.changeCallback(_this.state.isOpened);
        }
      });
    };

    return _this;
  }

  _createClass(Dropdown, [{
    key: 'handleClickOutside',
    value: function handleClickOutside() {
      var _this2 = this;

      this.setState({ isOpened: false }, function () {
        if (typeof _this2.props.changeCallback === 'function') {
          _this2.props.changeCallback(_this2.state.isOpened);
        }
      });
    }
  }, {
    key: 'render',
    value: function render() {
      var cx = _bind2.default.bind(_style2.default);
      var isOpenedClass = this.state.isOpened ? _style2.default['is-opened'] : null;
      var dropdownClasses = cx(_style2.default['dropdown-component'], this.props.optClass, isOpenedClass);

      return _react2.default.createElement(
        'div',
        { className: dropdownClasses },
        _react2.default.createElement(
          'span',
          {
            className: _style2.default.trigger, onClick: this.toggleDropdown },
          this.props.trigger
        ),
        _react2.default.createElement(
          'div',
          { className: _style2.default['dropdown-wrapper'] },
          this.props.children
        )
      );
    }
  }]);

  return Dropdown;
}(_react2.default.Component);

Dropdown.defaultProps = {
  isOpened: false
};
Dropdown.propTypes = {
  /**
   * A callback function to be called when dropdown isOpen state changes.
   */
  changeCallback: _react2.default.PropTypes.func,
  /**
   * Whether the dropdown is visible.
   */
  isOpened: _react2.default.PropTypes.bool,
  /**
   * Optional styles to add to the button.
   */
  optClass: _react2.default.PropTypes.string,
  /**
   * An element to pass as a target (number, string, node).
   */
  trigger: _react2.default.PropTypes.oneOfType([_react2.default.PropTypes.number, _react2.default.PropTypes.string, _react2.default.PropTypes.node])
};
exports.default = (0, _reactClickOutside2.default)(Dropdown);