"use strict";var _interopRequireWildcard=require("@babel/runtime/helpers/interopRequireWildcard"),_interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:!0}),exports.default=void 0;var _objectWithoutProperties2=_interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties")),_extends2=_interopRequireDefault(require("@babel/runtime/helpers/extends")),_objectSpread2=_interopRequireDefault(require("@babel/runtime/helpers/objectSpread")),_classCallCheck2=_interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")),_createClass2=_interopRequireDefault(require("@babel/runtime/helpers/createClass")),_possibleConstructorReturn2=_interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn")),_getPrototypeOf3=_interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf")),_inherits2=_interopRequireDefault(require("@babel/runtime/helpers/inherits")),_assertThisInitialized2=_interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized")),_defineProperty2=_interopRequireDefault(require("@babel/runtime/helpers/defineProperty")),React=_interopRequireWildcard(require("react")),_classnames=_interopRequireDefault(require("classnames")),_idx=_interopRequireDefault(require("idx")),_downshift=_interopRequireDefault(require("downshift")),_propTypes=_interopRequireDefault(require("prop-types")),_withStyles=_interopRequireDefault(require("../../hoc/withStyles")),_ErrorBoundary=_interopRequireDefault(require("../../hoc/ErrorBoundary")),_TextField=_interopRequireDefault(require("../TextField")),_styles=_interopRequireDefault(require("./styles")),SearchInput=/*#__PURE__*/function(_React$Component){function SearchInput(){var _getPrototypeOf2,_this;(0,_classCallCheck2.default)(this,SearchInput);for(var _len=arguments.length,args=Array(_len),_key=0;_key<_len;_key++)args[_key]=arguments[_key];return _this=(0,_possibleConstructorReturn2.default)(this,(_getPrototypeOf2=(0,_getPrototypeOf3.default)(SearchInput)).call.apply(_getPrototypeOf2,[this].concat(args))),(0,_defineProperty2.default)((0,_assertThisInitialized2.default)((0,_assertThisInitialized2.default)(_this)),"_handleStateReducer",function(state,changes){switch(changes.type){// Downshift by default reset the input value on blur when there is no item selected
// we dont want to reset that value
case _downshift.default.stateChangeTypes.mouseUp:case _downshift.default.stateChangeTypes.blurInput:return(0,_objectSpread2.default)({},changes,{inputValue:state.inputValue});default:return changes;}}),(0,_defineProperty2.default)((0,_assertThisInitialized2.default)((0,_assertThisInitialized2.default)(_this)),"_renderItem",function(_ref){var getItemProps=_ref.getItemProps,item=_ref.item,index=_ref.index,selectedItem=_ref.selectedItem,_this$props=_this.props,classes=_this$props.classes,renderItem=_this$props.renderItem,labelKey=_this$props.labelKey;return React.createElement("div",(0,_extends2.default)({},getItemProps({item:item}),{key:"".concat(item[labelKey],"-").concat(index),className:(0,_classnames.default)(classes.searchItem,{isSelected:selectedItem===item})}),renderItem?renderItem(item):item[labelKey])}),_this}return(0,_inherits2.default)(SearchInput,_React$Component),(0,_createClass2.default)(SearchInput,[{key:"render",value:function render(){var _this2=this,_this$props2=this.props,classes=_this$props2.classes,onSearch=_this$props2.onSearch,onItemClick=_this$props2.onItemClick,searchLoading=_this$props2.searchLoading,_this$props2$items=_this$props2.items,items=void 0===_this$props2$items?[]:_this$props2$items,showSearchIcon=_this$props2.showSearchIcon,defaultInputValue=_this$props2.defaultInputValue,renderItem=_this$props2.renderItem,labelKey=_this$props2.labelKey,showClearIcon=_this$props2.showClearIcon,onInputChange=_this$props2.onInputChange,rest=(0,_objectWithoutProperties2.default)(_this$props2,["classes","onSearch","onItemClick","searchLoading","items","showSearchIcon","defaultInputValue","renderItem","labelKey","showClearIcon","onInputChange"]);return React.createElement(_ErrorBoundary.default,null,React.createElement("div",{className:classes.root},React.createElement(_downshift.default,{itemToString:function itemToString(i){return i?i[labelKey]:i},onChange:onItemClick,initialInputValue:defaultInputValue,stateReducer:this._handleStateReducer},function(_ref2){var getInputProps=_ref2.getInputProps,getItemProps=_ref2.getItemProps,isOpen=_ref2.isOpen,inputValue=_ref2.inputValue,selectedItem=_ref2.selectedItem,clearSelection=_ref2.clearSelection;return React.createElement("div",{style:{position:"relative"}},React.createElement("div",{className:(0,_classnames.default)(classes.component,isOpen&&0<items.length?"is-open":"")},React.createElement(_TextField.default,getInputProps((0,_objectSpread2.default)({},rest,{value:inputValue||"",showClearIcon:showClearIcon||!1,label:"",classes:{textInputWrapper:classes.input},onChange:function onChange(event){var value=(0,_idx.default)(event,function(_){return _.target.value});"undefined"==typeof value||((""===value||!value)&&clearSelection(),onInputChange(value))}}))),showSearchIcon&&React.createElement("div",{className:(0,_classnames.default)(classes.icon,{isLoading:searchLoading}),onClick:function onClick(){return onSearch(inputValue)}},React.createElement("i",{className:(0,_classnames.default)({"ion-search":!searchLoading,"ion-load-c fa-spin":searchLoading})}))),isOpen&&0<(items||[]).length&&React.createElement("div",{className:classes.searchedItemsWrapper},items.map(function(item,index){return _this2._renderItem({item:item,index:index,getItemProps:getItemProps,selectedItem:selectedItem})})))})))}}]),SearchInput}(React.Component);(0,_defineProperty2.default)(SearchInput,"propTypes",(0,_objectSpread2.default)({},_TextField.default.propTypes,{items:_propTypes.default.arrayOf(_propTypes.default.any),onSearch:_propTypes.default.func,onInputChange:_propTypes.default.func,onItemClick:_propTypes.default.func,searchLoading:_propTypes.default.bool,showSearchIcon:_propTypes.default.bool,defaultInputValue:_propTypes.default.string,renderItem:_propTypes.default.func,labelKey:_propTypes.default.string})),(0,_defineProperty2.default)(SearchInput,"defaultProps",{items:[],showSearchIcon:!0,onSearch:function onSearch(){},onInputChange:function onInputChange(){},onItemClick:function onItemClick(){},defaultInputValue:"",labelKey:"label"});var _default=(0,_withStyles.default)(_styles.default)(SearchInput);exports.default=_default;