"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.Dropdown=void 0;var _react=_interopRequireWildcard(require("react")),_propTypes=_interopRequireDefault(require("prop-types")),_DomHandler=_interopRequireDefault(require("../utils/DomHandler")),_ObjectUtils=_interopRequireDefault(require("../utils/ObjectUtils")),_classnames=_interopRequireDefault(require("classnames")),_DropdownPanel=require("./DropdownPanel"),_DropdownItem=require("./DropdownItem"),_Tooltip=_interopRequireDefault(require("../tooltip/Tooltip"));function _interopRequireDefault(e){return e&&e.__esModule?e:{default:e}}function _getRequireWildcardCache(){if("function"!=typeof WeakMap)return null;var e=new WeakMap;return _getRequireWildcardCache=function(){return e},e}function _interopRequireWildcard(e){if(e&&e.__esModule)return e;if(null===e||"object"!==_typeof(e)&&"function"!=typeof e)return{default:e};var t=_getRequireWildcardCache();if(t&&t.has(e))return t.get(e);var n,i={},o=Object.defineProperty&&Object.getOwnPropertyDescriptor;for(var r in e){Object.prototype.hasOwnProperty.call(e,r)&&((n=o?Object.getOwnPropertyDescriptor(e,r):null)&&(n.get||n.set)?Object.defineProperty(i,r,n):i[r]=e[r])}return i.default=e,t&&t.set(e,i),i}function _typeof(e){return(_typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function _defineProperties(e,t){for(var n=0;n<t.length;n++){var i=t[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(e,i.key,i)}}function _createClass(e,t,n){return t&&_defineProperties(e.prototype,t),n&&_defineProperties(e,n),e}function _inherits(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function");e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,writable:!0,configurable:!0}}),t&&_setPrototypeOf(e,t)}function _setPrototypeOf(e,t){return(_setPrototypeOf=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}function _createSuper(n){return function(){var e,t=_getPrototypeOf(n);return _possibleConstructorReturn(this,_isNativeReflectConstruct()?(e=_getPrototypeOf(this).constructor,Reflect.construct(t,arguments,e)):t.apply(this,arguments))}}function _possibleConstructorReturn(e,t){return!t||"object"!==_typeof(t)&&"function"!=typeof t?_assertThisInitialized(e):t}function _assertThisInitialized(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}function _isNativeReflectConstruct(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Date.prototype.toString.call(Reflect.construct(Date,[],function(){})),!0}catch(e){return!1}}function _getPrototypeOf(e){return(_getPrototypeOf=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)})(e)}function _defineProperty(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}var Dropdown=function(){_inherits(i,_react.Component);var n=_createSuper(i);function i(e){var t;return _classCallCheck(this,i),(t=n.call(this,e)).state={filter:"",overlayVisible:null},t.onClick=t.onClick.bind(_assertThisInitialized(t)),t.onInputFocus=t.onInputFocus.bind(_assertThisInitialized(t)),t.onInputBlur=t.onInputBlur.bind(_assertThisInitialized(t)),t.onInputKeyDown=t.onInputKeyDown.bind(_assertThisInitialized(t)),t.onEditableInputClick=t.onEditableInputClick.bind(_assertThisInitialized(t)),t.onEditableInputChange=t.onEditableInputChange.bind(_assertThisInitialized(t)),t.onEditableInputFocus=t.onEditableInputFocus.bind(_assertThisInitialized(t)),t.onOptionClick=t.onOptionClick.bind(_assertThisInitialized(t)),t.onFilterInputChange=t.onFilterInputChange.bind(_assertThisInitialized(t)),t.onFilterInputKeyDown=t.onFilterInputKeyDown.bind(_assertThisInitialized(t)),t.clear=t.clear.bind(_assertThisInitialized(t)),t}return _createClass(i,[{key:"onClick",value:function(e){var t=this;this.props.disabled||this.isClearClicked(e)||(this.focusInput.focus(),this.panel.element.offsetParent?this.hide():(this.show(),this.props.filter&&this.props.filterInputAutoFocus&&setTimeout(function(){t.filterInput.focus()},200)))}},{key:"onInputFocus",value:function(e){_DomHandler.default.addClass(this.container,"p-focus"),this.props.onFocus&&this.props.onFocus(e)}},{key:"onInputBlur",value:function(e){_DomHandler.default.removeClass(this.container,"p-focus"),this.props.onBlur&&this.props.onBlur(e)}},{key:"onUpKey",value:function(e){var t,n;this.props.options&&(t=this.findOptionIndex(this.props.value),(n=this.findPrevVisibleItem(t))&&this.selectItem({originalEvent:e,option:n})),e.preventDefault()}},{key:"onDownKey",value:function(e){var t,n;this.props.options&&(!this.panel.element.offsetParent&&e.altKey?this.show():(t=this.findOptionIndex(this.props.value),(n=this.findNextVisibleItem(t))&&this.selectItem({originalEvent:e,option:n}))),e.preventDefault()}},{key:"onInputKeyDown",value:function(e){switch(e.which){case 40:this.onDownKey(e);break;case 38:this.onUpKey(e);break;case 32:this.panel.element.offsetParent||(this.show(),e.preventDefault());break;case 13:this.hide(),e.preventDefault();break;case 27:case 9:this.hide();break;default:this.search(e)}}},{key:"search",value:function(e){var t=this;this.searchTimeout&&clearTimeout(this.searchTimeout);var n=String.fromCharCode(e.keyCode);this.previousSearchChar=this.currentSearchChar,this.currentSearchChar=n,this.previousSearchChar===this.currentSearchChar?this.searchValue=this.currentSearchChar:this.searchValue=this.searchValue?this.searchValue+n:n;var i=this.props.value?this.findOptionIndex(this.props.value):-1,o=this.searchOption(++i);o&&(this.selectItem({originalEvent:e,option:o}),this.selectedOptionUpdated=!0),this.searchTimeout=setTimeout(function(){t.searchValue=null},250)}},{key:"searchOption",value:function(e){var t;return this.searchValue&&(t=(t=this.searchOptionInRange(e,this.props.options.length))||this.searchOptionInRange(0,e)),t}},{key:"searchOptionInRange",value:function(e,t){for(var n=e;n<t;n++){var i=this.props.options[n];if(this.getOptionLabel(i).toString().toLocaleLowerCase(this.props.filterLocale).startsWith(this.searchValue.toLocaleLowerCase(this.props.filterLocale)))return i}return null}},{key:"findNextVisibleItem",value:function(e){var t=e+1;if(t===this.props.options.length)return null;var n=this.props.options[t];return!n.disabled&&(!this.hasFilter()||this.filter(n))?n:this.findNextVisibleItem(t)}},{key:"findPrevVisibleItem",value:function(e){var t=e-1;if(-1==t)return null;var n=this.props.options[t];return!n.disabled&&(!this.hasFilter()||this.filter(n))?n:this.findPrevVisibleItem(t)}},{key:"onEditableInputClick",value:function(){this.bindDocumentClickListener()}},{key:"onEditableInputChange",value:function(e){this.props.onChange({originalEvent:e.originalEvent,value:e.target.value,stopPropagation:function(){},preventDefault:function(){},target:{name:this.props.name,id:this.props.id,value:e.target.value}})}},{key:"onEditableInputFocus",value:function(e){_DomHandler.default.addClass(this.container,"p-focus"),this.hide(),this.props.onFocus&&this.props.onFocus(e)}},{key:"onOptionClick",value:function(e){var t=this;e.option.disabled||(this.selectItem(e),this.focusInput.focus()),setTimeout(function(){t.hide()},100)}},{key:"onFilterInputChange",value:function(e){this.setState({filter:e.target.value})}},{key:"resetFilter",value:function(){this.setState({filter:""})}},{key:"onFilterInputKeyDown",value:function(e){switch(e.which){case 40:this.onDownKey(e);break;case 38:this.onUpKey(e);break;case 13:this.hide(),e.preventDefault()}}},{key:"clear",value:function(e){this.props.onChange({originalEvent:e,value:null,stopPropagation:function(){},preventDefault:function(){},target:{name:this.props.name,id:this.props.id,value:null}}),this.updateEditableLabel()}},{key:"selectItem",value:function(e){var t;this.findOption(this.props.value)!==e.option&&(this.updateEditableLabel(e.option),t=this.getOptionValue(e.option),this.props.onChange({originalEvent:e.originalEvent,value:t,stopPropagation:function(){},preventDefault:function(){},target:{name:this.props.name,id:this.props.id,value:t}}))}},{key:"findOptionIndex",value:function(e){var t=-1;if(this.props.options)for(var n=0;n<this.props.options.length;n++){var i=this.getOptionValue(this.props.options[n]);if(null===e&&null==i||_ObjectUtils.default.equals(e,i,this.props.dataKey)){t=n;break}}return t}},{key:"findOption",value:function(e){var t=this.findOptionIndex(e);return-1!==t?this.props.options[t]:null}},{key:"show",value:function(){var e=this;this.panel.element.style.zIndex=String(_DomHandler.default.generateZIndex()),this.panel.element.style.display="block",setTimeout(function(){_DomHandler.default.addClass(e.panel.element,"p-input-overlay-visible"),_DomHandler.default.removeClass(e.panel.element,"p-input-overlay-hidden")},1),this.alignPanel(),this.bindDocumentClickListener(),this.setState({overlayVisible:!0})}},{key:"hide",value:function(){var e=this;this.panel&&this.panel.element&&this.panel.element.offsetParent&&(_DomHandler.default.addClass(this.panel.element,"p-input-overlay-hidden"),_DomHandler.default.removeClass(this.panel.element,"p-input-overlay-visible"),this.unbindDocumentClickListener(),this.hideTimeout=setTimeout(function(){e.panel.element.style.display="none",_DomHandler.default.removeClass(e.panel.element,"p-input-overlay-hidden")},150),this.setState({overlayVisible:!1}))}},{key:"alignPanel",value:function(){this.props.appendTo?(this.panel.element.style.minWidth=_DomHandler.default.getWidth(this.container)+"px",_DomHandler.default.absolutePosition(this.panel.element,this.container)):_DomHandler.default.relativePosition(this.panel.element,this.container)}},{key:"bindDocumentClickListener",value:function(){var t=this;this.documentClickListener||(this.documentClickListener=function(e){t.isOutsideClicked(e)&&t.hide()},document.addEventListener("click",this.documentClickListener))}},{key:"unbindDocumentClickListener",value:function(){this.documentClickListener&&(document.removeEventListener("click",this.documentClickListener),this.documentClickListener=null)}},{key:"isOutsideClicked",value:function(e){return this.container&&!(this.container.isSameNode(e.target)||this.isClearClicked(e)||this.container.contains(e.target)||this.panel&&this.panel.element&&this.panel.element.contains(e.target))}},{key:"isClearClicked",value:function(e){return _DomHandler.default.hasClass(e.target,"p-dropdown-clear-icon")}},{key:"updateEditableLabel",value:function(e){this.editableInput&&(this.editableInput.value=e?this.getOptionLabel(e):this.props.value||"")}},{key:"hasFilter",value:function(){return this.state.filter&&0<this.state.filter.trim().length}},{key:"renderHiddenSelect",value:function(e){var t=this,n=_react.default.createElement("option",{value:""},this.props.placeholder),i=e?_react.default.createElement("option",{value:e.value},this.getOptionLabel(e)):null;return _react.default.createElement("div",{className:"p-hidden-accessible p-dropdown-hidden-select"},_react.default.createElement("select",{ref:function(e){return t.nativeSelect=e},required:this.props.required,name:this.props.name,tabIndex:"-1","aria-hidden":"true"},n,i))}},{key:"renderKeyboardHelper",value:function(){var t=this;return _react.default.createElement("div",{className:"p-hidden-accessible"},_react.default.createElement("input",{ref:function(e){return t.focusInput=e},id:this.props.inputId,type:"text",readOnly:!0,"aria-haspopup":"listbox",onFocus:this.onInputFocus,onBlur:this.onInputBlur,onKeyDown:this.onInputKeyDown,disabled:this.props.disabled,tabIndex:this.props.tabIndex,"aria-label":this.props.ariaLabel,"aria-labelledby":this.props.ariaLabelledBy}))}},{key:"renderLabel",value:function(e){var t=this;if(this.props.editable){var n=e||this.props.value||"";return _react.default.createElement("input",{ref:function(e){return t.editableInput=e},type:"text",defaultValue:n,className:"p-dropdown-label p-inputtext",disabled:this.props.disabled,placeholder:this.props.placeholder,maxLength:this.props.maxLength,onClick:this.onEditableInputClick,onInput:this.onEditableInputChange,onFocus:this.onEditableInputFocus,onBlur:this.onInputBlur,"aria-label":this.props.ariaLabel,"aria-labelledby":this.props.ariaLabelledBy,"aria-haspopup":"listbox"})}var i=(0,_classnames.default)("p-dropdown-label p-inputtext",{"p-placeholder":null===e&&this.props.placeholder,"p-dropdown-label-empty":null===e&&!this.props.placeholder});return _react.default.createElement("label",{className:i},e||this.props.placeholder||"empty")}},{key:"renderClearIcon",value:function(){return null!=this.props.value&&this.props.showClear&&!this.props.disabled?_react.default.createElement("i",{className:"p-dropdown-clear-icon pi pi-times",onClick:this.clear}):null}},{key:"renderDropdownIcon",value:function(){return _react.default.createElement("div",{className:"p-dropdown-trigger",role:"button","aria-haspopup":"listbox","aria-expanded":this.state.overlayVisible},_react.default.createElement("span",{className:"p-dropdown-trigger-icon pi pi-chevron-down p-clickable"}))}},{key:"renderItems",value:function(n){var e,t,i=this,o=this.props.options;return o&&this.hasFilter()&&(e=this.state.filter.trim().toLocaleLowerCase(this.props.filterLocale),t=this.props.filterBy?this.props.filterBy.split(","):[this.props.optionLabel||"label"],o=_ObjectUtils.default.filter(o,t,e,this.props.filterMatchMode,this.props.filterLocale)),o?o.map(function(e){var t=i.getOptionLabel(e);return _react.default.createElement(_DropdownItem.DropdownItem,{key:i.getOptionKey(e),label:t,option:e,template:i.props.itemTemplate,selected:n===e,disabled:e.disabled,onClick:i.onOptionClick})}):null}},{key:"renderFilter",value:function(){var t=this;return this.props.filter?_react.default.createElement("div",{className:"p-dropdown-filter-container"},_react.default.createElement("input",{ref:function(e){return t.filterInput=e},type:"text",autoComplete:"off",className:"p-dropdown-filter p-inputtext p-component",placeholder:this.props.filterPlaceholder,onKeyDown:this.onFilterInputKeyDown,onChange:this.onFilterInputChange,value:this.state.filter}),_react.default.createElement("span",{className:"p-dropdown-filter-icon pi pi-search"})):null}},{key:"getOptionLabel",value:function(e){return this.props.optionLabel?_ObjectUtils.default.resolveFieldData(e,this.props.optionLabel):void 0!==e.label?e.label:e}},{key:"getOptionValue",value:function(e){return this.props.optionValue?_ObjectUtils.default.resolveFieldData(e,this.props.optionValue):void 0!==e.value?e.value:e}},{key:"getOptionKey",value:function(e){return this.props.dataKey?_ObjectUtils.default.resolveFieldData(e,this.props.dataKey):this.getOptionLabel(e)}},{key:"checkValidity",value:function(){return this.nativeSelect.checkValidity()}},{key:"componentDidMount",value:function(){this.props.autoFocus&&this.focusInput&&this.focusInput.focus(),this.props.tooltip&&this.renderTooltip(),this.nativeSelect.selectedIndex=1}},{key:"componentWillUnmount",value:function(){this.unbindDocumentClickListener(),this.tooltip&&(this.tooltip.destroy(),this.tooltip=null),this.hideTimeout&&(clearTimeout(this.hideTimeout),this.hideTimeout=null)}},{key:"componentDidUpdate",value:function(e){var t;this.props.filter&&this.alignPanel(),!this.panel.element.offsetParent||(t=_DomHandler.default.findSingle(this.panel.element,"li.p-highlight"))&&_DomHandler.default.scrollInView(this.panel.itemsWrapper,t),e.tooltip!==this.props.tooltip&&(this.tooltip?this.tooltip.updateContent(this.props.tooltip):this.renderTooltip()),!this.state.filter||this.props.options&&0!==this.props.options.length||this.setState({filter:""}),this.nativeSelect.selectedIndex=1}},{key:"renderTooltip",value:function(){this.tooltip=new _Tooltip.default({target:this.container,content:this.props.tooltip,options:this.props.tooltipOptions})}},{key:"render",value:function(){var e,t=this,n=(0,_classnames.default)("p-dropdown p-component",this.props.className,{"p-disabled":this.props.disabled,"p-dropdown-clearable":this.props.showClear&&!this.props.disabled}),i=this.findOption(this.props.value),o=i?this.getOptionLabel(i):null,r=this.renderHiddenSelect(i),l=this.renderKeyboardHelper(),a=this.renderLabel(o),s=this.renderDropdownIcon(),p=this.renderItems(i),u=this.renderFilter(),c=this.renderClearIcon();return this.props.editable&&this.editableInput&&(e=o||this.props.value||"",this.editableInput.value=e),_react.default.createElement("div",{id:this.props.id,ref:function(e){return t.container=e},className:n,style:this.props.style,onClick:this.onClick,onMouseDown:this.props.onMouseDown,onContextMenu:this.props.onContextMenu},l,r,a,c,s,_react.default.createElement(_DropdownPanel.DropdownPanel,{ref:function(e){return t.panel=e},appendTo:this.props.appendTo,panelStyle:this.props.panelStyle,panelClassName:this.props.panelClassName,scrollHeight:this.props.scrollHeight,filter:u},p))}}]),i}();_defineProperty(exports.Dropdown=Dropdown,"defaultProps",{id:null,name:null,value:null,options:null,optionLabel:null,optionValue:null,itemTemplate:null,style:null,className:null,scrollHeight:"200px",filter:!1,filterBy:null,filterMatchMode:"contains",filterPlaceholder:null,filterLocale:void 0,editable:!1,placeholder:null,required:!1,disabled:!1,appendTo:null,tabIndex:null,autoFocus:!1,filterInputAutoFocus:!0,panelClassName:null,panelStyle:null,dataKey:null,inputId:null,showClear:!1,maxLength:null,tooltip:null,tooltipOptions:null,ariaLabel:null,ariaLabelledBy:null,onChange:null,onFocus:null,onBlur:null,onMouseDown:null,onContextMenu:null}),_defineProperty(Dropdown,"propTypes",{id:_propTypes.default.string,name:_propTypes.default.string,value:_propTypes.default.any,options:_propTypes.default.array,optionLabel:_propTypes.default.string,optionValue:_propTypes.default.string,itemTemplate:_propTypes.default.func,style:_propTypes.default.object,className:_propTypes.default.string,scrollHeight:_propTypes.default.string,filter:_propTypes.default.bool,filterBy:_propTypes.default.string,filterMatchMode:_propTypes.default.string,filterPlaceholder:_propTypes.default.string,filterLocale:_propTypes.default.string,editable:_propTypes.default.bool,placeholder:_propTypes.default.string,required:_propTypes.default.bool,disabled:_propTypes.default.bool,appendTo:_propTypes.default.any,tabIndex:_propTypes.default.number,autoFocus:_propTypes.default.bool,filterInputAutoFocus:_propTypes.default.bool,lazy:_propTypes.default.bool,panelClassName:_propTypes.default.string,panelStyle:_propTypes.default.object,dataKey:_propTypes.default.string,inputId:_propTypes.default.string,showClear:_propTypes.default.bool,maxLength:_propTypes.default.number,tooltip:_propTypes.default.string,tooltipOptions:_propTypes.default.object,ariaLabel:_propTypes.default.string,ariaLabelledBy:_propTypes.default.string,onChange:_propTypes.default.func,onFocus:_propTypes.default.func,onBlur:_propTypes.default.func,onMouseDown:_propTypes.default.func,onContextMenu:_propTypes.default.func});