'use strict';

Object.defineProperty(exports, "__esModule", {
  value: 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 _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 _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _FieldWrapper = require('./FieldWrapper');

var _FieldWrapper2 = _interopRequireDefault(_FieldWrapper);

var _Select = require('../inputs/Select');

var _Select2 = _interopRequireDefault(_Select);

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

  function SelectField() {
    var _ref;

    var _temp, _this, _ret;

    _classCallCheck(this, SelectField);

    for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = SelectField.__proto__ || Object.getPrototypeOf(SelectField)).call.apply(_ref, [this].concat(args))), _this), _this.renderOptions = function () {
      var _this$props = _this.props,
          options = _this$props.options,
          renderOption = _this$props.renderOption,
          selectOptionKey = _this$props.selectOptionKey,
          allowNone = _this$props.allowNone,
          noneText = _this$props.noneText;

      var opts = options.map(function (option) {
        return _react2.default.createElement(
          'option',
          { key: selectOptionKey(option) },
          renderOption(option)
        );
      });
      if (allowNone) {
        opts.unshift(_react2.default.createElement(
          'option',
          { key: 'None' },
          noneText
        ));
      }

      return opts;
    }, _temp), _possibleConstructorReturn(_this, _ret);
  }

  _createClass(SelectField, [{
    key: 'render',
    value: function render() {
      var _props = this.props,
          label = _props.label,
          helpText = _props.helpText,
          disabled = _props.disabled,
          required = _props.required,
          input = _props.input,
          noneText = _props.noneText,
          renderOption = _props.renderOption,
          callout = _props.callout;

      return _react2.default.createElement(
        _FieldWrapper2.default,
        {
          label: label,
          helpText: helpText,
          disabled: disabled,
          required: required,
          callout: callout
        },
        _react2.default.createElement(
          _Select2.default,
          _extends({}, input, {
            value: input.value ? renderOption(input.value) : noneText,
            disabled: disabled,
            required: required
          }),
          this.renderOptions()
        )
      );
    }
  }]);

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

SelectField.propTypes = {
  /** Input props, expected to be passed by redux-form */
  input: _propTypes2.default.shape({
    /** The currently selected value */
    value: _propTypes2.default.any,
    /** Event handler for change in value */
    onChange: _propTypes2.default.func
  }).isRequired,
  /** All possible values */
  options: _propTypes2.default.array,
  /** Function to render the display of a value */
  renderOption: _propTypes2.default.func,
  /** Can the user select 'none' ? */
  allowNone: _propTypes2.default.bool,
  /** Text to show if none is selected */
  noneText: _propTypes2.default.string,
  /** Adds a label above the select */
  label: _propTypes2.default.string,
  disabled: _propTypes2.default.bool,
  required: _propTypes2.default.bool,
  helpText: _propTypes2.default.string,
  selectOptionKey: _propTypes2.default.func,
  callout: _propTypes2.default.node
};
SelectField.defaultProps = {
  options: [],
  renderOption: function renderOption(val) {
    return '' + val;
  },
  allowNone: true,
  noneText: 'None',
  label: null,
  disabled: false,
  required: false,
  helpText: null,
  selectOptionKey: function selectOptionKey(option) {
    return option.key || option.id || '' + option;
  },
  callout: null
};


SelectField.usage = '\n# SelectField\n\nA dropdown input which lets you pick from a list of provided items. Supports default input-style props:\n\n* `input`: supplied by Redux Form\'s Field component, you can also specify this manually. Should contain `value` and `onChange` at least.\n* `label`: a renderable label to go above the component\n* `helpText`: some text to be rendered below the component\n* `disabled`: disables the component\n* `required`: adds a required mark and HTML field validation\n* `options`: an array of data\n* `renderOption`: a function to transform an option item into some text to show. Defaults to `val => \'\' + val`\n* `selectOptionKey`: a function to transform an option into a **unique** key\n* `callout`: optional flyout to show on hover\n\n```\n<SelectField label="Choose:" required helpText="Make a choice!" options={[\'a\', \'b\']}>\n```\n';

exports.default = SelectField;