@pro6pp/infer-js 0.0.2-beta.13 → 0.0.2-beta.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -1
- package/dist/index.cjs +28 -15
- package/dist/index.d.mts +6 -6
- package/dist/index.d.ts +6 -6
- package/dist/index.global.js +28 -15
- package/dist/index.mjs +28 -15
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -114,7 +114,6 @@ You can then target the following classes in your CSS:
|
|
|
114
114
|
| :----------------------- | :-------------------------------------------------------- |
|
|
115
115
|
| `.pro6pp-wrapper` | The container element wrapping the input and dropdown. |
|
|
116
116
|
| `.pro6pp-input` | The input element itself. |
|
|
117
|
-
| `.pro6pp-loader` | The loading spinner shown during API requests. |
|
|
118
117
|
| `.pro6pp-dropdown` | The `<ul>` list containing the suggestions. |
|
|
119
118
|
| `.pro6pp-item` | A single suggestion item (`<li>`). |
|
|
120
119
|
| `.pro6pp-item--active` | The currently highlighted item (for keyboard navigation). |
|
|
@@ -122,3 +121,5 @@ You can then target the following classes in your CSS:
|
|
|
122
121
|
| `.pro6pp-item__subtitle` | The secondary text (e.g., city or result count). |
|
|
123
122
|
| `.pro6pp-item__chevron` | The icon indicating a folder/expandable result. |
|
|
124
123
|
| `.pro6pp-no-results` | The message shown when no suggestions are found. |
|
|
124
|
+
| `.pro6pp-loader-item` | The list item containing the loading spinner and text. |
|
|
125
|
+
| `.pro6pp-mini-spinner` | The spinner icon inside the loader item. |
|
package/dist/index.cjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"use strict";var m=Object.defineProperty,
|
|
1
|
+
"use strict";var m=Object.defineProperty,_=Object.defineProperties,k=Object.getOwnPropertyDescriptor,R=Object.getOwnPropertyDescriptors,F=Object.getOwnPropertyNames,I=Object.getOwnPropertySymbols;var T=Object.prototype.hasOwnProperty,D=Object.prototype.propertyIsEnumerable;var L=(o,e,t)=>e in o?m(o,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):o[e]=t,f=(o,e)=>{for(var t in e||(e={}))T.call(e,t)&&L(o,t,e[t]);if(I)for(var t of I(e))D.call(e,t)&&L(o,t,e[t]);return o},b=(o,e)=>_(o,R(e));var $=(o,e)=>{for(var t in e)m(o,t,{get:e[t],enumerable:!0})},N=(o,e,t,i)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of F(e))!T.call(o,n)&&n!==t&&m(o,n,{get:()=>e[n],enumerable:!(i=k(e,n))||i.enumerable});return o};var B=o=>N(m({},"__esModule",{value:!0}),o);var H={};$(H,{InferJS:()=>y,attach:()=>U});module.exports=B(H);var g={API_URL:"https://api.pro6pp.nl/v2",LIMIT:20,DEBOUNCE_MS:150,MIN_DEBOUNCE_MS:50,MAX_RETRIES:0},S={DIGITS_1_3:/^[0-9]{1,3}$/,STREET_NUMBER_PREFIX:/^(\d+)\s*,\s*$/},A={query:"",stage:null,cities:[],streets:[],suggestions:[],isValid:!1,value:null,isError:!1,isLoading:!1,hasMore:!1,selectedSuggestionIndex:-1},x=class{constructor(e){this.abortController=null;this.country=e.country,this.authKey=e.authKey,this.explicitApiUrl=e.apiUrl,this.baseLimit=e.limit||g.LIMIT,this.currentLimit=this.baseLimit;let t=e.maxRetries!==void 0?e.maxRetries:g.MAX_RETRIES;this.maxRetries=Math.max(0,Math.min(t,10)),this.fetcher=e.fetcher||((r,s)=>fetch(r,s)),this.onStateChange=e.onStateChange||(()=>{}),this.onSelect=e.onSelect||(()=>{}),this.state=f({},A);let i=e.debounceMs!==void 0?e.debounceMs:g.DEBOUNCE_MS,n=Math.max(i,g.MIN_DEBOUNCE_MS);this.debouncedFetch=this.debounce(r=>this.executeFetch(r),n)}handleInput(e){this.currentLimit=this.baseLimit;let t=this.state.stage==="final"&&e!==this.state.query;this.updateState({query:e,isValid:!1,value:null,isLoading:!!e.trim(),selectedSuggestionIndex:-1,hasMore:!1,stage:t?null:this.state.stage}),t&&this.onSelect(null),this.debouncedFetch(e)}loadMore(){this.state.isLoading||(this.currentLimit+=this.baseLimit,this.updateState({isLoading:!0}),this.executeFetch(this.state.query))}handleKeyDown(e){let t=e.target;if(!t)return;let i=this.state.cities.length+this.state.streets.length+this.state.suggestions.length;if(i>0){if(e.key==="ArrowDown"){e.preventDefault();let r=this.state.selectedSuggestionIndex+1;r>=i&&(r=0),this.updateState({selectedSuggestionIndex:r});return}if(e.key==="ArrowUp"){e.preventDefault();let r=this.state.selectedSuggestionIndex-1;r<0&&(r=i-1),this.updateState({selectedSuggestionIndex:r});return}if(e.key==="Enter"&&this.state.selectedSuggestionIndex>=0){e.preventDefault();let s=[...this.state.cities,...this.state.streets,...this.state.suggestions][this.state.selectedSuggestionIndex];s&&(this.selectItem(s),this.updateState({selectedSuggestionIndex:-1}));return}}let n=t.value;if(e.key===" "&&this.shouldAutoInsertComma(n)){e.preventDefault();let r=`${n.trim()}, `;this.updateQueryAndFetch(r)}}selectItem(e){this.debouncedFetch.cancel(),this.abortController&&this.abortController.abort();let t=typeof e=="string"?e:e.label,i=t;typeof e!="string"&&typeof e.value=="string"&&(i=e.value);let n=typeof e!="string"&&typeof e.value=="object"?e.value:void 0,r=!!n&&Object.keys(n).length>0;if(this.state.stage==="final"||r){let u=t;if(n&&Object.keys(n).length>0){let{street:p,street_number:a,city:c,addition:l}=n;if(p&&a&&c){let h=l?` ${l}`:"";u=`${p} ${a}${h}, ${c}`}}return this.finishSelection(u,n),!0}let s=typeof e!="string"?e.subtitle:null;return this.processSelection(i,s),!1}shouldAutoInsertComma(e){if(!e.includes(",")&&S.DIGITS_1_3.test(e.trim()))return!0;if(this.state.stage==="street_number"){let i=this.getCurrentFragment(e);return S.DIGITS_1_3.test(i)}return!1}finishSelection(e,t){this.updateState({query:e,suggestions:[],cities:[],streets:[],isValid:!0,value:t||null,stage:"final",hasMore:!1}),this.onSelect(t||e)}processSelection(e,t){let{stage:i,query:n}=this.state,r=n;if(t&&(i==="city"||i==="street"||i==="mixed")){if(i==="city")r=`${t}, ${e}, `;else{let a=this.getQueryPrefix(n),c=!a||!a.includes(t),l=a;if(a&&t){let h=a.match(S.STREET_NUMBER_PREFIX);if(h){let C=h[1];t.startsWith(C)&&(l="")}}c?r=l?`${l} ${e}, ${t}, `:`${e}, ${t}, `:r=l?`${l} ${e}, `:`${e}, `}this.updateQueryAndFetch(r);return}if(i==="direct"||i==="addition"){this.finishSelection(e);return}!n.includes(",")&&(i==="city"||i==="street"||i==="street_number_first")?r=`${e}, `:(r=this.replaceLastSegment(n,e),i!=="street_number"&&(r+=", ")),this.updateQueryAndFetch(r)}executeFetch(e,t=0){var a,c;let i=(e||"").toString();if(!i.trim()){(a=this.abortController)==null||a.abort(),this.resetState();return}t===0&&(this.updateState({isError:!1}),this.abortController&&this.abortController.abort(),this.abortController=new AbortController);let n=(c=this.abortController)==null?void 0:c.signal,r=this.explicitApiUrl?this.explicitApiUrl:`${g.API_URL}/infer/${this.country.toLowerCase()}`,s=new URLSearchParams({query:i,limit:this.currentLimit.toString()});this.explicitApiUrl&&s.append("country",this.country.toLowerCase()),this.authKey&&s.set("authKey",this.authKey);let u=r.includes("?")?"&":"?",p=`${r}${u}${s.toString()}`;this.fetcher(p,{signal:n}).then(l=>{if(!l.ok){if(t<this.maxRetries&&(l.status>=500||l.status===429))return this.retry(e,t,n);throw new Error("Network error")}return l.json()}).then(l=>{l&&this.mapResponseToState(l)}).catch(l=>{if(l.name!=="AbortError"){if(t<this.maxRetries)return this.retry(e,t,n);this.updateState({isError:!0,isLoading:!1})}})}retry(e,t,i){if(i!=null&&i.aborted)return;let n=Math.pow(2,t)*200;setTimeout(()=>{i!=null&&i.aborted||this.executeFetch(e,t+1)},n)}mapResponseToState(e){var u,p,a,c;let t={stage:e.stage,isLoading:!1},i=e.suggestions||[],n=[],r=new Set;for(let l of i){let h=`${l.label}|${l.subtitle||""}|${JSON.stringify(l.value||{})}`;r.has(h)||(r.add(h),n.push(l))}let s=n.length+(((u=e.cities)==null?void 0:u.length)||0)+(((p=e.streets)==null?void 0:p.length)||0);t.hasMore=s>=this.currentLimit,e.stage==="mixed"?(t.cities=e.cities||[],t.streets=e.streets||[],((a=t.cities)==null?void 0:a.length)===0&&((c=t.streets)==null?void 0:c.length)===0?t.suggestions=n:t.suggestions=[]):(t.suggestions=n,t.cities=[],t.streets=[]),t.isValid=e.stage==="final",this.updateState(t),t.isValid&&n.length===1&&this.selectItem(n[0])}updateQueryAndFetch(e){this.updateState({query:e,suggestions:[],cities:[],streets:[],isValid:!1,value:null,isLoading:!0,hasMore:!1}),this.debouncedFetch(e)}replaceLastSegment(e,t){let i=e.lastIndexOf(",");return i===-1?t:`${e.slice(0,i+1)} ${t}`.trim()}getQueryPrefix(e){let t=e.lastIndexOf(",");return t===-1?"":e.slice(0,t+1).trimEnd()}getCurrentFragment(e){var t;return((t=e.split(",").slice(-1)[0])!=null?t:"").trim()}resetState(){this.updateState(b(f({},A),{query:this.state.query}))}updateState(e){this.state=f(f({},this.state),e),this.onStateChange(this.state)}debounce(e,t){let i,n=(...r)=>{i&&clearTimeout(i),i=setTimeout(()=>e.apply(this,r),t)};return n.cancel=()=>{i&&(clearTimeout(i),i=void 0)},n}};function M(o,e){if(!e||!o)return[{text:o,match:!1}];let t=[],i=o.toLowerCase(),n=e.toLowerCase(),r=0,s=0;for(let a=0;a<o.length;a++){if(!(r<e.length&&i[a]===n[r]))continue;a>s&&t.push({text:o.slice(s,a),match:!1}),t.push({text:o[a],match:!0}),r++,s=a+1}return s<o.length&&t.push({text:o.slice(s),match:!1}),r===e.length?t:[{text:o,match:!1}]}var w=`
|
|
2
2
|
.pro6pp-wrapper {
|
|
3
3
|
position: relative;
|
|
4
4
|
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
|
|
@@ -71,17 +71,6 @@
|
|
|
71
71
|
background-color: #f3f4f6;
|
|
72
72
|
}
|
|
73
73
|
|
|
74
|
-
.pro6pp-loader {
|
|
75
|
-
width: 20px;
|
|
76
|
-
height: 20px;
|
|
77
|
-
margin: 0 8px;
|
|
78
|
-
border: 2px solid #e0e0e0;
|
|
79
|
-
border-top-color: #6b7280;
|
|
80
|
-
border-radius: 50%;
|
|
81
|
-
animation: pro6pp-spin 0.6s linear infinite;
|
|
82
|
-
flex-shrink: 0;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
74
|
.pro6pp-dropdown {
|
|
86
75
|
position: absolute;
|
|
87
76
|
top: 100%;
|
|
@@ -183,6 +172,27 @@
|
|
|
183
172
|
touch-action: manipulation;
|
|
184
173
|
}
|
|
185
174
|
|
|
175
|
+
.pro6pp-loader-item {
|
|
176
|
+
padding: 10px 12px;
|
|
177
|
+
color: #6b7280;
|
|
178
|
+
font-size: 0.875rem;
|
|
179
|
+
display: flex;
|
|
180
|
+
align-items: center;
|
|
181
|
+
justify-content: center;
|
|
182
|
+
gap: 8px;
|
|
183
|
+
background-color: #f9fafb;
|
|
184
|
+
border-top: 1px solid #f3f4f6;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
.pro6pp-mini-spinner {
|
|
188
|
+
width: 14px;
|
|
189
|
+
height: 14px;
|
|
190
|
+
border: 2px solid #e5e7eb;
|
|
191
|
+
border-top-color: #6b7280;
|
|
192
|
+
border-radius: 50%;
|
|
193
|
+
animation: pro6pp-spin 0.6s linear infinite;
|
|
194
|
+
}
|
|
195
|
+
|
|
186
196
|
@media (max-width: 640px) {
|
|
187
197
|
.pro6pp-input {
|
|
188
198
|
font-size: 16px;
|
|
@@ -208,13 +218,16 @@
|
|
|
208
218
|
@keyframes pro6pp-spin {
|
|
209
219
|
to { transform: rotate(360deg); }
|
|
210
220
|
}
|
|
211
|
-
`;var y=class{constructor(e,t){this.isOpen=!1;var r;let i=typeof e=="string"?document.querySelector(e):e;if(!i)throw new Error("InferJS: Target element not found.");if(this.noResultsText=t.noResultsText||"No results found",this.
|
|
221
|
+
`;var y=class{constructor(e,t){this.isOpen=!1;var r;let i=typeof e=="string"?document.querySelector(e):e;if(!i)throw new Error("InferJS: Target element not found.");if(this.noResultsText=t.noResultsText||"No results found",this.loadingText=t.loadingText||"Loading more...",this.showClearButton=t.showClearButton!==!1,this.useDefaultStyles=t.style!=="none",this.useDefaultStyles&&this.injectStyles(),this.wrapper=document.createElement("div"),this.wrapper.className="pro6pp-wrapper",i instanceof HTMLInputElement?(this.input=i,(r=this.input.parentNode)==null||r.insertBefore(this.wrapper,this.input),this.wrapper.appendChild(this.input)):(i.appendChild(this.wrapper),this.input=document.createElement("input"),this.input.type="text",t.placeholder&&(this.input.placeholder=t.placeholder),this.wrapper.appendChild(this.input)),this.input.setAttribute("autocomplete","off"),this.input.setAttribute("autocorrect","off"),this.input.setAttribute("autocapitalize","none"),this.input.setAttribute("spellcheck","false"),this.input.setAttribute("inputmode","search"),this.input.setAttribute("enterkeyhint","search"),this.useDefaultStyles&&this.input.classList.add("pro6pp-input"),t.inputClass){let s=t.inputClass.split(" ");this.input.classList.add(...s)}let n=document.createElement("div");n.className="pro6pp-input-addons",this.wrapper.appendChild(n),this.clearButton=document.createElement("button"),this.clearButton.type="button",this.clearButton.className="pro6pp-clear-button",this.clearButton.setAttribute("aria-label","Clear input"),this.clearButton.style.display="none",this.clearButton.innerHTML=`
|
|
212
222
|
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
|
|
213
223
|
<line x1="18" y1="6" x2="6" y2="18"></line>
|
|
214
224
|
<line x1="6" y1="6" x2="18" y2="18"></line>
|
|
215
225
|
</svg>
|
|
216
|
-
`,n.appendChild(this.clearButton),this.dropdown=document.createElement("div"),this.dropdown.className="pro6pp-dropdown",this.dropdown.style.display="none",this.wrapper.appendChild(this.dropdown),this.list=document.createElement("ul"),this.list.className="pro6pp-list",this.list.setAttribute("role","listbox"),this.dropdown.appendChild(this.list),this.
|
|
226
|
+
`,n.appendChild(this.clearButton),this.dropdown=document.createElement("div"),this.dropdown.className="pro6pp-dropdown",this.dropdown.style.display="none",this.wrapper.appendChild(this.dropdown),this.list=document.createElement("ul"),this.list.className="pro6pp-list",this.list.setAttribute("role","listbox"),this.dropdown.appendChild(this.list),this.dropdownLoader=document.createElement("div"),this.dropdownLoader.className="pro6pp-loader-item",this.dropdownLoader.style.display="none",this.dropdownLoader.innerHTML=`
|
|
227
|
+
<div class="pro6pp-mini-spinner"></div>
|
|
228
|
+
<span>${this.loadingText}</span>
|
|
229
|
+
`,this.dropdown.appendChild(this.dropdownLoader),this.core=new x(b(f({},t),{onStateChange:s=>{this.render(s),t.onStateChange&&t.onStateChange(s)},onSelect:s=>{typeof s=="string"?this.input.value=s:s&&typeof s=="object"&&(this.input.value=this.core.state.query),t.onSelect&&t.onSelect(s)}})),this.observer=new IntersectionObserver(s=>{s[0].isIntersecting&&this.core.state.hasMore&&!this.core.state.isLoading&&this.core.loadMore()},{threshold:.1}),this.bindEvents()}get value(){return this.core.state.value||null}set value(e){if(!e)return;let t=`${e.street} ${e.street_number}, ${e.city}`;this.core.selectItem({label:t,value:e})}injectStyles(){let e="pro6pp-styles";if(!document.getElementById(e)){let t=document.createElement("style");t.id=e,t.textContent=w,document.head.appendChild(t)}}bindEvents(){this.input.addEventListener("input",e=>{let t=e.target.value;this.isOpen=!0,this.core.handleInput(t)}),this.input.addEventListener("keydown",e=>{this.core.handleKeyDown(e)}),this.clearButton.addEventListener("click",()=>{this.core.handleInput(""),this.input.focus()}),document.addEventListener("mousedown",e=>{this.wrapper.contains(e.target)||(this.isOpen=!1,this.dropdown.style.display="none")}),this.input.addEventListener("focus",()=>{this.isOpen=!0,this.render(this.core.state)})}render(e){this.input.value!==e.query&&(this.input.value=e.query),this.showClearButton&&(this.clearButton.style.display=e.query.length>0?"flex":"none"),this.list.innerHTML="";let t=[...e.cities,...e.streets,...e.suggestions],i=t.length>0,n=!e.isLoading&&!e.isError&&e.query.length>0&&!i&&!e.isValid;if(!(this.isOpen&&(i||e.isLoading||n))){this.dropdown.style.display="none";return}if(this.dropdown.style.display="block",e.isLoading&&i?this.dropdownLoader.style.display="flex":this.dropdownLoader.style.display="none",e.isLoading&&!i){let s=document.createElement("li");s.className="pro6pp-no-results",s.textContent="Searching...",this.list.appendChild(s),this.dropdownLoader.style.display="none";return}if(n){let s=document.createElement("li");s.className="pro6pp-no-results",s.textContent=this.noResultsText,this.list.appendChild(s);return}if(t.forEach((s,u)=>{if(!s.label)return;let p=document.createElement("li");p.className="pro6pp-item",u===e.selectedSuggestionIndex&&p.classList.add("pro6pp-item--active"),p.setAttribute("role","option"),p.setAttribute("aria-selected",u===e.selectedSuggestionIndex?"true":"false");let a=document.createElement("span");a.className="pro6pp-item__label",M(s.label,e.query).forEach(({text:d,match:E})=>{if(E){let v=document.createElement("strong");v.className="pro6pp-item__label--match",v.textContent=d,a.appendChild(v)}else a.appendChild(document.createTextNode(d))}),p.appendChild(a);let l=s.count!==void 0&&s.count!==null?s.count:"",h=s.subtitle||l;if(h!==""){let d=document.createElement("span");d.className="pro6pp-item__subtitle",d.textContent=`, ${h}`,p.appendChild(d)}if(s.value===void 0||s.value===null){let d=document.createElement("div");d.className="pro6pp-item__chevron",d.innerHTML=`
|
|
217
230
|
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
|
|
218
231
|
<polyline points="9 18 15 12 9 6"></polyline>
|
|
219
232
|
</svg>
|
|
220
|
-
`,
|
|
233
|
+
`,p.appendChild(d)}p.onmousedown=d=>d.preventDefault(),p.onclick=d=>{d.stopPropagation(),this.core.selectItem(s)?(this.isOpen=!1,this.dropdown.style.display="none"):setTimeout(()=>this.input.focus(),0)},this.list.appendChild(p)}),e.hasMore&&!e.isLoading){let s=document.createElement("li");s.style.height="1px",s.style.opacity="0",this.list.appendChild(s),this.observer.observe(s)}}};function U(o,e){return new y(o,e)}0&&(module.exports={InferJS,attach});
|
package/dist/index.d.mts
CHANGED
|
@@ -25,10 +25,10 @@ interface InferJSConfig extends InferConfig {
|
|
|
25
25
|
*/
|
|
26
26
|
noResultsText?: string;
|
|
27
27
|
/**
|
|
28
|
-
* The text to show on the
|
|
29
|
-
* @default '
|
|
28
|
+
* The text to show on the bottom loading indicator.
|
|
29
|
+
* @default 'Loading more...'
|
|
30
30
|
*/
|
|
31
|
-
|
|
31
|
+
loadingText?: string;
|
|
32
32
|
/**
|
|
33
33
|
* If true, shows a clear button when the input is not empty.
|
|
34
34
|
* @default true
|
|
@@ -45,14 +45,14 @@ declare class InferJS {
|
|
|
45
45
|
private list;
|
|
46
46
|
private dropdown;
|
|
47
47
|
private wrapper;
|
|
48
|
-
private
|
|
48
|
+
private dropdownLoader;
|
|
49
49
|
private clearButton;
|
|
50
|
-
private loadMoreButton;
|
|
51
50
|
private useDefaultStyles;
|
|
52
51
|
private noResultsText;
|
|
53
|
-
private
|
|
52
|
+
private loadingText;
|
|
54
53
|
private showClearButton;
|
|
55
54
|
private isOpen;
|
|
55
|
+
private observer;
|
|
56
56
|
/**
|
|
57
57
|
* Initializes the Infer logic on a target element.
|
|
58
58
|
* @param target Either a CSS selector string or a direct HTMLElement.
|
package/dist/index.d.ts
CHANGED
|
@@ -25,10 +25,10 @@ interface InferJSConfig extends InferConfig {
|
|
|
25
25
|
*/
|
|
26
26
|
noResultsText?: string;
|
|
27
27
|
/**
|
|
28
|
-
* The text to show on the
|
|
29
|
-
* @default '
|
|
28
|
+
* The text to show on the bottom loading indicator.
|
|
29
|
+
* @default 'Loading more...'
|
|
30
30
|
*/
|
|
31
|
-
|
|
31
|
+
loadingText?: string;
|
|
32
32
|
/**
|
|
33
33
|
* If true, shows a clear button when the input is not empty.
|
|
34
34
|
* @default true
|
|
@@ -45,14 +45,14 @@ declare class InferJS {
|
|
|
45
45
|
private list;
|
|
46
46
|
private dropdown;
|
|
47
47
|
private wrapper;
|
|
48
|
-
private
|
|
48
|
+
private dropdownLoader;
|
|
49
49
|
private clearButton;
|
|
50
|
-
private loadMoreButton;
|
|
51
50
|
private useDefaultStyles;
|
|
52
51
|
private noResultsText;
|
|
53
|
-
private
|
|
52
|
+
private loadingText;
|
|
54
53
|
private showClearButton;
|
|
55
54
|
private isOpen;
|
|
55
|
+
private observer;
|
|
56
56
|
/**
|
|
57
57
|
* Initializes the Infer logic on a target element.
|
|
58
58
|
* @param target Either a CSS selector string or a direct HTMLElement.
|
package/dist/index.global.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"use strict";var Pro6PP=(()=>{var m=Object.defineProperty,
|
|
1
|
+
"use strict";var Pro6PP=(()=>{var m=Object.defineProperty,_=Object.defineProperties,k=Object.getOwnPropertyDescriptor,R=Object.getOwnPropertyDescriptors,F=Object.getOwnPropertyNames,I=Object.getOwnPropertySymbols;var T=Object.prototype.hasOwnProperty,D=Object.prototype.propertyIsEnumerable;var L=(o,e,t)=>e in o?m(o,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):o[e]=t,f=(o,e)=>{for(var t in e||(e={}))T.call(e,t)&&L(o,t,e[t]);if(I)for(var t of I(e))D.call(e,t)&&L(o,t,e[t]);return o},b=(o,e)=>_(o,R(e));var $=(o,e)=>{for(var t in e)m(o,t,{get:e[t],enumerable:!0})},N=(o,e,t,i)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of F(e))!T.call(o,n)&&n!==t&&m(o,n,{get:()=>e[n],enumerable:!(i=k(e,n))||i.enumerable});return o};var B=o=>N(m({},"__esModule",{value:!0}),o);var H={};$(H,{InferJS:()=>y,attach:()=>U});var g={API_URL:"https://api.pro6pp.nl/v2",LIMIT:20,DEBOUNCE_MS:150,MIN_DEBOUNCE_MS:50,MAX_RETRIES:0},S={DIGITS_1_3:/^[0-9]{1,3}$/,STREET_NUMBER_PREFIX:/^(\d+)\s*,\s*$/},A={query:"",stage:null,cities:[],streets:[],suggestions:[],isValid:!1,value:null,isError:!1,isLoading:!1,hasMore:!1,selectedSuggestionIndex:-1},x=class{constructor(e){this.abortController=null;this.country=e.country,this.authKey=e.authKey,this.explicitApiUrl=e.apiUrl,this.baseLimit=e.limit||g.LIMIT,this.currentLimit=this.baseLimit;let t=e.maxRetries!==void 0?e.maxRetries:g.MAX_RETRIES;this.maxRetries=Math.max(0,Math.min(t,10)),this.fetcher=e.fetcher||((r,s)=>fetch(r,s)),this.onStateChange=e.onStateChange||(()=>{}),this.onSelect=e.onSelect||(()=>{}),this.state=f({},A);let i=e.debounceMs!==void 0?e.debounceMs:g.DEBOUNCE_MS,n=Math.max(i,g.MIN_DEBOUNCE_MS);this.debouncedFetch=this.debounce(r=>this.executeFetch(r),n)}handleInput(e){this.currentLimit=this.baseLimit;let t=this.state.stage==="final"&&e!==this.state.query;this.updateState({query:e,isValid:!1,value:null,isLoading:!!e.trim(),selectedSuggestionIndex:-1,hasMore:!1,stage:t?null:this.state.stage}),t&&this.onSelect(null),this.debouncedFetch(e)}loadMore(){this.state.isLoading||(this.currentLimit+=this.baseLimit,this.updateState({isLoading:!0}),this.executeFetch(this.state.query))}handleKeyDown(e){let t=e.target;if(!t)return;let i=this.state.cities.length+this.state.streets.length+this.state.suggestions.length;if(i>0){if(e.key==="ArrowDown"){e.preventDefault();let r=this.state.selectedSuggestionIndex+1;r>=i&&(r=0),this.updateState({selectedSuggestionIndex:r});return}if(e.key==="ArrowUp"){e.preventDefault();let r=this.state.selectedSuggestionIndex-1;r<0&&(r=i-1),this.updateState({selectedSuggestionIndex:r});return}if(e.key==="Enter"&&this.state.selectedSuggestionIndex>=0){e.preventDefault();let s=[...this.state.cities,...this.state.streets,...this.state.suggestions][this.state.selectedSuggestionIndex];s&&(this.selectItem(s),this.updateState({selectedSuggestionIndex:-1}));return}}let n=t.value;if(e.key===" "&&this.shouldAutoInsertComma(n)){e.preventDefault();let r=`${n.trim()}, `;this.updateQueryAndFetch(r)}}selectItem(e){this.debouncedFetch.cancel(),this.abortController&&this.abortController.abort();let t=typeof e=="string"?e:e.label,i=t;typeof e!="string"&&typeof e.value=="string"&&(i=e.value);let n=typeof e!="string"&&typeof e.value=="object"?e.value:void 0,r=!!n&&Object.keys(n).length>0;if(this.state.stage==="final"||r){let u=t;if(n&&Object.keys(n).length>0){let{street:p,street_number:a,city:c,addition:l}=n;if(p&&a&&c){let h=l?` ${l}`:"";u=`${p} ${a}${h}, ${c}`}}return this.finishSelection(u,n),!0}let s=typeof e!="string"?e.subtitle:null;return this.processSelection(i,s),!1}shouldAutoInsertComma(e){if(!e.includes(",")&&S.DIGITS_1_3.test(e.trim()))return!0;if(this.state.stage==="street_number"){let i=this.getCurrentFragment(e);return S.DIGITS_1_3.test(i)}return!1}finishSelection(e,t){this.updateState({query:e,suggestions:[],cities:[],streets:[],isValid:!0,value:t||null,stage:"final",hasMore:!1}),this.onSelect(t||e)}processSelection(e,t){let{stage:i,query:n}=this.state,r=n;if(t&&(i==="city"||i==="street"||i==="mixed")){if(i==="city")r=`${t}, ${e}, `;else{let a=this.getQueryPrefix(n),c=!a||!a.includes(t),l=a;if(a&&t){let h=a.match(S.STREET_NUMBER_PREFIX);if(h){let C=h[1];t.startsWith(C)&&(l="")}}c?r=l?`${l} ${e}, ${t}, `:`${e}, ${t}, `:r=l?`${l} ${e}, `:`${e}, `}this.updateQueryAndFetch(r);return}if(i==="direct"||i==="addition"){this.finishSelection(e);return}!n.includes(",")&&(i==="city"||i==="street"||i==="street_number_first")?r=`${e}, `:(r=this.replaceLastSegment(n,e),i!=="street_number"&&(r+=", ")),this.updateQueryAndFetch(r)}executeFetch(e,t=0){var a,c;let i=(e||"").toString();if(!i.trim()){(a=this.abortController)==null||a.abort(),this.resetState();return}t===0&&(this.updateState({isError:!1}),this.abortController&&this.abortController.abort(),this.abortController=new AbortController);let n=(c=this.abortController)==null?void 0:c.signal,r=this.explicitApiUrl?this.explicitApiUrl:`${g.API_URL}/infer/${this.country.toLowerCase()}`,s=new URLSearchParams({query:i,limit:this.currentLimit.toString()});this.explicitApiUrl&&s.append("country",this.country.toLowerCase()),this.authKey&&s.set("authKey",this.authKey);let u=r.includes("?")?"&":"?",p=`${r}${u}${s.toString()}`;this.fetcher(p,{signal:n}).then(l=>{if(!l.ok){if(t<this.maxRetries&&(l.status>=500||l.status===429))return this.retry(e,t,n);throw new Error("Network error")}return l.json()}).then(l=>{l&&this.mapResponseToState(l)}).catch(l=>{if(l.name!=="AbortError"){if(t<this.maxRetries)return this.retry(e,t,n);this.updateState({isError:!0,isLoading:!1})}})}retry(e,t,i){if(i!=null&&i.aborted)return;let n=Math.pow(2,t)*200;setTimeout(()=>{i!=null&&i.aborted||this.executeFetch(e,t+1)},n)}mapResponseToState(e){var u,p,a,c;let t={stage:e.stage,isLoading:!1},i=e.suggestions||[],n=[],r=new Set;for(let l of i){let h=`${l.label}|${l.subtitle||""}|${JSON.stringify(l.value||{})}`;r.has(h)||(r.add(h),n.push(l))}let s=n.length+(((u=e.cities)==null?void 0:u.length)||0)+(((p=e.streets)==null?void 0:p.length)||0);t.hasMore=s>=this.currentLimit,e.stage==="mixed"?(t.cities=e.cities||[],t.streets=e.streets||[],((a=t.cities)==null?void 0:a.length)===0&&((c=t.streets)==null?void 0:c.length)===0?t.suggestions=n:t.suggestions=[]):(t.suggestions=n,t.cities=[],t.streets=[]),t.isValid=e.stage==="final",this.updateState(t),t.isValid&&n.length===1&&this.selectItem(n[0])}updateQueryAndFetch(e){this.updateState({query:e,suggestions:[],cities:[],streets:[],isValid:!1,value:null,isLoading:!0,hasMore:!1}),this.debouncedFetch(e)}replaceLastSegment(e,t){let i=e.lastIndexOf(",");return i===-1?t:`${e.slice(0,i+1)} ${t}`.trim()}getQueryPrefix(e){let t=e.lastIndexOf(",");return t===-1?"":e.slice(0,t+1).trimEnd()}getCurrentFragment(e){var t;return((t=e.split(",").slice(-1)[0])!=null?t:"").trim()}resetState(){this.updateState(b(f({},A),{query:this.state.query}))}updateState(e){this.state=f(f({},this.state),e),this.onStateChange(this.state)}debounce(e,t){let i,n=(...r)=>{i&&clearTimeout(i),i=setTimeout(()=>e.apply(this,r),t)};return n.cancel=()=>{i&&(clearTimeout(i),i=void 0)},n}};function M(o,e){if(!e||!o)return[{text:o,match:!1}];let t=[],i=o.toLowerCase(),n=e.toLowerCase(),r=0,s=0;for(let a=0;a<o.length;a++){if(!(r<e.length&&i[a]===n[r]))continue;a>s&&t.push({text:o.slice(s,a),match:!1}),t.push({text:o[a],match:!0}),r++,s=a+1}return s<o.length&&t.push({text:o.slice(s),match:!1}),r===e.length?t:[{text:o,match:!1}]}var w=`
|
|
2
2
|
.pro6pp-wrapper {
|
|
3
3
|
position: relative;
|
|
4
4
|
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
|
|
@@ -71,17 +71,6 @@
|
|
|
71
71
|
background-color: #f3f4f6;
|
|
72
72
|
}
|
|
73
73
|
|
|
74
|
-
.pro6pp-loader {
|
|
75
|
-
width: 20px;
|
|
76
|
-
height: 20px;
|
|
77
|
-
margin: 0 8px;
|
|
78
|
-
border: 2px solid #e0e0e0;
|
|
79
|
-
border-top-color: #6b7280;
|
|
80
|
-
border-radius: 50%;
|
|
81
|
-
animation: pro6pp-spin 0.6s linear infinite;
|
|
82
|
-
flex-shrink: 0;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
74
|
.pro6pp-dropdown {
|
|
86
75
|
position: absolute;
|
|
87
76
|
top: 100%;
|
|
@@ -183,6 +172,27 @@
|
|
|
183
172
|
touch-action: manipulation;
|
|
184
173
|
}
|
|
185
174
|
|
|
175
|
+
.pro6pp-loader-item {
|
|
176
|
+
padding: 10px 12px;
|
|
177
|
+
color: #6b7280;
|
|
178
|
+
font-size: 0.875rem;
|
|
179
|
+
display: flex;
|
|
180
|
+
align-items: center;
|
|
181
|
+
justify-content: center;
|
|
182
|
+
gap: 8px;
|
|
183
|
+
background-color: #f9fafb;
|
|
184
|
+
border-top: 1px solid #f3f4f6;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
.pro6pp-mini-spinner {
|
|
188
|
+
width: 14px;
|
|
189
|
+
height: 14px;
|
|
190
|
+
border: 2px solid #e5e7eb;
|
|
191
|
+
border-top-color: #6b7280;
|
|
192
|
+
border-radius: 50%;
|
|
193
|
+
animation: pro6pp-spin 0.6s linear infinite;
|
|
194
|
+
}
|
|
195
|
+
|
|
186
196
|
@media (max-width: 640px) {
|
|
187
197
|
.pro6pp-input {
|
|
188
198
|
font-size: 16px;
|
|
@@ -208,13 +218,16 @@
|
|
|
208
218
|
@keyframes pro6pp-spin {
|
|
209
219
|
to { transform: rotate(360deg); }
|
|
210
220
|
}
|
|
211
|
-
`;var y=class{constructor(e,t){this.isOpen=!1;var r;let i=typeof e=="string"?document.querySelector(e):e;if(!i)throw new Error("InferJS: Target element not found.");if(this.noResultsText=t.noResultsText||"No results found",this.
|
|
221
|
+
`;var y=class{constructor(e,t){this.isOpen=!1;var r;let i=typeof e=="string"?document.querySelector(e):e;if(!i)throw new Error("InferJS: Target element not found.");if(this.noResultsText=t.noResultsText||"No results found",this.loadingText=t.loadingText||"Loading more...",this.showClearButton=t.showClearButton!==!1,this.useDefaultStyles=t.style!=="none",this.useDefaultStyles&&this.injectStyles(),this.wrapper=document.createElement("div"),this.wrapper.className="pro6pp-wrapper",i instanceof HTMLInputElement?(this.input=i,(r=this.input.parentNode)==null||r.insertBefore(this.wrapper,this.input),this.wrapper.appendChild(this.input)):(i.appendChild(this.wrapper),this.input=document.createElement("input"),this.input.type="text",t.placeholder&&(this.input.placeholder=t.placeholder),this.wrapper.appendChild(this.input)),this.input.setAttribute("autocomplete","off"),this.input.setAttribute("autocorrect","off"),this.input.setAttribute("autocapitalize","none"),this.input.setAttribute("spellcheck","false"),this.input.setAttribute("inputmode","search"),this.input.setAttribute("enterkeyhint","search"),this.useDefaultStyles&&this.input.classList.add("pro6pp-input"),t.inputClass){let s=t.inputClass.split(" ");this.input.classList.add(...s)}let n=document.createElement("div");n.className="pro6pp-input-addons",this.wrapper.appendChild(n),this.clearButton=document.createElement("button"),this.clearButton.type="button",this.clearButton.className="pro6pp-clear-button",this.clearButton.setAttribute("aria-label","Clear input"),this.clearButton.style.display="none",this.clearButton.innerHTML=`
|
|
212
222
|
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
|
|
213
223
|
<line x1="18" y1="6" x2="6" y2="18"></line>
|
|
214
224
|
<line x1="6" y1="6" x2="18" y2="18"></line>
|
|
215
225
|
</svg>
|
|
216
|
-
`,n.appendChild(this.clearButton),this.dropdown=document.createElement("div"),this.dropdown.className="pro6pp-dropdown",this.dropdown.style.display="none",this.wrapper.appendChild(this.dropdown),this.list=document.createElement("ul"),this.list.className="pro6pp-list",this.list.setAttribute("role","listbox"),this.dropdown.appendChild(this.list),this.
|
|
226
|
+
`,n.appendChild(this.clearButton),this.dropdown=document.createElement("div"),this.dropdown.className="pro6pp-dropdown",this.dropdown.style.display="none",this.wrapper.appendChild(this.dropdown),this.list=document.createElement("ul"),this.list.className="pro6pp-list",this.list.setAttribute("role","listbox"),this.dropdown.appendChild(this.list),this.dropdownLoader=document.createElement("div"),this.dropdownLoader.className="pro6pp-loader-item",this.dropdownLoader.style.display="none",this.dropdownLoader.innerHTML=`
|
|
227
|
+
<div class="pro6pp-mini-spinner"></div>
|
|
228
|
+
<span>${this.loadingText}</span>
|
|
229
|
+
`,this.dropdown.appendChild(this.dropdownLoader),this.core=new x(b(f({},t),{onStateChange:s=>{this.render(s),t.onStateChange&&t.onStateChange(s)},onSelect:s=>{typeof s=="string"?this.input.value=s:s&&typeof s=="object"&&(this.input.value=this.core.state.query),t.onSelect&&t.onSelect(s)}})),this.observer=new IntersectionObserver(s=>{s[0].isIntersecting&&this.core.state.hasMore&&!this.core.state.isLoading&&this.core.loadMore()},{threshold:.1}),this.bindEvents()}get value(){return this.core.state.value||null}set value(e){if(!e)return;let t=`${e.street} ${e.street_number}, ${e.city}`;this.core.selectItem({label:t,value:e})}injectStyles(){let e="pro6pp-styles";if(!document.getElementById(e)){let t=document.createElement("style");t.id=e,t.textContent=w,document.head.appendChild(t)}}bindEvents(){this.input.addEventListener("input",e=>{let t=e.target.value;this.isOpen=!0,this.core.handleInput(t)}),this.input.addEventListener("keydown",e=>{this.core.handleKeyDown(e)}),this.clearButton.addEventListener("click",()=>{this.core.handleInput(""),this.input.focus()}),document.addEventListener("mousedown",e=>{this.wrapper.contains(e.target)||(this.isOpen=!1,this.dropdown.style.display="none")}),this.input.addEventListener("focus",()=>{this.isOpen=!0,this.render(this.core.state)})}render(e){this.input.value!==e.query&&(this.input.value=e.query),this.showClearButton&&(this.clearButton.style.display=e.query.length>0?"flex":"none"),this.list.innerHTML="";let t=[...e.cities,...e.streets,...e.suggestions],i=t.length>0,n=!e.isLoading&&!e.isError&&e.query.length>0&&!i&&!e.isValid;if(!(this.isOpen&&(i||e.isLoading||n))){this.dropdown.style.display="none";return}if(this.dropdown.style.display="block",e.isLoading&&i?this.dropdownLoader.style.display="flex":this.dropdownLoader.style.display="none",e.isLoading&&!i){let s=document.createElement("li");s.className="pro6pp-no-results",s.textContent="Searching...",this.list.appendChild(s),this.dropdownLoader.style.display="none";return}if(n){let s=document.createElement("li");s.className="pro6pp-no-results",s.textContent=this.noResultsText,this.list.appendChild(s);return}if(t.forEach((s,u)=>{if(!s.label)return;let p=document.createElement("li");p.className="pro6pp-item",u===e.selectedSuggestionIndex&&p.classList.add("pro6pp-item--active"),p.setAttribute("role","option"),p.setAttribute("aria-selected",u===e.selectedSuggestionIndex?"true":"false");let a=document.createElement("span");a.className="pro6pp-item__label",M(s.label,e.query).forEach(({text:d,match:E})=>{if(E){let v=document.createElement("strong");v.className="pro6pp-item__label--match",v.textContent=d,a.appendChild(v)}else a.appendChild(document.createTextNode(d))}),p.appendChild(a);let l=s.count!==void 0&&s.count!==null?s.count:"",h=s.subtitle||l;if(h!==""){let d=document.createElement("span");d.className="pro6pp-item__subtitle",d.textContent=`, ${h}`,p.appendChild(d)}if(s.value===void 0||s.value===null){let d=document.createElement("div");d.className="pro6pp-item__chevron",d.innerHTML=`
|
|
217
230
|
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
|
|
218
231
|
<polyline points="9 18 15 12 9 6"></polyline>
|
|
219
232
|
</svg>
|
|
220
|
-
`,
|
|
233
|
+
`,p.appendChild(d)}p.onmousedown=d=>d.preventDefault(),p.onclick=d=>{d.stopPropagation(),this.core.selectItem(s)?(this.isOpen=!1,this.dropdown.style.display="none"):setTimeout(()=>this.input.focus(),0)},this.list.appendChild(p)}),e.hasMore&&!e.isLoading){let s=document.createElement("li");s.style.height="1px",s.style.opacity="0",this.list.appendChild(s),this.observer.observe(s)}}};function U(o,e){return new y(o,e)}return B(H);})();
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
var
|
|
1
|
+
var A=Object.defineProperty,M=Object.defineProperties;var _=Object.getOwnPropertyDescriptors;var E=Object.getOwnPropertySymbols;var k=Object.prototype.hasOwnProperty,R=Object.prototype.propertyIsEnumerable;var I=(l,e,t)=>e in l?A(l,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):l[e]=t,f=(l,e)=>{for(var t in e||(e={}))k.call(e,t)&&I(l,t,e[t]);if(E)for(var t of E(e))R.call(e,t)&&I(l,t,e[t]);return l},m=(l,e)=>M(l,_(e));var g={API_URL:"https://api.pro6pp.nl/v2",LIMIT:20,DEBOUNCE_MS:150,MIN_DEBOUNCE_MS:50,MAX_RETRIES:0},y={DIGITS_1_3:/^[0-9]{1,3}$/,STREET_NUMBER_PREFIX:/^(\d+)\s*,\s*$/},L={query:"",stage:null,cities:[],streets:[],suggestions:[],isValid:!1,value:null,isError:!1,isLoading:!1,hasMore:!1,selectedSuggestionIndex:-1},b=class{constructor(e){this.abortController=null;this.country=e.country,this.authKey=e.authKey,this.explicitApiUrl=e.apiUrl,this.baseLimit=e.limit||g.LIMIT,this.currentLimit=this.baseLimit;let t=e.maxRetries!==void 0?e.maxRetries:g.MAX_RETRIES;this.maxRetries=Math.max(0,Math.min(t,10)),this.fetcher=e.fetcher||((n,s)=>fetch(n,s)),this.onStateChange=e.onStateChange||(()=>{}),this.onSelect=e.onSelect||(()=>{}),this.state=f({},L);let i=e.debounceMs!==void 0?e.debounceMs:g.DEBOUNCE_MS,r=Math.max(i,g.MIN_DEBOUNCE_MS);this.debouncedFetch=this.debounce(n=>this.executeFetch(n),r)}handleInput(e){this.currentLimit=this.baseLimit;let t=this.state.stage==="final"&&e!==this.state.query;this.updateState({query:e,isValid:!1,value:null,isLoading:!!e.trim(),selectedSuggestionIndex:-1,hasMore:!1,stage:t?null:this.state.stage}),t&&this.onSelect(null),this.debouncedFetch(e)}loadMore(){this.state.isLoading||(this.currentLimit+=this.baseLimit,this.updateState({isLoading:!0}),this.executeFetch(this.state.query))}handleKeyDown(e){let t=e.target;if(!t)return;let i=this.state.cities.length+this.state.streets.length+this.state.suggestions.length;if(i>0){if(e.key==="ArrowDown"){e.preventDefault();let n=this.state.selectedSuggestionIndex+1;n>=i&&(n=0),this.updateState({selectedSuggestionIndex:n});return}if(e.key==="ArrowUp"){e.preventDefault();let n=this.state.selectedSuggestionIndex-1;n<0&&(n=i-1),this.updateState({selectedSuggestionIndex:n});return}if(e.key==="Enter"&&this.state.selectedSuggestionIndex>=0){e.preventDefault();let s=[...this.state.cities,...this.state.streets,...this.state.suggestions][this.state.selectedSuggestionIndex];s&&(this.selectItem(s),this.updateState({selectedSuggestionIndex:-1}));return}}let r=t.value;if(e.key===" "&&this.shouldAutoInsertComma(r)){e.preventDefault();let n=`${r.trim()}, `;this.updateQueryAndFetch(n)}}selectItem(e){this.debouncedFetch.cancel(),this.abortController&&this.abortController.abort();let t=typeof e=="string"?e:e.label,i=t;typeof e!="string"&&typeof e.value=="string"&&(i=e.value);let r=typeof e!="string"&&typeof e.value=="object"?e.value:void 0,n=!!r&&Object.keys(r).length>0;if(this.state.stage==="final"||n){let u=t;if(r&&Object.keys(r).length>0){let{street:p,street_number:o,city:c,addition:a}=r;if(p&&o&&c){let h=a?` ${a}`:"";u=`${p} ${o}${h}, ${c}`}}return this.finishSelection(u,r),!0}let s=typeof e!="string"?e.subtitle:null;return this.processSelection(i,s),!1}shouldAutoInsertComma(e){if(!e.includes(",")&&y.DIGITS_1_3.test(e.trim()))return!0;if(this.state.stage==="street_number"){let i=this.getCurrentFragment(e);return y.DIGITS_1_3.test(i)}return!1}finishSelection(e,t){this.updateState({query:e,suggestions:[],cities:[],streets:[],isValid:!0,value:t||null,stage:"final",hasMore:!1}),this.onSelect(t||e)}processSelection(e,t){let{stage:i,query:r}=this.state,n=r;if(t&&(i==="city"||i==="street"||i==="mixed")){if(i==="city")n=`${t}, ${e}, `;else{let o=this.getQueryPrefix(r),c=!o||!o.includes(t),a=o;if(o&&t){let h=o.match(y.STREET_NUMBER_PREFIX);if(h){let w=h[1];t.startsWith(w)&&(a="")}}c?n=a?`${a} ${e}, ${t}, `:`${e}, ${t}, `:n=a?`${a} ${e}, `:`${e}, `}this.updateQueryAndFetch(n);return}if(i==="direct"||i==="addition"){this.finishSelection(e);return}!r.includes(",")&&(i==="city"||i==="street"||i==="street_number_first")?n=`${e}, `:(n=this.replaceLastSegment(r,e),i!=="street_number"&&(n+=", ")),this.updateQueryAndFetch(n)}executeFetch(e,t=0){var o,c;let i=(e||"").toString();if(!i.trim()){(o=this.abortController)==null||o.abort(),this.resetState();return}t===0&&(this.updateState({isError:!1}),this.abortController&&this.abortController.abort(),this.abortController=new AbortController);let r=(c=this.abortController)==null?void 0:c.signal,n=this.explicitApiUrl?this.explicitApiUrl:`${g.API_URL}/infer/${this.country.toLowerCase()}`,s=new URLSearchParams({query:i,limit:this.currentLimit.toString()});this.explicitApiUrl&&s.append("country",this.country.toLowerCase()),this.authKey&&s.set("authKey",this.authKey);let u=n.includes("?")?"&":"?",p=`${n}${u}${s.toString()}`;this.fetcher(p,{signal:r}).then(a=>{if(!a.ok){if(t<this.maxRetries&&(a.status>=500||a.status===429))return this.retry(e,t,r);throw new Error("Network error")}return a.json()}).then(a=>{a&&this.mapResponseToState(a)}).catch(a=>{if(a.name!=="AbortError"){if(t<this.maxRetries)return this.retry(e,t,r);this.updateState({isError:!0,isLoading:!1})}})}retry(e,t,i){if(i!=null&&i.aborted)return;let r=Math.pow(2,t)*200;setTimeout(()=>{i!=null&&i.aborted||this.executeFetch(e,t+1)},r)}mapResponseToState(e){var u,p,o,c;let t={stage:e.stage,isLoading:!1},i=e.suggestions||[],r=[],n=new Set;for(let a of i){let h=`${a.label}|${a.subtitle||""}|${JSON.stringify(a.value||{})}`;n.has(h)||(n.add(h),r.push(a))}let s=r.length+(((u=e.cities)==null?void 0:u.length)||0)+(((p=e.streets)==null?void 0:p.length)||0);t.hasMore=s>=this.currentLimit,e.stage==="mixed"?(t.cities=e.cities||[],t.streets=e.streets||[],((o=t.cities)==null?void 0:o.length)===0&&((c=t.streets)==null?void 0:c.length)===0?t.suggestions=r:t.suggestions=[]):(t.suggestions=r,t.cities=[],t.streets=[]),t.isValid=e.stage==="final",this.updateState(t),t.isValid&&r.length===1&&this.selectItem(r[0])}updateQueryAndFetch(e){this.updateState({query:e,suggestions:[],cities:[],streets:[],isValid:!1,value:null,isLoading:!0,hasMore:!1}),this.debouncedFetch(e)}replaceLastSegment(e,t){let i=e.lastIndexOf(",");return i===-1?t:`${e.slice(0,i+1)} ${t}`.trim()}getQueryPrefix(e){let t=e.lastIndexOf(",");return t===-1?"":e.slice(0,t+1).trimEnd()}getCurrentFragment(e){var t;return((t=e.split(",").slice(-1)[0])!=null?t:"").trim()}resetState(){this.updateState(m(f({},L),{query:this.state.query}))}updateState(e){this.state=f(f({},this.state),e),this.onStateChange(this.state)}debounce(e,t){let i,r=(...n)=>{i&&clearTimeout(i),i=setTimeout(()=>e.apply(this,n),t)};return r.cancel=()=>{i&&(clearTimeout(i),i=void 0)},r}};function T(l,e){if(!e||!l)return[{text:l,match:!1}];let t=[],i=l.toLowerCase(),r=e.toLowerCase(),n=0,s=0;for(let o=0;o<l.length;o++){if(!(n<e.length&&i[o]===r[n]))continue;o>s&&t.push({text:l.slice(s,o),match:!1}),t.push({text:l[o],match:!0}),n++,s=o+1}return s<l.length&&t.push({text:l.slice(s),match:!1}),n===e.length?t:[{text:l,match:!1}]}var v=`
|
|
2
2
|
.pro6pp-wrapper {
|
|
3
3
|
position: relative;
|
|
4
4
|
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
|
|
@@ -71,17 +71,6 @@ var T=Object.defineProperty,M=Object.defineProperties;var A=Object.getOwnPropert
|
|
|
71
71
|
background-color: #f3f4f6;
|
|
72
72
|
}
|
|
73
73
|
|
|
74
|
-
.pro6pp-loader {
|
|
75
|
-
width: 20px;
|
|
76
|
-
height: 20px;
|
|
77
|
-
margin: 0 8px;
|
|
78
|
-
border: 2px solid #e0e0e0;
|
|
79
|
-
border-top-color: #6b7280;
|
|
80
|
-
border-radius: 50%;
|
|
81
|
-
animation: pro6pp-spin 0.6s linear infinite;
|
|
82
|
-
flex-shrink: 0;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
74
|
.pro6pp-dropdown {
|
|
86
75
|
position: absolute;
|
|
87
76
|
top: 100%;
|
|
@@ -183,6 +172,27 @@ var T=Object.defineProperty,M=Object.defineProperties;var A=Object.getOwnPropert
|
|
|
183
172
|
touch-action: manipulation;
|
|
184
173
|
}
|
|
185
174
|
|
|
175
|
+
.pro6pp-loader-item {
|
|
176
|
+
padding: 10px 12px;
|
|
177
|
+
color: #6b7280;
|
|
178
|
+
font-size: 0.875rem;
|
|
179
|
+
display: flex;
|
|
180
|
+
align-items: center;
|
|
181
|
+
justify-content: center;
|
|
182
|
+
gap: 8px;
|
|
183
|
+
background-color: #f9fafb;
|
|
184
|
+
border-top: 1px solid #f3f4f6;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
.pro6pp-mini-spinner {
|
|
188
|
+
width: 14px;
|
|
189
|
+
height: 14px;
|
|
190
|
+
border: 2px solid #e5e7eb;
|
|
191
|
+
border-top-color: #6b7280;
|
|
192
|
+
border-radius: 50%;
|
|
193
|
+
animation: pro6pp-spin 0.6s linear infinite;
|
|
194
|
+
}
|
|
195
|
+
|
|
186
196
|
@media (max-width: 640px) {
|
|
187
197
|
.pro6pp-input {
|
|
188
198
|
font-size: 16px;
|
|
@@ -208,13 +218,16 @@ var T=Object.defineProperty,M=Object.defineProperties;var A=Object.getOwnPropert
|
|
|
208
218
|
@keyframes pro6pp-spin {
|
|
209
219
|
to { transform: rotate(360deg); }
|
|
210
220
|
}
|
|
211
|
-
`;var
|
|
221
|
+
`;var S=class{constructor(e,t){this.isOpen=!1;var n;let i=typeof e=="string"?document.querySelector(e):e;if(!i)throw new Error("InferJS: Target element not found.");if(this.noResultsText=t.noResultsText||"No results found",this.loadingText=t.loadingText||"Loading more...",this.showClearButton=t.showClearButton!==!1,this.useDefaultStyles=t.style!=="none",this.useDefaultStyles&&this.injectStyles(),this.wrapper=document.createElement("div"),this.wrapper.className="pro6pp-wrapper",i instanceof HTMLInputElement?(this.input=i,(n=this.input.parentNode)==null||n.insertBefore(this.wrapper,this.input),this.wrapper.appendChild(this.input)):(i.appendChild(this.wrapper),this.input=document.createElement("input"),this.input.type="text",t.placeholder&&(this.input.placeholder=t.placeholder),this.wrapper.appendChild(this.input)),this.input.setAttribute("autocomplete","off"),this.input.setAttribute("autocorrect","off"),this.input.setAttribute("autocapitalize","none"),this.input.setAttribute("spellcheck","false"),this.input.setAttribute("inputmode","search"),this.input.setAttribute("enterkeyhint","search"),this.useDefaultStyles&&this.input.classList.add("pro6pp-input"),t.inputClass){let s=t.inputClass.split(" ");this.input.classList.add(...s)}let r=document.createElement("div");r.className="pro6pp-input-addons",this.wrapper.appendChild(r),this.clearButton=document.createElement("button"),this.clearButton.type="button",this.clearButton.className="pro6pp-clear-button",this.clearButton.setAttribute("aria-label","Clear input"),this.clearButton.style.display="none",this.clearButton.innerHTML=`
|
|
212
222
|
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
|
|
213
223
|
<line x1="18" y1="6" x2="6" y2="18"></line>
|
|
214
224
|
<line x1="6" y1="6" x2="18" y2="18"></line>
|
|
215
225
|
</svg>
|
|
216
|
-
`,r.appendChild(this.clearButton),this.dropdown=document.createElement("div"),this.dropdown.className="pro6pp-dropdown",this.dropdown.style.display="none",this.wrapper.appendChild(this.dropdown),this.list=document.createElement("ul"),this.list.className="pro6pp-list",this.list.setAttribute("role","listbox"),this.dropdown.appendChild(this.list),this.
|
|
226
|
+
`,r.appendChild(this.clearButton),this.dropdown=document.createElement("div"),this.dropdown.className="pro6pp-dropdown",this.dropdown.style.display="none",this.wrapper.appendChild(this.dropdown),this.list=document.createElement("ul"),this.list.className="pro6pp-list",this.list.setAttribute("role","listbox"),this.dropdown.appendChild(this.list),this.dropdownLoader=document.createElement("div"),this.dropdownLoader.className="pro6pp-loader-item",this.dropdownLoader.style.display="none",this.dropdownLoader.innerHTML=`
|
|
227
|
+
<div class="pro6pp-mini-spinner"></div>
|
|
228
|
+
<span>${this.loadingText}</span>
|
|
229
|
+
`,this.dropdown.appendChild(this.dropdownLoader),this.core=new b(m(f({},t),{onStateChange:s=>{this.render(s),t.onStateChange&&t.onStateChange(s)},onSelect:s=>{typeof s=="string"?this.input.value=s:s&&typeof s=="object"&&(this.input.value=this.core.state.query),t.onSelect&&t.onSelect(s)}})),this.observer=new IntersectionObserver(s=>{s[0].isIntersecting&&this.core.state.hasMore&&!this.core.state.isLoading&&this.core.loadMore()},{threshold:.1}),this.bindEvents()}get value(){return this.core.state.value||null}set value(e){if(!e)return;let t=`${e.street} ${e.street_number}, ${e.city}`;this.core.selectItem({label:t,value:e})}injectStyles(){let e="pro6pp-styles";if(!document.getElementById(e)){let t=document.createElement("style");t.id=e,t.textContent=v,document.head.appendChild(t)}}bindEvents(){this.input.addEventListener("input",e=>{let t=e.target.value;this.isOpen=!0,this.core.handleInput(t)}),this.input.addEventListener("keydown",e=>{this.core.handleKeyDown(e)}),this.clearButton.addEventListener("click",()=>{this.core.handleInput(""),this.input.focus()}),document.addEventListener("mousedown",e=>{this.wrapper.contains(e.target)||(this.isOpen=!1,this.dropdown.style.display="none")}),this.input.addEventListener("focus",()=>{this.isOpen=!0,this.render(this.core.state)})}render(e){this.input.value!==e.query&&(this.input.value=e.query),this.showClearButton&&(this.clearButton.style.display=e.query.length>0?"flex":"none"),this.list.innerHTML="";let t=[...e.cities,...e.streets,...e.suggestions],i=t.length>0,r=!e.isLoading&&!e.isError&&e.query.length>0&&!i&&!e.isValid;if(!(this.isOpen&&(i||e.isLoading||r))){this.dropdown.style.display="none";return}if(this.dropdown.style.display="block",e.isLoading&&i?this.dropdownLoader.style.display="flex":this.dropdownLoader.style.display="none",e.isLoading&&!i){let s=document.createElement("li");s.className="pro6pp-no-results",s.textContent="Searching...",this.list.appendChild(s),this.dropdownLoader.style.display="none";return}if(r){let s=document.createElement("li");s.className="pro6pp-no-results",s.textContent=this.noResultsText,this.list.appendChild(s);return}if(t.forEach((s,u)=>{if(!s.label)return;let p=document.createElement("li");p.className="pro6pp-item",u===e.selectedSuggestionIndex&&p.classList.add("pro6pp-item--active"),p.setAttribute("role","option"),p.setAttribute("aria-selected",u===e.selectedSuggestionIndex?"true":"false");let o=document.createElement("span");o.className="pro6pp-item__label",T(s.label,e.query).forEach(({text:d,match:C})=>{if(C){let x=document.createElement("strong");x.className="pro6pp-item__label--match",x.textContent=d,o.appendChild(x)}else o.appendChild(document.createTextNode(d))}),p.appendChild(o);let a=s.count!==void 0&&s.count!==null?s.count:"",h=s.subtitle||a;if(h!==""){let d=document.createElement("span");d.className="pro6pp-item__subtitle",d.textContent=`, ${h}`,p.appendChild(d)}if(s.value===void 0||s.value===null){let d=document.createElement("div");d.className="pro6pp-item__chevron",d.innerHTML=`
|
|
217
230
|
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
|
|
218
231
|
<polyline points="9 18 15 12 9 6"></polyline>
|
|
219
232
|
</svg>
|
|
220
|
-
`,
|
|
233
|
+
`,p.appendChild(d)}p.onmousedown=d=>d.preventDefault(),p.onclick=d=>{d.stopPropagation(),this.core.selectItem(s)?(this.isOpen=!1,this.dropdown.style.display="none"):setTimeout(()=>this.input.focus(),0)},this.list.appendChild(p)}),e.hasMore&&!e.isLoading){let s=document.createElement("li");s.style.height="1px",s.style.opacity="0",this.list.appendChild(s),this.observer.observe(s)}}};function Q(l,e){return new S(l,e)}export{S as InferJS,Q as attach};
|
package/package.json
CHANGED
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"sideEffects": [
|
|
19
19
|
"*.css"
|
|
20
20
|
],
|
|
21
|
-
"version": "0.0.2-beta.
|
|
21
|
+
"version": "0.0.2-beta.15",
|
|
22
22
|
"main": "./dist/index.js",
|
|
23
23
|
"module": "./dist/index.mjs",
|
|
24
24
|
"types": "./dist/index.d.ts",
|
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
"test:coverage": "vitest run --coverage"
|
|
44
44
|
},
|
|
45
45
|
"dependencies": {
|
|
46
|
-
"@pro6pp/infer-core": "0.0.2-beta.
|
|
46
|
+
"@pro6pp/infer-core": "0.0.2-beta.14"
|
|
47
47
|
},
|
|
48
48
|
"devDependencies": {
|
|
49
49
|
"@testing-library/dom": "^10.4.1",
|