react-autocomplete-plugin
Version:
A simple, powerful, lightweight and customizable autocomplete tool programmed for React projects!
2 lines (1 loc) • 31.7 kB
JavaScript
import{jsx as e,jsxs as t}from"react/jsx-runtime";import n,{useCallback as o,useRef as r,useState as l,useEffect as i}from"react";function a(e,t){if(e||t){var n=e||"",o=null==t?void 0:t.split(" ");if(!o)return n;for(var r=0,l=o;r<l.length;r++){n=n+" "+l[r]}return n}}var u=function(){return u=Object.assign||function(e){for(var t,n=1,o=arguments.length;n<o;n++)for(var r in t=arguments[n])Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r]);return e},u.apply(this,arguments)};function d(t){var o=t.ariaInputLabel?t.ariaInputLabel:"Autocomplete input label",r=t.inputLabelStyle?t.inputLabelStyle:{},l=t.inputLabelClass?t.inputLabelClass:"",i=t.inputLabel?t.inputLabel:"Select or search item.";return e(n.Fragment,{children:e("label",u({"aria-label":o,className:a("label-value",l),htmlFor:"searchInput",style:r},{children:i}))})}var c=function(){return c=Object.assign||function(e){for(var t,n=1,o=arguments.length;n<o;n++)for(var r in t=arguments[n])Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r]);return e},c.apply(this,arguments)};var s=n.forwardRef(function(o,r){var l=void 0===o.showdropDownArrow||null===o.showdropDownArrow||o.showdropDownArrow,i=void 0===o.showClearOption||null===o.showClearOption||o.showClearOption,u=o.placeHolder?o.placeHolder:"Type or select..";return t(n.Fragment,{children:[e("input",{onFocus:o.handleOnFocusEvent,onBlur:o.handleOnBlurEvent,onInput:o.onSearch,id:"searchInput",className:a("auto-complete-textfield",o.inputFieldClass),style:o.inputFieldStyle,ref:r,placeholder:u,type:"text",autoComplete:"off",disabled:!!o.isAutoCompleteDisabled,role:(null==o?void 0:o.ariaRole)?o.ariaRole:"textbox","aria-roledescription":(null==o?void 0:o.ariaRoleDescription)?o.ariaRoleDescription:"Autocomplete input field","aria-label":(null==o?void 0:o.ariaInputField)?o.ariaInputField:"Autocomplete input field"}),l&&e("span",{tabIndex:0,className:"arrow down",onClick:o.handleDropdownClick}),i&&e("span",c({className:"auto-complete-remove-selection",onClick:o.clearSearch},{children:"×"}))]})}),v=function(){return v=Object.assign||function(e){for(var t,n=1,o=arguments.length;n<o;n++)for(var r in t=arguments[n])Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r]);return e},v.apply(this,arguments)};var p=n.forwardRef(function(t,r){var l=o(function(){var e={};return(null==t?void 0:t.dropdownListStyle)&&(e=v({},t.dropdownListStyle)),e},[t.dropdownListStyle]),i=o(function(){var e="";return(null==t?void 0:t.dropdownListClass)&&(e=t.dropdownListClass),e&&""!==e?a("autocomplete-data-list",e):a("autocomplete-data-list")},[t.dropdownListClass]);return e(n.Fragment,{children:e("li",v({tabIndex:0,ref:r,id:"autocomplete-li-element-"+t.index,className:"".concat(i()).concat(t.disableProperty&&t.data[t.disableProperty]||t.disableListFn&&t.disableListFn(t.index,t.data)?" disable-list-element":""),style:l(),onMouseDown:function(){return t.onSelect(t.index,t.data,!0)},"aria-disabled":!!(t.disableProperty&&t.data[t.disableProperty]||t.disableListFn&&t.disableListFn(t.index,t.data)),"aria-label":t.objectProperty?t.data[t.objectProperty]:t.data},{children:t.objectProperty?t.data[t.objectProperty]:t.data}),t.index)})}),f=function(){return f=Object.assign||function(e){for(var t,n=1,o=arguments.length;n<o;n++)for(var r in t=arguments[n])Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r]);return e},f.apply(this,arguments)},h=function(e,t,n,o){return new(n||(n=Promise))(function(r,l){function i(e){try{u(o.next(e))}catch(e){l(e)}}function a(e){try{u(o.throw(e))}catch(e){l(e)}}function u(e){var t;e.done?r(e.value):(t=e.value,t instanceof n?t:new n(function(e){e(t)})).then(i,a)}u((o=o.apply(e,t||[])).next())})},m=function(e,t){var n,o,r,l,i={label:0,sent:function(){if(1&r[0])throw r[1];return r[1]},trys:[],ops:[]};return l={next:a(0),throw:a(1),return:a(2)},"function"==typeof Symbol&&(l[Symbol.iterator]=function(){return this}),l;function a(a){return function(u){return function(a){if(n)throw new TypeError("Generator is already executing.");for(;l&&(l=0,a[0]&&(i=0)),i;)try{if(n=1,o&&(r=2&a[0]?o.return:a[0]?o.throw||((r=o.return)&&r.call(o),0):o.next)&&!(r=r.call(o,a[1])).done)return r;switch(o=0,r&&(a=[2&a[0],r.value]),a[0]){case 0:case 1:r=a;break;case 4:return i.label++,{value:a[1],done:!1};case 5:i.label++,o=a[1],a=[0];continue;case 7:a=i.ops.pop(),i.trys.pop();continue;default:if(!(r=i.trys,(r=r.length>0&&r[r.length-1])||6!==a[0]&&2!==a[0])){i=0;continue}if(3===a[0]&&(!r||a[1]>r[0]&&a[1]<r[3])){i.label=a[1];break}if(6===a[0]&&i.label<r[1]){i.label=r[1],r=a;break}if(r&&i.label<r[2]){i.label=r[2],i.ops.push(a);break}r[2]&&i.ops.pop(),i.trys.pop();continue}a=t.call(e,i)}catch(e){a=[6,e],o=0}finally{n=r=0}if(5&a[0])throw a[1];return{value:a[0]?a[1]:void 0,done:!0}}([a,u])}}},w=function(e,t,n){if(n||2===arguments.length)for(var o,r=0,l=t.length;r<l;r++)!o&&r in t||(o||(o=Array.prototype.slice.call(t,0,r)),o[r]=t[r]);return e.concat(o||Array.prototype.slice.call(t))};function g(u){var d,c,v,g,y,b,S,C,L,D,F,x,k,V,O,E,A,R,T,j,P,M,I,N,H,B,z,W,U,q=this,G=r(null),Y=r(0),J=u.scrollThreshold?u.scrollThreshold:3,K=l(!1),Q=K[0],X=K[1],Z=r(null),$=r([]),_=l(!1),ee=_[0],te=_[1],ne=l([]),oe=ne[0],re=ne[1],le=l([]),ie=le[0],ae=le[1],ue=u.noSearchResultMessage?u.noSearchResultMessage:"No results found.",de=u.initialVisibleData?u.initialVisibleData:1e3,ce=void 0===u.isScrollThresholdRequired||null===u.isScrollThresholdRequired||u.isScrollThresholdRequired,se=l(!1),ve=se[0],pe=se[1],fe=l(0),he=fe[0],me=fe[1],we=l(!1),ge=we[0],ye=we[1],be=l(!1),Se=be[0],Ce=be[1],Le=u.viewMoreText?u.viewMoreText:"View More",De=void 0===u.showViewMore||null===u.showViewMore||u.showViewMore,Fe=void 0===u.showLoadingSpinner||null===u.showLoadingSpinner||u.showLoadingSpinner,xe=void 0!==u.isApiLoad&&null!==u.isApiLoad&&u.isApiLoad,ke=void 0!==u.disableProperty&&null!==u.disableProperty?u.disableProperty:void 0,Ve=void 0!==u.objectProperty&&null!==u.objectProperty?u.objectProperty:void 0,Oe=void 0!==u.triggerApiLoadEvent&&null!==u.triggerApiLoadEvent&&"function"==typeof u.triggerApiLoadEvent?u.triggerApiLoadEvent:void 0,Ee=void 0!==u.disableListFn&&null!==u.disableListFn&&"function"==typeof u.disableListFn?u.disableListFn:void 0,Ae=void 0!==u.broadcastSelectedValue&&null!==u.broadcastSelectedValue&&"function"==typeof u.broadcastSelectedValue?u.broadcastSelectedValue:void 0,Re=r(null),Te=r(null),je=r(-1),Pe=r(!1),Me=o(function(){var e,t,n=null===(e=Re.current)||void 0===e?void 0:e.style,o=null===(t=G.current)||void 0===t?void 0:t.clientWidth;n&&o&&(n.width=o+"px")},[]),Ie=o(function(e,t,n){if(!n)return!1;null!=t&&(ke&&t[ke]||null!=Ee&&Ee(e,t)||(G.current.value=Ve?t[Ve]:t,X(!1),ye(!1),re([]),ae([]),Ce(!1),Ye(),Ae&&Ae(t)))},[Ae,Ee,ke,Ve]),Ne=function(e){return h(q,void 0,void 0,function(){var e,t,n,o,r,l,i,a,d,c,s;return m(this,function(v){switch(v.label){case 0:return v.trys.push([0,3,,4]),(null===(i=G.current)||void 0===i?void 0:i.value)&&""!==(null===(a=G.current)||void 0===a?void 0:a.value.trim())?(ge||ye(!0),Y.current=0,Q||X(!0),u.searchFn&&"function"==typeof u.searchFn?[4,u.searchFn(G.current.value,u.dropdownData)]:[3,2]):(ae([]),He(),Be(),null===(d=Z.current)||void 0===d||d.scrollTo(0,0),[2]);case 1:if((e=v.sent())&&e.length>0)return ae(e),r=e.slice(0,de),Y.current=Y.current+r.length,re(r),Be(),[2];v.label=2;case 2:if(Ve){if(Je()){if("object"==typeof(n=null===(c=u.additionalData)||void 0===c?void 0:c.relativeSearch)&&Object.prototype.hasOwnProperty.call(n,"customRelativeSearchFunction")&&"function"==typeof n.customRelativeSearchFunction)return[2,null===(s=Xe())||void 0===s?void 0:s.then(function(e){if(ae(e),(t=e).length>0){var n=t.slice(0,de);return Y.current=Y.current+n.length,ae(t),re(n),void Be()}}).catch(function(e){console.log(e),re([])})];t=$e(),ae(t)}else t=u.dropdownData.filter(function(e){var t;return null===(t=e[Ve])||void 0===t?void 0:t.toString().toLowerCase().includes(G.current.value.toLowerCase().trim())});return t.length>0?(r=t.slice(0,de),Y.current=Y.current+r.length,ae(t),re(r),Be(),[2]):(re([]),[2])}return(o=u.dropdownData.filter(function(e){return null==e?void 0:e.toString().toLowerCase().includes(G.current.value.toLowerCase().trim())})).length>0?(r=o.slice(0,de),Y.current=Y.current+r.length,ae(o),re(r),Be(),[2]):(re([]),[2]);case 3:return l=v.sent(),console.log(l),[3,4];case 4:return[2]}})})},He=function(){var e,t=ge&&ie.length>0&&(null===(e=G.current)||void 0===e?void 0:e.value)?ie.slice(0,de):u.dropdownData.slice(0,de);re(t),Y.current=t.length},Be=o(function(){var e;void 0!==u.totalRecords&&he>=u.totalRecords?Ce(!1):(null===(e=G.current)||void 0===e?void 0:e.value)&&""!==G.current.value&&xe&&!ve||Y.current>=he?Ce(!0):Ce(!1)},[he,u.totalRecords,ve,xe]),ze=function(){try{if(!Q)return;setTimeout(function(){We()},50)}catch(e){console.log(e)}},We=function(){var e,t,n=ie.length>0?w([],ie,!0):w([],u.dropdownData,!0);if(u.dropdownData.length!==Y.current&&ie.length!==Y.current){if(n.length>oe.length){var o,r=Math.ceil(oe.length/J);if(ce&&r>=de){var l=w([],oe,!0);l.splice(0,de),(o=n.slice(Y.current,Y.current+de)).length>0&&(Y.current=Y.current+o.length,ge&&(null===(e=G.current)||void 0===e?void 0:e.value)&&""!==G.current.value&&(o=Ve?o.filter(function(e){var t,n,o;return null===(t=e[Ve])||void 0===t?void 0:t.toString().toLowerCase().includes(null===(o=null===(n=u.defaultValue)||void 0===n?void 0:n.toString())||void 0===o?void 0:o.toLowerCase().trim())}):o.filter(function(e){var t,n;return null==e?void 0:e.toString().toLowerCase().includes(null===(n=null===(t=u.defaultValue)||void 0===t?void 0:t.toString())||void 0===n?void 0:n.toLowerCase().trim())})),re(w(w([],l,!0),o,!0)))}else(o=n.slice(Y.current,Y.current+de)).length>0&&(Y.current=Y.current+o.length,ge&&(null===(t=G.current)||void 0===t?void 0:t.value)&&""!==G.current.value&&(o=Ve?o.filter(function(e){var t,n,o;return null===(t=e[Ve])||void 0===t?void 0:t.toString().toLowerCase().includes(null===(o=null===(n=G.current.value)||void 0===n?void 0:n.toString())||void 0===o?void 0:o.toLowerCase().trim())}):o.filter(function(e){var t,n;return null==e?void 0:e.toString().toLowerCase().includes(null===(n=null===(t=G.current.value)||void 0===t?void 0:t.toString())||void 0===n?void 0:n.toLowerCase().trim())})),re(function(e){return w(w([],e,!0),o,!0)}));setTimeout(function(){var e;null===(e=Z.current)||void 0===e||e.scrollTo(0,Math.ceil(50*Z.current.scrollHeight/100))},75)}}else if(!ve&&xe&&Oe&&"function"==typeof Oe){if(u.optViewMoreOnlyForApiCall)return Ce(!0),void setTimeout(function(){var e;null===(e=Z.current)||void 0===e||e.scrollTo(0,Z.current.scrollHeight+10)},75);Oe({dataIndex:u.dropdownData.length}),pe(!0),Fe&&(te(!0),setTimeout(function(){var e;null===(e=Z.current)||void 0===e||e.scrollTo(0,Z.current.scrollHeight+10)},75))}},Ue=o(function(e,t,n){var o;try{var r=w([],e,!0);if(r.length<=de)return re(r),Y.current=r.length,void Be();var l=r.slice(0,de);return Y.current=l.length,ge&&(null===(o=G.current)||void 0===o?void 0:o.value)&&""!==G.current.value&&(l=n?l.filter(function(e){var t,o,r;return null===(t=e[n])||void 0===t?void 0:t.toString().toLowerCase().includes(null===(r=null===(o=G.current.value)||void 0===o?void 0:o.toString())||void 0===r?void 0:r.toLowerCase().trim())}):l.filter(function(e){var t,n;return null==e?void 0:e.toString().toLowerCase().includes(null===(n=null===(t=G.current.value)||void 0===t?void 0:t.toString())||void 0===n?void 0:n.toLowerCase().trim())})),re(l),void Be()}catch(e){console.log(e)}},[de,ge,Be]),qe=o(function(e){var t,n,o;try{if(!ve)return;if((null===(t=G.current)||void 0===t?void 0:t.value)&&""!==G.current.value){var r=void 0;if(r=Ve?e.filter(function(e){var t;return null===(t=e[Ve])||void 0===t?void 0:t.toString().toLowerCase().includes(G.current.value.toLowerCase().trim())}):e.filter(function(e){return null==e?void 0:e.toString().toLowerCase().includes(G.current.value.toLowerCase().trim())}),r&&r.length>0){ge&&ae(r);var l=r.slice(Y.current,Y.current+de);if(l&&l.length>0){Y.current=Y.current+l.length;var i=Math.ceil(oe.length/J);if(ce&&i>=de)(d=w([],oe,!0)).splice(0,de),re(w(w([],d,!0),l,!0));else re(function(e){return w(w([],e,!0),l,!0)})}setTimeout(function(){var e;null===(e=Z.current)||void 0===e||e.scrollTo(0,Math.ceil(50*Z.current.scrollHeight/100)),pe(!1),te(!1)},100)}else pe(!1),te(!1),ae([]),re([]);return}if(e.length>oe.length){var a,d;i=Math.ceil(oe.length/J);if(ce&&i>=de)(d=w([],oe,!0)).splice(0,de),(a=e.slice(Y.current,Y.current+de)).length>0&&(Y.current=Y.current+a.length,u.defaultValue&&(null===(n=G.current)||void 0===n?void 0:n.value)&&""!==G.current.value&&(a=Ve?a.filter(function(e){var t,n,o;return null===(t=e[Ve])||void 0===t?void 0:t.toString().toLowerCase().includes(null===(o=null===(n=u.defaultValue)||void 0===n?void 0:n.toString())||void 0===o?void 0:o.toLowerCase().trim())}):a.filter(function(e){var t,n;return null==e?void 0:e.toString().toLowerCase().includes(null===(n=null===(t=u.defaultValue)||void 0===t?void 0:t.toString())||void 0===n?void 0:n.toLowerCase().trim())})),re(w(w([],d,!0),a,!0)));else(a=e.slice(Y.current,Y.current+de)).length>0&&(Y.current=Y.current+a.length,u.defaultValue&&(null===(o=G.current)||void 0===o?void 0:o.value)&&""!==G.current.value&&(a=Ve?a.filter(function(e){var t,n,o;return null===(t=e[Ve])||void 0===t?void 0:t.toString().toLowerCase().includes(null===(o=null===(n=u.defaultValue)||void 0===n?void 0:n.toString())||void 0===o?void 0:o.toLowerCase().trim())}):a.filter(function(e){var t,n;return null==e?void 0:e.toString().toLowerCase().includes(null===(n=null===(t=u.defaultValue)||void 0===t?void 0:t.toString())||void 0===n?void 0:n.toLowerCase().trim())})),re(function(e){return w(w([],e,!0),a,!0)}));setTimeout(function(){var e;null===(e=Z.current)||void 0===e||e.scrollTo(0,Math.ceil(50*Z.current.scrollHeight/100)),pe(!1),te(!1)},100)}}catch(e){console.log(e)}},[oe,de,ce,J,ve,Ve,u.defaultValue,ge]),Ge=o(function(e,t){var n;t&&xe&&!ve&&"function"==typeof Oe&&(Oe({dataIndex:he}),null===(n=G.current)||void 0===n||n.focus(),pe(!0),Fe&&te(!0))},[xe,ve,Oe,he,Fe]),Ye=function(){var e,t;-1!==je.current&&(null===(e=$.current[je.current])||void 0===e||e.classList.remove("autocomplete-keydown-background")),Pe.current&&(null===(t=Te.current)||void 0===t||t.classList.remove("autocomplete-keydown-viewmore")),je.current=-1,Pe.current=!1,$.current=[]},Je=o(function(){var e;return!!(null===(e=u.additionalData)||void 0===e?void 0:e.relativeSearch)},[null===(d=u.additionalData)||void 0===d?void 0:d.relativeSearch]),Ke=o(function(){var e,t;return"object"==typeof(null===(e=u.additionalData)||void 0===e?void 0:e.relativeSearch)&&"function"==typeof(null===(t=u.additionalData.relativeSearch)||void 0===t?void 0:t.setDefaultValueWithACustomFunction)},[null===(c=u.additionalData)||void 0===c?void 0:c.relativeSearch,null===(g=null===(v=u.additionalData)||void 0===v?void 0:v.relativeSearch)||void 0===g?void 0:g.setDefaultValueWithACustomFunction]),Qe=o(function(){return new Promise(function(e,t){var n,o,r,l=null===(o=null===(n=u.additionalData)||void 0===n?void 0:n.relativeSearch)||void 0===o?void 0:o.setDefaultValueWithACustomFunction(null===(r=G.current)||void 0===r?void 0:r.value);l&&e(l),t([])})},[null===(y=u.additionalData)||void 0===y?void 0:y.relativeSearch,null===(S=null===(b=u.additionalData)||void 0===b?void 0:b.relativeSearch)||void 0===S?void 0:S.setDefaultValueWithACustomFunction]),Xe=o(function(){var e,t=null===(e=u.additionalData)||void 0===e?void 0:e.relativeSearch;if("object"==typeof t&&Object.prototype.hasOwnProperty.call(t,"customRelativeSearchFunction")&&"function"==typeof t.customRelativeSearchFunction)return new Promise(function(e,n){var o,r=t.customRelativeSearchFunction(null===(o=G.current)||void 0===o?void 0:o.value);r&&e(r),n([])})},[null===(C=u.additionalData)||void 0===C?void 0:C.relativeSearch,null===(D=null===(L=u.additionalData)||void 0===L?void 0:L.relativeSearch)||void 0===D?void 0:D.customRelativeSearchFunction]),Ze=o(function(e,t){return e?u.dropdownData.filter(function(n){return t.some(function(t){return n[t].toString().toLowerCase().includes(e)})}):[]},[u.dropdownData]),$e=o(function(){var e,t,n,o=null===(e=u.additionalData)||void 0===e?void 0:e.relativeSearch;if(!o)return[];var r=[];if("boolean"==typeof o&&!0===o)for(var l in u.dropdownData[0])r.push(l);else if("object"==typeof o&&Object.keys(o).length>0){if(Object.prototype.hasOwnProperty.call(o,"includeOnly")&&o.includeOnly.length>0)r=w([],o.includeOnly,!0);else for(var l in u.dropdownData[0])r.push(l);Object.prototype.hasOwnProperty.call(o,"customRelativeSearchFunction")&&o.customRelativeSearchFunction}return r.length>0?Ze(null===(n=null===(t=G.current)||void 0===t?void 0:t.value)||void 0===n?void 0:n.toLowerCase().trim(),r):[]},[u.dropdownData]);return i(function(){var e,t;if(Q){var n=null===(e=Re.current)||void 0===e?void 0:e.style,o=null===(t=G.current)||void 0===t?void 0:t.clientWidth;(null==n?void 0:n.width)!==o+"px"&&Me(),ve&&he!==u.dropdownData.length&&"function"==typeof Oe&&qe(u.dropdownData)}},[he,ve,Oe,qe,u.dropdownData,Q,Me]),i(function(){Q||Ue(u.dropdownData,u.defaultValue,Ve)},[Ue,u.dropdownData,u.defaultValue,Ve,Q]),i(function(){u.dropdownData.length>0&&me(u.dropdownData.length)},[u.dropdownData]),i(function(){var e=function(){Me()};return Q?window.addEventListener("resize",e):window.removeEventListener("resize",e),function(){return window.removeEventListener("resize",e)}},[Me,Q]),i(function(){var e=function(e){var t,n,o,r,l,i,a,u,d,c,s;if(!(oe.length<=0&&27!==(null==e?void 0:e.keyCode)))switch(null==e?void 0:e.keyCode){case 27:return void(Q&&(X(!1),null===(t=G.current)||void 0===t||t.blur(),Ye()));case 13:if(Pe.current)return Ge(null,!0),void(Pe.current=!1);if(null===(n=$.current[je.current])||void 0===n?void 0:n.classList.contains("disable-list-element"))return;var v=oe[je.current];return null!=v&&(Ie(je.current,v,!0),null===(o=$.current[je.current])||void 0===o||o.classList.remove("autocomplete-keydown-background")),void(null===(r=G.current)||void 0===r||r.blur());case 40:if(je.current+1===$.current.length)return void(Te.current&&(null===(l=Te.current)||void 0===l||l.scrollIntoView({behavior:"smooth",block:"nearest"}),null===(i=Te.current)||void 0===i||i.classList.add("autocomplete-keydown-viewmore"),null===(a=$.current[je.current])||void 0===a||a.classList.remove("autocomplete-keydown-background"),Pe.current=!0));if(je.current=je.current+1,null==(s=null===(u=$.current[je.current])||void 0===u?void 0:u.id))return;if(!s.includes("autocomplete-li-element-"))return;if("number"!=typeof Number(s.substring(s.lastIndexOf("-")+1)))return;return void setTimeout(function(){var e,t,n,o,r;null===(e=$.current[je.current])||void 0===e||e.scrollIntoView({behavior:"smooth",block:"nearest"}),null===(t=$.current[je.current])||void 0===t||t.classList.add("autocomplete-keydown-background"),je.current>0&&(null===(n=$.current[je.current-1])||void 0===n||n.classList.remove("autocomplete-keydown-background"),je.current+1===$.current.length&&Se&&(null===(o=Z.current)||void 0===o||o.scrollTo(0,(null===(r=Z.current)||void 0===r?void 0:r.scrollHeight)+10)))},20);case 38:if(je.current<=0)return;if(Pe.current?(null===(d=Te.current)||void 0===d||d.classList.remove("autocomplete-keydown-viewmore"),Pe.current=!1):je.current=je.current-1,null==(s=null===(c=$.current[je.current])||void 0===c?void 0:c.id))return;if(!s.includes("autocomplete-li-element-"))return;if("number"!=typeof Number(s.substring(s.lastIndexOf("-")+1)))return;return void setTimeout(function(){var e,t,n;null===(e=$.current[je.current])||void 0===e||e.scrollIntoView({behavior:"smooth",block:"nearest"}),null===(t=$.current[je.current])||void 0===t||t.classList.add("autocomplete-keydown-background"),null===(n=$.current[je.current+1])||void 0===n||n.classList.remove("autocomplete-keydown-background")},20);default:return}};return Q?window.addEventListener("keydown",e):window.removeEventListener("keydown",e),function(){return window.removeEventListener("keydown",e)}},[oe,Se,Ie,Ge,Q]),i(function(){if(!(ve||he>0)&&u.defaultValue)if(Ve){var e=void 0;if("object"==typeof u.defaultValue){if(Je()){if(!Ke())return;return void Qe().then(function(e){e&&(null==e?void 0:e[u.objectProperty])&&(G.current.value=e[u.objectProperty])}).catch(function(e){return console.log(e)})}e=u.dropdownData.find(function(e){return e[Ve]===u.defaultValue[Ve]})}else{if(Je()){if(!Ke())return;return void Qe().then(function(e){e&&(null==e?void 0:e[u.objectProperty])&&(G.current.value=e[u.objectProperty])}).catch(function(e){return console.log(e)})}e=u.dropdownData.find(function(e){return e[Ve]===u.defaultValue})}e&&(G.current.value=e[Ve])}else{e=void 0;(e="object"==typeof u.defaultValue?u.dropdownData.find(function(e){return e===u.defaultValue[Ve]}):u.dropdownData.find(function(e){return e===u.defaultValue}))&&(G.current.value=e)}},[u.dropdownData,u.defaultValue,Ve,ve,he]),t(n.Fragment,{children:[e(s,{handleOnBlurEvent:function(e){var t,n,o,r,l,i,a,d,c;(null===(n=null===(t=null==e?void 0:e.relatedTarget)||void 0===t?void 0:t.classList)||void 0===n?void 0:n.contains("arrow"))||((null===(r=null===(o=null==e?void 0:e.relatedTarget)||void 0===o?void 0:o.classList)||void 0===r?void 0:r.contains("unorder-list"))?null===(l=G.current)||void 0===l||l.focus():(null===(a=null===(i=null==e?void 0:e.relatedTarget)||void 0===i?void 0:i.classList)||void 0===a?void 0:a.contains("disable-list-element"))||(null===(c=null===(d=null==e?void 0:e.relatedTarget)||void 0===d?void 0:d.classList)||void 0===c?void 0:c.contains("autocomplete-data-list"))||(u.inspectAutoCompleteList?console.warn("You have turned off blur event in autocomplete module which will not close autocomplete list. Hope you know what you are doing. Update to false after inspect is complete."):(X(!1),re([]),Ye(),u.triggerBlurEvent&&"function"==typeof u.triggerBlurEvent&&u.triggerBlurEvent(e))))},handleOnFocusEvent:function(e){var t,n,o;if(!u.isAutoCompleteDisabled){X(!0),Me();var r=null===(t=Re.current)||void 0===t?void 0:t.style,l=null===(n=G.current)||void 0===n?void 0:n.clientWidth;r&&l&&(r.width=l+"px"),u.triggerOnFocusEvent&&"function"==typeof u.triggerOnFocusEvent&&u.triggerOnFocusEvent(e),oe.length<=0&&(ge&&(null===(o=G.current)||void 0===o?void 0:o.value)&&""!==G.current.value?Ne():Ue(u.dropdownData,u.defaultValue,Ve))}},onSearch:u.searchFn?u.searchFn:Ne,ariaInputField:null===(F=u.aria)||void 0===F?void 0:F.ariaInputField,ariaRole:null===(x=u.aria)||void 0===x?void 0:x.ariaRole,ariaRoleDescription:null===(k=u.aria)||void 0===k?void 0:k.ariaRoleDescription,inputFieldClass:null===(V=u.customClass)||void 0===V?void 0:V.inputFieldClass,inputFieldStyle:null===(O=u.customStyle)||void 0===O?void 0:O.inputFieldStyle,isAutoCompleteDisabled:u.isAutoCompleteDisabled,placeHolder:u.placeHolder,ref:G,clearSearch:function(e){u.isAutoCompleteDisabled||(G.current.value="",u.triggerClearSelectionEvent&&"function"==typeof u.triggerClearSelectionEvent&&u.triggerClearSelectionEvent(e))},handleDropdownClick:function(e){var t;u.isAutoCompleteDisabled||(Q?X(!1):null===(t=G.current)||void 0===t||t.focus())},showClearOption:u.showClearOption,showdropDownArrow:u.showdropDownArrow},"input-field"),e("div",f({id:"autoCompleteListContainerId",ref:Re,className:a("auto-complete-list",null===(E=null==u?void 0:u.customClass)||void 0===E?void 0:E.listContainerClass),style:(null===(A=null==u?void 0:u.customStyle)||void 0===A?void 0:A.listContainerStyle)?u.customStyle.listContainerStyle:{},"aria-label":(null===(R=u.aria)||void 0===R?void 0:R.ariaListContainer)?u.aria.ariaListContainer:"Autocomplete list container."},{children:Q&&e("div",f({id:"list-container"},{children:t("ul",f({id:"list-id",ref:Z,onScroll:function(e){var t,n;if(!(oe.length<=0)){if(e.target.offsetHeight+e.target.scrollTop>=e.target.scrollHeight){if(u.totalRecords&&Y.current>=u.totalRecords)return void Ce(!1);ze()}else ce&&e.target.scrollTop<10&&oe.length>de&&He();0===e.target.scrollTop&&(-1!==je.current&&(null===(t=$.current[je.current])||void 0===t||t.classList.remove("autocomplete-keydown-background")),Pe.current&&(null===(n=Te.current)||void 0===n||n.classList.remove("autocomplete-keydown-viewmore")),je.current=-1,Pe.current=!1)}},className:a("unorder-list",null===(T=null==u?void 0:u.customClass)||void 0===T?void 0:T.dropdownUnorderedListClass),style:(null===(j=null==u?void 0:u.customStyle)||void 0===j?void 0:j.dropdownUnorderedListStyle)?u.customStyle.dropdownUnorderedListStyle:{},tabIndex:0,"aria-label":(null===(P=u.aria)||void 0===P?void 0:P.ariaULList)?u.aria.ariaULList:"Autocomplete unordered list"},{children:[oe.length>0&&oe.map(function(t,n){var o,r;return Ve||"object"!=typeof t?e(p,{index:n,onSelect:Ie,data:t,disableListFn:Ee,disableProperty:ke,dropdownListClass:null===(o=u.customClass)||void 0===o?void 0:o.dropdownListClass,dropdownListStyle:null===(r=u.customStyle)||void 0===r?void 0:r.dropdownListStyle,objectProperty:Ve,ref:function(e){$.current[n]=e}},n):null}),ee&&Fe&&!u.isCustomSpinner&&e("li",f({className:"auto-complete-list-spinner"},{children:e("span",{className:"autocomplete-plugin-loader"})})),ee&&Fe&&u.isCustomSpinner&&e("li",f({className:"auto-complete-list-spinner"},{children:e("span",{className:(null===(M=null==u?void 0:u.customClass)||void 0===M?void 0:M.customSpinnerClass)?u.customClass.customSpinnerClass:"",style:(null===(I=null==u?void 0:u.customStyle)||void 0===I?void 0:I.customSpinnerStyle)?u.customStyle.customSpinnerStyle:{}})})),!ee&&oe.length<=0&&e("li",f({className:a("autocomplete-data-list noSearchResult",null===(N=null==u?void 0:u.customClass)||void 0===N?void 0:N.noResultClass),style:(null===(H=null==u?void 0:u.customStyle)||void 0===H?void 0:H.noResultStyle)?u.customStyle.noResultStyle:{},"aria-label":(null===(B=u.aria)||void 0===B?void 0:B.ariaNoSearchResult)?u.aria.ariaNoSearchResult:"No search result."},{children:ue})),De&&!ve&&!ee&&xe&&Se&&e("li",f({className:a("autocomplete-data-list view-more",null===(z=null==u?void 0:u.customClass)||void 0===z?void 0:z.viewMoreClass),style:(null===(W=null==u?void 0:u.customStyle)||void 0===W?void 0:W.viewMoreStyle)?u.customStyle.viewMoreStyle:{},"aria-label":(null===(U=u.aria)||void 0===U?void 0:U.ariaViewMore)?u.aria.ariaViewMore:"View more results",tabIndex:0,onClick:function(e){return Ge(e,!0)},ref:Te},{children:Le}))]}))}))}))]})}!function(e,t){void 0===t&&(t={});var n=t.insertAt;if(e&&"undefined"!=typeof document){var o=document.head||document.getElementsByTagName("head")[0],r=document.createElement("style");r.type="text/css","top"===n&&o.firstChild?o.insertBefore(r,o.firstChild):o.appendChild(r),r.styleSheet?r.styleSheet.cssText=e:r.appendChild(document.createTextNode(e))}}("\n.auto-complete-textfield-container {\n width: 100%;\n height: inherit;\n }\n \n .auto-complete-remove-selection {\n display: inline-block;\n margin-top: 5px;\n margin-left: -25px;\n cursor: pointer;\n }\n \n .auto-complete-textfield {\n width: 100%;\n height: 2rem;\n outline: none;\n border: 1px solid grey;\n border-radius: 5px;\n cursor: text;\n }\n \n .auto-complete-list {\n position: absolute;\n background-color: #FFFFFF;\n }\n \n .field-container {\n width: inherit;\n }\n \n .unorder-list {\n list-style: none;\n max-height: 20rem;\n overflow: auto;\n margin: 0;\n padding: 0;\n text-align: center;\n border: 1px solid #E8E8E8;\n box-shadow: 0 2px 3px -1px rgba(0, 0, 0, 0.1);\n color: #000000;\n }\n \n .autocomplete-data-list {\n padding: 1rem;\n }\n \n .autocomplete-data-list:hover {\n background-color: #EEEDED;\n cursor: pointer;\n }\n \n .showDropdown {\n display: block;\n }\n \n .autocomplete-plugin-loader {\n width: 2rem;\n height: 2rem;\n border: 3px solid #D4D3D3;\n border-bottom-color: transparent;\n border-radius: 50%;\n display: inline-block;\n box-sizing: border-box;\n animation: rotation 1s linear infinite;\n position: relative;\n }\n \n .auto-complete-textfield:disabled {\n background-color: #E7E7E7;\n border: 1px solid #D5D5D5;\n cursor: default;\n }\n \n .auto-complete-list-spinner {\n padding: 1rem;\n }\n \n @keyframes rotation {\n 0% {\n transform: rotate(0deg);\n }\n \n 100% {\n transform: rotate(360deg);\n }\n }\n \n .arrow {\n height: 0;\n width: 0;\n border: 4px solid transparent;\n display: inline-block;\n margin-top: 5px;\n margin-left: -20px;\n cursor: pointer;\n }\n \n .arrow.down {\n border-top-color: #000;\n }\n \n .disable-list-element {\n pointer-events:none;\n opacity:0.5;\n background-color: #E5E5E5;\n }\n \n ::-webkit-input-placeholder {\n text-transform: capitalize;\n }\n \n ::-moz-placeholder {\n text-transform: capitalize;\n }\n \n ::-ms-placeholder {\n text-transform: capitalize;\n }\n \n .view-more {\n cursor: pointer;\n background-color: #f6f5f5;\n letter-spacing: 0.5px;\n }\n \n .autocomplete-keydown-background {\n background-color: #F4F4F4;\n }\n \n .autocomplete-keydown-viewmore {\n background-color: #c8c8c8;\n }");var y=function(){return y=Object.assign||function(e){for(var t,n=1,o=arguments.length;n<o;n++)for(var r in t=arguments[n])Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r]);return e},y.apply(this,arguments)};function b(n){var o,l,i,a,u,c,s,v,p=r(null),f=n.customClass?n.customClass:{},h=n.customStyle?n.customStyle:{},m=n.inputLabel?n.inputLabel:"Select a value",w=function(e,t){if(e||t){var n=e||"",o=null==t?void 0:t.split(" ");if(!o)return n;for(var r=0,l=o;r<l.length;r++){n=n+" "+l[r]}return n}};return t("div",y({className:w("auto-complete-textfield-container",f.parentContainerClass),ref:p,style:h.parentContainerStyle?h.parentContainerStyle:{}},{children:[n.showInputlabel&&e("div",y({className:w("label-container",f.inputLabelContainerClass),style:h.inputLabelContainerStyle?h.inputLabelContainerStyle:{}},{children:e(d,{inputLabel:m,ariaInputLabel:null===(o=n.aria)||void 0===o?void 0:o.ariaInputLabel,inputLabelClass:null===(l=n.customClass)||void 0===l?void 0:l.inputLabelClass,inputLabelStyle:null===(i=n.customStyle)||void 0===i?void 0:i.inputLabelStyle},"label-key")})),e(g,{dropdownData:n.dropdownData,objectProperty:n.objectProperty,initialVisibleData:n.initialVisibleData,placeHolder:n.placeHolder,inputFieldClass:null===(a=n.customClass)||void 0===a?void 0:a.inputFieldClass,inputFieldStyle:null===(u=n.customStyle)||void 0===u?void 0:u.inputFieldStyle,isAutoCompleteDisabled:n.isAutoCompleteDisabled,ariaInputField:null===(c=n.aria)||void 0===c?void 0:c.ariaInputField,ariaRole:null===(s=n.aria)||void 0===s?void 0:s.ariaRole,ariaRoleDescription:null===(v=n.aria)||void 0===v?void 0:v.ariaRoleDescription,showClearOption:n.showClearOption,showdropDownArrow:n.showdropDownArrow,inspectAutoCompleteList:n.inspectAutoCompleteList,broadcastSelectedValue:n.broadcastSelectedValue,disableListFn:n.disableListFn,disableProperty:n.disableProperty,triggerClearSelectionEvent:n.triggerClearSelectionEvent,customStyle:n.customStyle,aria:n.aria,customClass:n.customClass,isCustomSpinner:n.isCustomSpinner,noSearchResultMessage:n.noSearchResultMessage,showLoadingSpinner:n.showLoadingSpinner,defaultValue:n.defaultValue,isScrollThresholdRequired:n.isScrollThresholdRequired,triggerBlurEvent:n.triggerBlurEvent,triggerApiLoadEvent:n.triggerApiLoadEvent,isApiLoad:n.isApiLoad,scrollThreshold:n.scrollThreshold,triggerOnFocusEvent:n.triggerOnFocusEvent,totalRecords:n.totalRecords,searchFn:n.searchFn,optViewMoreOnlyForApiCall:n.optViewMoreOnlyForApiCall,showViewMore:n.showViewMore,viewMoreText:n.viewMoreText,additionalData:n.additionalData},"input-field")]}))}export{b as Autocomplete};