'use strict';

exports.__esModule = true;

var _extends = Object.assign || 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; };

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

require('./style.scss');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

var Select = function Select(_ref) {
  var options = _ref.options,
      props = _objectWithoutProperties(_ref, ['options']);

  var _useState = (0, _react.useState)(''),
      selectedOption = _useState[0],
      setSelectedOption = _useState[1];

  var className = props.className,
      isInvalid = props.isInvalid,
      onChange = props.onChange,
      size = props.size,
      attrs = _objectWithoutProperties(props, ['className', 'isInvalid', 'onChange', 'size']);

  var changeHandler = function changeHandler(e) {
    setSelectedOption(e.target.value);
    /* istanbul ignore else */
    if (typeof onChange === 'function') onChange(e);
  };
  return _react2.default.createElement(
    'div',
    { className: 'Select ' + (className || '') },
    _react2.default.createElement(
      'select',
      _extends({
        className: 'Select-Input ' + (size && 'Select-Input_' + size + ' ' + (isInvalid && 'Select-Input_invalid')),
        onChange: changeHandler,
        value: selectedOption
      }, attrs),
      options.map(function (o) {
        return _react2.default.createElement(
          'option',
          { key: o.label, disabled: o.disabled, value: o.value },
          o.label
        );
      })
    )
  );
};

var Options = _propTypes2.default.shape({
  disabled: _propTypes2.default.bool,
  label: _propTypes2.default.string,
  value: _propTypes2.default.oneOfType([_propTypes2.default.number, _propTypes2.default.string])
});

Select.propTypes = process.env.NODE_ENV !== "production" ? {
  className: _propTypes2.default.string,
  isInvalid: _propTypes2.default.bool,
  onChange: _propTypes2.default.func,
  options: _propTypes2.default.arrayOf(Options),
  size: _propTypes2.default.oneOf(['lg', 'md', 'sm']),
  value: _propTypes2.default.oneOfType([_propTypes2.default.number, _propTypes2.default.string])
} : {};

Select.defaultProps = {
  size: 'md'
};

exports.default = Select;
module.exports = exports['default'];