"use strict";

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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = 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 _Select = _interopRequireDefault(require("@material-ui/core/Select"));

var _MenuItem = _interopRequireDefault(require("@material-ui/core/MenuItem"));

var _correctInput = _interopRequireDefault(require("./correct-input"));

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

var _renderUi = require("../../render-ui");

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 Dropdown = /*#__PURE__*/function (_React$Component) {
  (0, _inherits2["default"])(Dropdown, _React$Component);

  var _super = _createSuper(Dropdown);

  function Dropdown(props) {
    var _this;

    (0, _classCallCheck2["default"])(this, Dropdown);
    _this = _super.call(this, props);
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "showCheckmarkAndOpen", function () {
      _this.setState({
        showCheckmark: true,
        open: true
      });
    });
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "hideCheckmarkAndClose", function () {
      _this.setState({
        showCheckmark: false,
        open: false
      });
    });
    _this.state = {
      showCheckmark: false,
      open: false
    };
    return _this;
  }

  (0, _createClass2["default"])(Dropdown, [{
    key: "render",
    value: function render() {
      var _this$props = this.props,
          classes = _this$props.classes,
          id = _this$props.id,
          correct = _this$props.correct,
          disabled = _this$props.disabled,
          value = _this$props.value,
          _onChange = _this$props.onChange,
          choices = _this$props.choices,
          showCorrectAnswer = _this$props.showCorrectAnswer;
      var _this$state = this.state,
          showCheckmark = _this$state.showCheckmark,
          open = _this$state.open;
      return /*#__PURE__*/_react["default"].createElement(_Select["default"], {
        classes: {
          root: classes.root,
          icon: classes.icon,
          selectMenu: classes.selectMenu,
          select: classes.select
        },
        disabled: disabled,
        value: value || '',
        onOpen: this.showCheckmarkAndOpen,
        onClose: this.hideCheckmarkAndClose,
        open: open,
        input: /*#__PURE__*/_react["default"].createElement(_correctInput["default"], {
          correct: showCorrectAnswer || correct
        }),
        MenuProps: {
          keepMounted: true,
          disablePortal: true
        },
        onChange: function onChange(e) {
          _onChange(id, e.target.value);
        }
      }, (choices || []).map(function (c, index) {
        return /*#__PURE__*/_react["default"].createElement(_MenuItem["default"], {
          classes: {
            root: classes.menuRoot,
            selected: classes.selected
          },
          key: "".concat(c.label, "-").concat(index),
          value: c.value
        }, /*#__PURE__*/_react["default"].createElement("span", {
          className: classes.label,
          dangerouslySetInnerHTML: {
            __html: c.label
          }
        }), showCheckmark && /*#__PURE__*/_react["default"].createElement("span", {
          className: classes.label,
          dangerouslySetInnerHTML: {
            __html: c.value === value ? ' &check;' : ''
          }
        }));
      }));
    }
  }]);
  return Dropdown;
}(_react["default"].Component);

(0, _defineProperty2["default"])(Dropdown, "propTypes", {
  id: _propTypes["default"].string,
  value: _propTypes["default"].string,
  disabled: _propTypes["default"].bool,
  onChange: _propTypes["default"].func,
  classes: _propTypes["default"].object,
  correct: _propTypes["default"].bool,
  choices: _propTypes["default"].arrayOf(_propTypes["default"].shape({
    value: _propTypes["default"].string,
    label: _propTypes["default"].string
  })),
  showCorrectAnswer: _propTypes["default"].bool
});

var styles = function styles() {
  return {
    root: {
      color: _renderUi.color.text(),
      backgroundColor: _renderUi.color.background(),
      borderColor: _renderUi.color.secondaryLight(),
      '& ul': {
        paddingTop: 0,
        paddingBottom: 0,
        border: "1px solid ".concat(_renderUi.color.text()),
        borderRadius: '5px',
        color: _renderUi.color.text(),
        backgroundColor: _renderUi.color.background()
      }
    },
    select: {
      '&:focus': {
        borderRadius: '4px'
      }
    },
    selectMenu: {
      backgroundColor: _renderUi.color.background(),
      '&:hover': {
        borderColor: 'initial'
      },
      '&:focus': {
        borderColor: 'initial'
      }
    },
    icon: {
      color: _renderUi.color.text()
    },
    selected: {
      color: "".concat(_renderUi.color.text(), " !important"),
      backgroundColor: "".concat(_renderUi.color.background(), " !important"),
      '&:hover': {
        color: _renderUi.color.text(),
        backgroundColor: "".concat(_renderUi.color.secondaryLight(), " !important")
      }
    },
    menuRoot: {
      color: _renderUi.color.text(),
      backgroundColor: _renderUi.color.background(),
      '&:focus': {
        color: _renderUi.color.text(),
        backgroundColor: _renderUi.color.background()
      },
      '&:hover': {
        color: _renderUi.color.text(),
        backgroundColor: _renderUi.color.secondaryLight()
      },
      boxSizing: 'border-box',
      padding: '25px',
      '&:first-of-type': {
        borderRadius: '3px 3px 0 0'
      },
      '&:last-of-type': {
        borderRadius: '0 0 3px 3px'
      }
    },
    label: {
      fontSize: 'max(1rem, 14px)'
    }
  };
};

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

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