function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

function _extends() { _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; }; return _extends.apply(this, arguments); }

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }

function _iterableToArrayLimit(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }

function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }

function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }

function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

/*
Copyright (c) 2018 Uber Technologies, Inc.

This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/
import * as React from 'react';
import { getOverrides } from '../helpers/overrides.js';
import { Delete as DeleteIcon, TriangleDown as TriangleDownIcon, Search as SearchIconComponent } from '../icon/index.js';
import { LocaleContext } from '../locale/index.js';
import { Popover, PLACEMENT } from '../popover/index.js';
import { Spinner } from '../spinner/index.js';
import AutosizeInput from './autosize-input.js';
import { TYPE, STATE_CHANGE_TYPE } from './constants.js';
import defaultProps from './default-props.js';
import SelectDropdown from './dropdown.js';
import MultiValue from './multi-value.js';
import { StyledRoot, StyledControlContainer, StyledPlaceholder, StyledValueContainer, StyledInputContainer, StyledIconsContainer, StyledSelectArrow, StyledClearIcon, getLoadingIconStyles, StyledSearchIcon } from './styled-components.js';
import { shouldShowValue, shouldShowPlaceholder, expandValue } from './utils/index.js';
import Value from './value.js';

var isClick = function isClick(event) {
  return event.type === 'click';
};

var isLeftClick = function isLeftClick(event) {
  return event.button !== null && event.button !== undefined && event.button === 0;
};

var containsNode = function containsNode(parent, child) {
  if (typeof document !== 'undefined') {
    // eslint-disable-next-line flowtype/no-weak-types
    return child && parent && parent.contains(child);
  }
};

export function isInteractive(rootTarget, rootElement) {
  if (rootTarget instanceof Element) {
    var target = rootTarget;

    while (target && target !== rootElement) {
      var role = target.getAttribute('role');

      if (role === 'button' || role === 'link') {
        return true;
      }

      if (target.tagName) target = target.parentElement;
    }
  }

  return false;
}

var Select =
/*#__PURE__*/
function (_React$Component) {
  _inherits(Select, _React$Component);

  function Select() {
    var _getPrototypeOf2;

    var _this;

    _classCallCheck(this, Select);

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

    _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(Select)).call.apply(_getPrototypeOf2, [this].concat(args)));

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "anchor", React.createRef());

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "dropdown", React.createRef());

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "input", void 0);

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "dragging", void 0);

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "focusAfterClear", void 0);

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "openAfterFocus", void 0);

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "state", {
      inputValue: '',
      isFocused: false,
      isOpen: false,
      isPseudoFocused: false
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "handleTouchOutside", function (event) {
      if (!containsNode(_this.anchor.current, event.target)) {
        _this.closeMenu();
      }
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "handleTouchMove", function () {
      return _this.dragging = true;
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "handleTouchStart", function () {
      return _this.dragging = false;
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "handleTouchEnd", function (event) {
      if (_this.dragging) return;

      _this.handleClick(event);
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "handleTouchEndClearValue", function (event) {
      if (_this.dragging) return;

      _this.clearValue(event);
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "handleClick", function (event) {
      if (_this.props.disabled || !isClick(event) && !isLeftClick(event)) {
        return;
      } // Case comes up when text has been typed into the input field. If no text provided,
      // the 'input' element will have essentially 0 width therefore will not be clickable.
      // When click outside does not reset input, text provided will stay rendered after clicks away
      // from the select component. Upon subsequent clicks on the provided text, open the dropdown
      // menu, in addition to text edit operations.


      if (event.target === _this.input) {
        // CHASE: not sure why this condition is here. I cannot replicate a situation where clicks
        // on provided text break into here.
        if (!_this.state.isFocused) {
          _this.openAfterFocus = _this.props.openOnClick;

          _this.focus();

          return;
        }

        if (!_this.state.isOpen) {
          _this.setState({
            isOpen: true,
            isPseudoFocused: false
          });

          return;
        }
      } // Ensures that interactive elements within the Select component do not trigger the outer click
      // handler. For example, after an option is selected clicks on the 'clear' icon call here. We
      // should ignore those events. This comes after case where click is on input element, so that
      // those are handled on their own.


      if (_this.input && isInteractive(event.target, _this.input)) {
        return;
      } // For the simple case where clicking on the Select does not allow for providing
      // text input to filter the dropdown options.


      if (!_this.props.searchable) {
        _this.focus();

        _this.setState(function (prev) {
          return {
            isOpen: !prev.isOpen
          };
        });

        return;
      } // Cases below only apply to searchable Select component.


      if (_this.state.isFocused) {
        // iOS ignores programmaitc calls to input.focus() that were not triggered by a click event.
        // This component can get into a state where isFocused is true, but the DOM node is not
        // focused. Call focus here again to ensure.
        _this.focus(); // Case comes up when click outside does not reset input - once text has been provided to
        // the input, and the user closes the dropdown menu the provided text is maintained. After
        // this, if the user focuses back into the select component then clicks on the component,
        // the provided text highlights rather than position's the cursor at the end of the input.


        if (_this.input) _this.input.value = '';

        _this.setState(function (prev) {
          return {
            isOpen: _this.focusAfterClear ? false : !prev.isOpen,
            isPseudoFocused: false
          };
        });

        _this.focusAfterClear = false;
      } else {
        _this.openAfterFocus = _this.props.openOnClick;

        _this.focus();
      }
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "handleInputFocus", function (event) {
      if (_this.props.disabled) return;
      if (_this.props.onFocus) _this.props.onFocus(event);
      var toOpen = _this.state.isOpen || _this.openAfterFocus; // if focus happens after clear values, don't open dropdown yet.

      toOpen = _this.focusAfterClear ? false : toOpen;

      _this.setState({
        isFocused: true,
        isOpen: !!toOpen
      });

      _this.focusAfterClear = false;
      _this.openAfterFocus = false;
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "handleBlur", function (event) {
      if (containsNode(_this.anchor.current, event.target)) {
        return;
      }

      if (_this.props.onBlur) {
        _this.props.onBlur(event);
      }

      var onBlurredState = {
        isFocused: false,
        isOpen: false,
        isPseudoFocused: false
      };

      if (_this.props.onBlurResetsInput) {
        onBlurredState.inputValue = '';
      }

      _this.setState(onBlurredState);

      if (typeof document !== 'undefined') {
        document.removeEventListener('click', _this.handleClickOutside);
      }
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "handleClickOutside", function (event) {
      if (containsNode(_this.dropdown.current, event.target)) return;
      var isFocused = _this.state.isFocused || _this.state.isPseudoFocused;

      if (isFocused && !containsNode(_this.anchor.current, event.target)) {
        _this.handleBlur(event);
      }
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "handleInputChange", function (event) {
      var newInputValue = event.target.value;

      _this.setState({
        inputValue: newInputValue,
        isOpen: true,
        isPseudoFocused: false
      });

      if (_this.props.onInputChange) {
        _this.props.onInputChange(event);
      }
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "handleKeyDown", function (event) {
      if (_this.props.disabled) return;

      switch (event.keyCode) {
        case 8:
          // backspace
          if (!_this.state.inputValue && _this.props.backspaceRemoves) {
            event.preventDefault();

            _this.popValue();
          }

          break;

        case 13:
          // enter
          event.preventDefault();
          event.stopPropagation();

          if (!_this.state.isOpen) {
            _this.setState({
              isOpen: true
            });
          }

          break;

        case 9:
          // tab
          _this.setState(function (prevState) {
            return {
              isPseudoFocused: false,
              isFocused: false,
              isOpen: false,
              inputValue: _this.props.onCloseResetsInput ? '' : prevState.inputValue
            };
          });

          break;

        case 27:
          // escape
          event.preventDefault();

          if (_this.state.isOpen) {
            _this.closeMenu();

            event.stopPropagation();
          } else if (_this.props.clearable && _this.props.escapeClearsValue) {
            _this.clearValue(event);

            _this.setState({
              isFocused: false,
              isPseudoFocused: false
            });

            event.stopPropagation();
          }

          break;

        case 32:
          // space
          if (_this.props.searchable) {
            break;
          }

          event.preventDefault();

          if (!_this.state.isOpen) {
            _this.setState({
              isOpen: true
            });
          }

          break;

        case 38:
          // up
          event.preventDefault();

          if (!_this.state.isOpen) {
            _this.setState({
              isOpen: true
            });
          }

          break;

        case 40:
          // down
          event.preventDefault();

          if (!_this.state.isOpen) {
            _this.setState({
              isOpen: true
            });
          }

          break;

        case 33:
          // page up
          event.preventDefault();

          if (!_this.state.isOpen) {
            _this.setState({
              isOpen: true
            });
          }

          break;

        case 34:
          // page down
          event.preventDefault();

          if (!_this.state.isOpen) {
            _this.setState({
              isOpen: true
            });
          }

          break;

        case 35:
          // end key
          if (event.shiftKey) {
            break;
          }

          event.preventDefault();

          if (!_this.state.isOpen) {
            _this.setState({
              isOpen: true
            });
          }

          break;

        case 36:
          // home key
          if (event.shiftKey) {
            break;
          }

          event.preventDefault();

          if (!_this.state.isOpen) {
            _this.setState({
              isOpen: true
            });
          }

          break;

        case 46:
          // delete
          if (!_this.state.inputValue && _this.props.deleteRemoves) {
            event.preventDefault();

            _this.popValue();
          }

          break;
      }
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "getOptionLabel", function (locale, _ref) {
      var option = _ref.option;

      if (option.isCreatable) {
        return locale.select.create + ' ' + '"' + option[_this.props.labelKey] + '"';
      }

      return option[_this.props.labelKey];
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "getValueLabel", function (_ref2) {
      var option = _ref2.option;
      return option[_this.props.labelKey];
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "selectValue", function (_ref3) {
      var item = _ref3.item;

      if (item.disabled) {
        return;
      } // NOTE: we add/set the value in a callback to make sure the
      // input value is empty to avoid styling issues in Chrome


      var updatedValue = _this.props.onSelectResetsInput ? '' : _this.state.inputValue;

      if (_this.props.multi) {
        _this.setState({
          inputValue: updatedValue,
          isOpen: !_this.props.closeOnSelect
        }, function () {
          var valueArray = _this.props.value;

          if (valueArray.some(function (i) {
            return i[_this.props.valueKey] === item[_this.props.valueKey];
          })) {
            _this.removeValue(item);
          } else {
            _this.addValue(item);
          }
        });
      } else {
        _this.setState({
          inputValue: updatedValue,
          isOpen: !_this.props.closeOnSelect,
          isPseudoFocused: _this.state.isFocused
        }, function () {
          _this.setValue([item], item, STATE_CHANGE_TYPE.select);
        });
      }
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "addValue", function (item) {
      var valueArray = _toConsumableArray(_this.props.value);

      _this.setValue(valueArray.concat(item), item, STATE_CHANGE_TYPE.select);
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "popValue", function () {
      if (_this.props.multi) {
        var valueArray = _toConsumableArray(_this.props.value);

        var valueLength = valueArray.length;
        if (!valueLength) return;
        if (valueArray[valueLength - 1].clearableValue === false) return;
        var item = valueArray.pop();

        _this.setValue(valueArray, item, STATE_CHANGE_TYPE.remove);
      }
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "removeValue", function (item) {
      var valueArray = _toConsumableArray(_this.props.value);

      _this.setValue(valueArray.filter(function (i) {
        return i[_this.props.valueKey] !== item[_this.props.valueKey];
      }), item, STATE_CHANGE_TYPE.remove);

      _this.focus();
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "clearValue", function (event) {
      if (isClick(event) && !isLeftClick(event)) return;

      var resetValue = _this.props.value.filter(function (item) {
        return item.clearableValue === false;
      });

      _this.setValue(resetValue, null, STATE_CHANGE_TYPE.clear);

      _this.setState({
        inputValue: '',
        isOpen: false
      });

      _this.focus();

      _this.focusAfterClear = true;
    });

    return _this;
  }

  _createClass(Select, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      if (this.props.autoFocus) {
        this.focus();
      }
    }
  }, {
    key: "componentDidUpdate",
    value: function componentDidUpdate(prevProps, prevState) {
      if (typeof document !== 'undefined') {
        if (prevState.isOpen !== this.state.isOpen) {
          if (this.state.isOpen) {
            this.props.onOpen && this.props.onOpen();
            document.addEventListener('touchstart', this.handleTouchOutside);
          } else {
            this.props.onClose && this.props.onClose();
            document.removeEventListener('touchstart', this.handleTouchOutside);
          }
        }

        if (!prevState.isFocused && this.state.isFocused) {
          document.addEventListener('click', this.handleClickOutside);
        }
      }
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      if (typeof document !== 'undefined') {
        document.removeEventListener('touchstart', this.handleTouchOutside);
      }
    }
  }, {
    key: "focus",
    value: function focus() {
      if (!this.input) return;
      this.input.focus();
    } // Handle touch outside on mobile to dismiss menu, ensures that the
    // touch target is not within the anchor DOM node.

  }, {
    key: "closeMenu",
    value: function closeMenu() {
      if (this.props.onCloseResetsInput) {
        this.setState({
          inputValue: '',
          isOpen: false,
          isPseudoFocused: this.state.isFocused && !this.props.multi
        });
      } else {
        this.setState({
          isOpen: false,
          isPseudoFocused: this.state.isFocused && !this.props.multi
        });
      }
    }
  }, {
    key: "getValueArray",

    /**
     * Extends the value into an array from the given options
     */
    value: function getValueArray(value) {
      var _this2 = this;

      if (!Array.isArray(value)) {
        if (value === null || value === undefined) return [];
        value = [value];
      }

      return value.map(function (value) {
        return expandValue(value, _this2.props);
      });
    }
  }, {
    key: "setValue",
    value: function setValue(value, option, type) {
      if (this.props.onChange) {
        this.props.onChange({
          value: value,
          option: option,
          type: type
        });
      }
    }
  }, {
    key: "renderLoading",
    value: function renderLoading() {
      if (!this.props.isLoading) return;
      var sharedProps = this.getSharedProps();
      var _this$props$overrides = this.props.overrides,
          overrides = _this$props$overrides === void 0 ? {} : _this$props$overrides;

      var _getOverrides = getOverrides(overrides.LoadingIndicator, Spinner),
          _getOverrides2 = _slicedToArray(_getOverrides, 2),
          LoadingIndicator = _getOverrides2[0],
          loadingIndicatorProps = _getOverrides2[1];

      return React.createElement(LoadingIndicator, _extends({
        size: 16,
        overrides: {
          Svg: {
            style: getLoadingIconStyles
          }
        }
      }, sharedProps, loadingIndicatorProps));
    }
  }, {
    key: "renderValue",
    value: function renderValue(valueArray, isOpen, locale) {
      var _this3 = this;

      var _this$props$overrides2 = this.props.overrides,
          overrides = _this$props$overrides2 === void 0 ? {} : _this$props$overrides2;
      var sharedProps = this.getSharedProps();
      var renderLabel = this.props.getValueLabel || this.getValueLabel;

      var _getOverrides3 = getOverrides(overrides.Placeholder, StyledPlaceholder),
          _getOverrides4 = _slicedToArray(_getOverrides3, 2),
          Placeholder = _getOverrides4[0],
          placeholderProps = _getOverrides4[1];

      if (!valueArray.length) {
        var showPlaceholder = shouldShowPlaceholder(this.state, this.props, isOpen);
        return showPlaceholder ? React.createElement(Placeholder, _extends({}, sharedProps, placeholderProps), this.props.placeholder || locale.select.placeholder) : null;
      }

      if (this.props.multi) {
        return valueArray.map(function (value, i) {
          var disabled = sharedProps.$disabled || value.clearableValue === false;
          return React.createElement(MultiValue, _extends({
            value: value,
            key: "value-".concat(i, "-").concat(value[_this3.props.valueKey]),
            removeValue: function removeValue() {
              return _this3.removeValue(value);
            },
            disabled: disabled,
            overrides: {
              MultiValue: overrides.MultiValue
            }
          }, sharedProps, {
            $disabled: disabled
          }), renderLabel({
            option: value,
            index: i
          }));
        });
      } else if (shouldShowValue(this.state, this.props)) {
        return React.createElement(Value, _extends({
          value: valueArray[0],
          disabled: this.props.disabled,
          overrides: {
            SingleValue: overrides.SingleValue
          }
        }, sharedProps), renderLabel({
          option: valueArray[0]
        }));
      }
    }
  }, {
    key: "renderInput",
    value: function renderInput() {
      var _this4 = this;

      var _this$props$overrides3 = this.props.overrides,
          overrides = _this$props$overrides3 === void 0 ? {} : _this$props$overrides3;

      var _getOverrides5 = getOverrides(overrides.InputContainer, StyledInputContainer),
          _getOverrides6 = _slicedToArray(_getOverrides5, 2),
          InputContainer = _getOverrides6[0],
          inputContainerProps = _getOverrides6[1];

      var sharedProps = this.getSharedProps();
      var isOpen = this.state.isOpen;
      var value = this.state.inputValue;

      if (value && !this.props.onSelectResetsInput && !this.state.isFocused) {
        // It hides input value when it is not focused and was not reset on select
        value = '';
      }

      if (!this.props.searchable) {
        return React.createElement(InputContainer, _extends({
          "aria-expanded": isOpen,
          "aria-disabled": this.props.disabled,
          "aria-label": this.props['aria-label'],
          "aria-labelledby": this.props['aria-labelledby'],
          "aria-required": this.props.required || null,
          onBlur: this.handleBlur,
          onFocus: this.handleInputFocus,
          $ref: function $ref(ref) {
            return _this4.input = ref;
          },
          tabIndex: 0
        }, sharedProps, inputContainerProps));
      }

      return React.createElement(InputContainer, _extends({}, sharedProps, inputContainerProps), React.createElement(AutosizeInput, _extends({
        "aria-autocomplete": "list",
        "aria-describedby": this.props['aria-describedby'],
        "aria-disabled": this.props.disabled || null,
        "aria-expanded": isOpen,
        "aria-haspopup": isOpen,
        "aria-label": this.props['aria-label'],
        "aria-labelledby": this.props['aria-labelledby'],
        "aria-required": this.props.required || null,
        disabled: this.props.disabled || null,
        inputRef: function inputRef(ref) {
          return _this4.input = ref;
        },
        onBlur: this.handleBlur,
        onChange: this.handleInputChange,
        onFocus: this.handleInputFocus,
        overrides: {
          Input: overrides.Input
        },
        required: this.props.required && !this.props.value.length || null,
        role: "combobox",
        value: value
      }, sharedProps)));
    }
  }, {
    key: "renderClear",
    value: function renderClear() {
      var sharedProps = this.getSharedProps();
      var value = this.props.value;
      if (!this.props.clearable || !value || !value.length || this.props.disabled || this.props.isLoading) return;
      var _this$props$overrides4 = this.props.overrides,
          overrides = _this$props$overrides4 === void 0 ? {} : _this$props$overrides4;

      var _getOverrides7 = getOverrides(overrides.ClearIcon, DeleteIcon),
          _getOverrides8 = _slicedToArray(_getOverrides7, 2),
          ClearIcon = _getOverrides8[0],
          clearIconProps = _getOverrides8[1];

      var ariaLabel = this.props.multi ? 'Clear all' : 'Clear value';
      return React.createElement(ClearIcon, _extends({
        size: 16,
        title: ariaLabel,
        "aria-label": ariaLabel,
        onClick: this.clearValue,
        onTouchEnd: this.handleTouchEndClearValue,
        onTouchMove: this.handleTouchMove,
        onTouchStart: this.handleTouchStart,
        overrides: {
          Svg: StyledClearIcon
        },
        role: "button"
      }, sharedProps, clearIconProps));
    }
  }, {
    key: "renderArrow",
    value: function renderArrow() {
      if (this.props.type !== TYPE.select) {
        return null;
      }

      var _this$props$overrides5 = this.props.overrides,
          overrides = _this$props$overrides5 === void 0 ? {} : _this$props$overrides5;

      var _getOverrides9 = getOverrides(overrides.SelectArrow, TriangleDownIcon),
          _getOverrides10 = _slicedToArray(_getOverrides9, 2),
          SelectArrow = _getOverrides10[0],
          selectArrowProps = _getOverrides10[1];

      var sharedProps = this.getSharedProps();
      return React.createElement(SelectArrow, _extends({
        size: 16,
        title: 'open',
        overrides: {
          Svg: StyledSelectArrow
        }
      }, sharedProps, selectArrowProps));
    }
  }, {
    key: "renderSearch",
    value: function renderSearch() {
      if (this.props.type !== TYPE.search) {
        return null;
      }

      var _this$props$overrides6 = this.props.overrides,
          overrides = _this$props$overrides6 === void 0 ? {} : _this$props$overrides6;

      var _getOverrides11 = getOverrides(overrides.SearchIcon, SearchIconComponent),
          _getOverrides12 = _slicedToArray(_getOverrides11, 2),
          SearchIcon = _getOverrides12[0],
          searchIconProps = _getOverrides12[1];

      var sharedProps = this.getSharedProps();
      return React.createElement(SearchIcon, _extends({
        size: 16,
        title: 'search',
        overrides: {
          Svg: StyledSearchIcon
        }
      }, sharedProps, searchIconProps));
    }
  }, {
    key: "filterOptions",
    value: function filterOptions(excludeOptions) {
      var _this5 = this;

      var filterValue = this.state.inputValue;
      var options = this.props.options || []; // apply filter function

      if (this.props.filterOptions) {
        options = this.props.filterOptions(options, filterValue, excludeOptions, {
          valueKey: this.props.valueKey,
          labelKey: this.props.labelKey
        });
      } // can user create a new option + there's no exact match already


      if (filterValue && this.props.creatable && options.concat(this.props.value).every(function (opt) {
        return opt[_this5.props.labelKey].toLowerCase() !== filterValue.toLowerCase().trim();
      })) {
        var _options$push;

        // $FlowFixMe
        options.push((_options$push = {
          id: filterValue
        }, _defineProperty(_options$push, this.props.labelKey, filterValue), _defineProperty(_options$push, this.props.valueKey, filterValue), _defineProperty(_options$push, "isCreatable", true), _options$push));
      }

      return options;
    }
  }, {
    key: "getSharedProps",
    value: function getSharedProps() {
      var _this$props = this.props,
          clearable = _this$props.clearable,
          creatable = _this$props.creatable,
          disabled = _this$props.disabled,
          error = _this$props.error,
          isLoading = _this$props.isLoading,
          multi = _this$props.multi,
          required = _this$props.required,
          size = _this$props.size,
          searchable = _this$props.searchable,
          type = _this$props.type;
      var _this$state = this.state,
          isOpen = _this$state.isOpen,
          isFocused = _this$state.isFocused,
          isPseudoFocused = _this$state.isPseudoFocused;
      return {
        $clearable: clearable,
        $creatable: creatable,
        $disabled: disabled,
        $error: error,
        $isFocused: isFocused,
        $isLoading: isLoading,
        $isOpen: isOpen,
        $isPseudoFocused: isPseudoFocused,
        $multi: multi,
        $required: required,
        $searchable: searchable,
        $size: size,
        $type: type
      };
    }
  }, {
    key: "render",
    value: function render() {
      var _this6 = this;

      var _this$props2 = this.props,
          _this$props2$override = _this$props2.overrides,
          overrides = _this$props2$override === void 0 ? {} : _this$props2$override,
          type = _this$props2.type,
          multi = _this$props2.multi,
          value = _this$props2.value,
          filterOutSelected = _this$props2.filterOutSelected;

      var _getOverrides13 = getOverrides(overrides.Root, StyledRoot),
          _getOverrides14 = _slicedToArray(_getOverrides13, 2),
          Root = _getOverrides14[0],
          rootProps = _getOverrides14[1];

      var _getOverrides15 = getOverrides(overrides.ControlContainer, StyledControlContainer),
          _getOverrides16 = _slicedToArray(_getOverrides15, 2),
          ControlContainer = _getOverrides16[0],
          controlContainerProps = _getOverrides16[1];

      var _getOverrides17 = getOverrides(overrides.ValueContainer, StyledValueContainer),
          _getOverrides18 = _slicedToArray(_getOverrides17, 2),
          ValueContainer = _getOverrides18[0],
          valueContainerProps = _getOverrides18[1];

      var _getOverrides19 = getOverrides(overrides.IconsContainer, StyledIconsContainer),
          _getOverrides20 = _slicedToArray(_getOverrides19, 2),
          IconsContainer = _getOverrides20[0],
          iconsContainerProps = _getOverrides20[1];

      var sharedProps = this.getSharedProps();
      var valueArray = this.getValueArray(value);
      var options = this.filterOptions(multi && filterOutSelected ? valueArray : null);
      var isOpen = this.state.isOpen;

      if (multi && !options.length && valueArray.length && !this.state.inputValue) {
        isOpen = false;
      }

      sharedProps.$isOpen = isOpen;
      return React.createElement(LocaleContext.Consumer, null, function (locale) {
        return React.createElement(Popover // Popover does not provide ability to forward refs through, and if we were to simply
        // apply the ref to the Root component below it would be overwritten before the popover
        // renders it. Using this strategy, we will get a ref to the popover, then reuse its
        // anchorRef so we can check if clicks are on the select component or not.
        , {
          ref: function ref(_ref4) {
            if (!_ref4) return;
            _this6.anchor = _ref4.anchorRef;
          },
          mountNode: _this6.props.mountNode,
          isOpen: isOpen,
          content: function content() {
            var dropdownProps = {
              error: _this6.props.error,
              getOptionLabel: _this6.props.getOptionLabel || _this6.getOptionLabel.bind(_this6, locale),
              isLoading: _this6.props.isLoading,
              labelKey: _this6.props.labelKey,
              maxDropdownHeight: _this6.props.maxDropdownHeight,
              multi: multi,
              onItemSelect: _this6.selectValue,
              options: options,
              overrides: overrides,
              required: _this6.props.required,
              searchable: _this6.props.searchable,
              size: _this6.props.size,
              type: type,
              value: valueArray,
              valueKey: _this6.props.valueKey,
              width: _this6.anchor.current ? _this6.anchor.current.clientWidth : null
            };

            if (options && options.length) {
              return React.createElement(SelectDropdown, _extends({
                innerRef: _this6.dropdown
              }, dropdownProps));
            } else if (_this6.props.noResultsMsg) {
              var _noResults;

              var noResults = (_noResults = {}, _defineProperty(_noResults, _this6.props.valueKey, 'NO_RESULTS_FOUND'), _defineProperty(_noResults, _this6.props.labelKey, _this6.props.noResultsMsg || locale.select.noResultsMsg), _defineProperty(_noResults, "disabled", true), _noResults);
              return React.createElement(SelectDropdown, _extends({
                innerRef: _this6.dropdown
              }, dropdownProps, {
                options: [noResults]
              }));
            } else {
              return null;
            }
          },
          placement: PLACEMENT.bottom
        }, React.createElement(Root, _extends({
          "data-baseweb": "select"
        }, sharedProps, rootProps), React.createElement(ControlContainer, _extends({
          onKeyDown: _this6.handleKeyDown,
          onClick: _this6.handleClick,
          onTouchEnd: _this6.handleTouchEnd,
          onTouchMove: _this6.handleTouchMove,
          onTouchStart: _this6.handleTouchStart
        }, sharedProps, controlContainerProps), type === TYPE.search ? _this6.renderSearch() : null, React.createElement(ValueContainer, _extends({}, sharedProps, valueContainerProps), _this6.renderValue(valueArray, isOpen, locale), _this6.renderInput()), React.createElement(IconsContainer, _extends({}, sharedProps, iconsContainerProps), _this6.renderLoading(), _this6.renderClear(), type === TYPE.select ? _this6.renderArrow() : null))));
      });
    }
  }]);

  return Select;
}(React.Component);

_defineProperty(Select, "defaultProps", defaultProps);

export default Select;