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

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

import React, { useState } from 'react';
import PropTypes from 'prop-types';

import './style.scss';

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

  var _useState = 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 React.createElement(
    'div',
    { className: 'Select ' + (className || '') },
    React.createElement(
      'select',
      _extends({
        className: 'Select-Input ' + (size && 'Select-Input_' + size + ' ' + (isInvalid && 'Select-Input_invalid')),
        onChange: changeHandler,
        value: selectedOption
      }, attrs),
      options.map(function (o) {
        return React.createElement(
          'option',
          { key: o.label, disabled: o.disabled, value: o.value },
          o.label
        );
      })
    )
  );
};

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

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

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

export default Select;