'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 _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 Toggle = function (_React$Component) {
  _inherits(Toggle, _React$Component);

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

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

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

    _this.handleChange = function () {
      if (_this.props.disabled) {
        return;
      }

      _this.setState({ value: !_this.state.value }, function () {
        if (typeof this.props.changeCallback === 'function') {
          this.props.changeCallback({
            target: {
              name: 'toggle',
              value: this.state.value
            }
          });
        }
      });
    };

    _this.componentWillReceiveProps = function (nextProps) {
      if (nextProps.value !== _this.props.value) {
        _this.setState({ value: nextProps.value });
      }
    };

    return _this;
  }

  _createClass(Toggle, [{
    key: 'render',
    value: function render() {
      var cx = _bind2.default.bind(_style2.default);
      var onClass = this.state.value ? _style2.default.on : '';
      var outerClasses = cx(_style2.default.outer, onClass);
      var innerClasses = cx(_style2.default.inner, onClass);
      var disabledClass = this.props.disabled ? _style2.default['toggle-disabled'] : '';
      var toggleWrapper = cx(_style2.default['toggle-wrapper']);
      var toggleClass = cx(_style2.default['toggle-component'], disabledClass, this.props.optClass);

      return _react2.default.createElement(
        'div',
        { className: toggleClass, onClick: this.handleChange },
        this.props.label ? _react2.default.createElement(
          'label',
          { className: _style2.default['label-left'] },
          this.props.label
        ) : null,
        _react2.default.createElement(
          'div',
          { className: toggleWrapper },
          _react2.default.createElement('div', { className: outerClasses }),
          _react2.default.createElement('div', { className: innerClasses })
        )
      );
    }
  }]);

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

Toggle.defaultProps = {
  disabled: false,
  value: false
};
Toggle.propTypes = {
  /**
   * Value of the input.
   */
  value: _react2.default.PropTypes.bool,
  /**
   * Text displayed with the toggle.
   */
  label: _react2.default.PropTypes.string,
  /**
   * Whether the toggle is disabled.
   */
  disabled: _react2.default.PropTypes.bool,
  /**
   * Optional styles to add to the toggle.
   */
  optClass: _react2.default.PropTypes.string,
  /**
   * A callback function to be called when the toggle changes.
   */
  changeCallback: _react2.default.PropTypes.func
};
exports.default = Toggle;