@pro6pp/infer-js 0.0.2-beta.11 → 0.0.2-beta.13
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 +6 -1
- package/dist/index.cjs +43 -12
- package/dist/index.d.mts +11 -1
- package/dist/index.d.ts +11 -1
- package/dist/index.global.js +43 -12
- package/dist/index.mjs +43 -12
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -70,7 +70,12 @@ const inputElement = document.getElementById('address-input');
|
|
|
70
70
|
attach(inputElement, {
|
|
71
71
|
authKey: 'YOUR_AUTH_KEY',
|
|
72
72
|
country: 'NL',
|
|
73
|
-
|
|
73
|
+
// triggered whenever the internal state changes
|
|
74
|
+
onStateChange: function (state) {
|
|
75
|
+
console.log('Current State:', state);
|
|
76
|
+
},
|
|
77
|
+
// triggered when the user selects a final address
|
|
78
|
+
onSelect: function (result) {
|
|
74
79
|
console.log('Selected Address:', result);
|
|
75
80
|
},
|
|
76
81
|
});
|
package/dist/index.cjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"use strict";var m=Object.defineProperty,A=Object.defineProperties,k=Object.getOwnPropertyDescriptor,_=Object.getOwnPropertyDescriptors,B=Object.getOwnPropertyNames,
|
|
1
|
+
"use strict";var m=Object.defineProperty,A=Object.defineProperties,k=Object.getOwnPropertyDescriptor,_=Object.getOwnPropertyDescriptors,B=Object.getOwnPropertyNames,C=Object.getOwnPropertySymbols;var E=Object.prototype.hasOwnProperty,D=Object.prototype.propertyIsEnumerable;var I=(o,e,t)=>e in o?m(o,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):o[e]=t,c=(o,e)=>{for(var t in e||(e={}))E.call(e,t)&&I(o,t,e[t]);if(C)for(var t of C(e))D.call(e,t)&&I(o,t,e[t]);return o},b=(o,e)=>A(o,_(e));var F=(o,e)=>{for(var t in e)m(o,t,{get:e[t],enumerable:!0})},R=(o,e,t,i)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of B(e))!E.call(o,n)&&n!==t&&m(o,n,{get:()=>e[n],enumerable:!(i=k(e,n))||i.enumerable});return o};var $=o=>R(m({},"__esModule",{value:!0}),o);var U={};F(U,{InferJS:()=>y,attach:()=>N});module.exports=$(U);var f={API_URL:"https://api.pro6pp.nl/v2",LIMIT:20,DEBOUNCE_MS:150,MIN_DEBOUNCE_MS:50,MAX_RETRIES:0},L={DIGITS_1_3:/^[0-9]{1,3}$/},T={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||f.LIMIT,this.currentLimit=this.baseLimit;let t=e.maxRetries!==void 0?e.maxRetries:f.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=c({},T);let i=e.debounceMs!==void 0?e.debounceMs:f.DEBOUNCE_MS,n=Math.max(i,f.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}),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 d=t;if(n&&Object.keys(n).length>0){let{street:l,street_number:a,city:h,addition:p}=n;if(l&&a&&h){let g=p?` ${p}`:"";d=`${l} ${a}${g}, ${h}`}}return this.finishSelection(d,n),!0}let s=typeof e!="string"?e.subtitle:null;return this.processSelection(i,s),!1}shouldAutoInsertComma(e){if(!e.includes(",")&&L.DIGITS_1_3.test(e.trim()))return!0;if(this.state.stage==="street_number"){let i=this.getCurrentFragment(e);return L.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);!a||!a.includes(t)?r=a?`${a} ${e}, ${t}, `:`${e}, ${t}, `:r=a?`${a} ${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,h;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=(h=this.abortController)==null?void 0:h.signal,r=this.explicitApiUrl?this.explicitApiUrl:`${f.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 d=r.includes("?")?"&":"?",l=`${r}${d}${s.toString()}`;this.fetcher(l,{signal:n}).then(p=>{if(!p.ok){if(t<this.maxRetries&&(p.status>=500||p.status===429))return this.retry(e,t,n);throw new Error("Network error")}return p.json()}).then(p=>{p&&this.mapResponseToState(p)}).catch(p=>{if(p.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 d,l;let t={stage:e.stage,isLoading:!1},i=e.suggestions||[],n=[],r=new Set;for(let a of i){let h=`${a.label}|${a.subtitle||""}|${JSON.stringify(a.value||{})}`;r.has(h)||(r.add(h),n.push(a))}let s=n.length+(((d=e.cities)==null?void 0:d.length)||0)+(((l=e.streets)==null?void 0:l.length)||0);t.hasMore=s>=this.currentLimit,e.stage==="mixed"?(t.cities=e.cities||[],t.streets=e.streets||[],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(c({},T),{query:this.state.query}))}updateState(e){this.state=c(c({},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 S=`
|
|
2
2
|
.pro6pp-wrapper {
|
|
3
3
|
position: relative;
|
|
4
4
|
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
|
|
@@ -20,6 +20,12 @@
|
|
|
20
20
|
appearance: none;
|
|
21
21
|
transition: border-color 0.2s, box-shadow 0.2s;
|
|
22
22
|
}
|
|
23
|
+
|
|
24
|
+
.pro6pp-input::placeholder {
|
|
25
|
+
font-size: 16px;
|
|
26
|
+
color: #a3a3a3;
|
|
27
|
+
}
|
|
28
|
+
|
|
23
29
|
.pro6pp-input:focus {
|
|
24
30
|
outline: none;
|
|
25
31
|
border-color: #3b82f6;
|
|
@@ -42,8 +48,8 @@
|
|
|
42
48
|
.pro6pp-clear-button {
|
|
43
49
|
background: none;
|
|
44
50
|
border: none;
|
|
45
|
-
width:
|
|
46
|
-
height:
|
|
51
|
+
width: 32px;
|
|
52
|
+
height: 32px;
|
|
47
53
|
cursor: pointer;
|
|
48
54
|
color: #a3a3a3;
|
|
49
55
|
display: flex;
|
|
@@ -88,7 +94,7 @@
|
|
|
88
94
|
box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
|
|
89
95
|
z-index: 9999;
|
|
90
96
|
padding: 0;
|
|
91
|
-
max-height:
|
|
97
|
+
max-height: 280px;
|
|
92
98
|
overflow-y: auto;
|
|
93
99
|
display: flex;
|
|
94
100
|
flex-direction: column;
|
|
@@ -96,7 +102,7 @@
|
|
|
96
102
|
|
|
97
103
|
@media (max-height: 500px) {
|
|
98
104
|
.pro6pp-dropdown {
|
|
99
|
-
max-height:
|
|
105
|
+
max-height: 180px;
|
|
100
106
|
}
|
|
101
107
|
}
|
|
102
108
|
|
|
@@ -112,7 +118,8 @@
|
|
|
112
118
|
cursor: pointer;
|
|
113
119
|
display: flex;
|
|
114
120
|
align-items: center;
|
|
115
|
-
font-size:
|
|
121
|
+
font-size: 15px;
|
|
122
|
+
line-height: 1.4;
|
|
116
123
|
color: #374151;
|
|
117
124
|
border-bottom: 1px solid #f3f4f6;
|
|
118
125
|
transition: background-color 0.1s;
|
|
@@ -135,18 +142,24 @@
|
|
|
135
142
|
|
|
136
143
|
.pro6pp-item__label {
|
|
137
144
|
font-weight: 500;
|
|
138
|
-
flex-shrink:
|
|
145
|
+
flex-shrink: 1;
|
|
146
|
+
overflow: hidden;
|
|
147
|
+
text-overflow: ellipsis;
|
|
148
|
+
white-space: nowrap;
|
|
139
149
|
}
|
|
150
|
+
|
|
140
151
|
.pro6pp-item__subtitle {
|
|
141
|
-
font-size:
|
|
152
|
+
font-size: 13px;
|
|
142
153
|
color: #6b7280;
|
|
143
|
-
flex-
|
|
154
|
+
flex-shrink: 0;
|
|
144
155
|
}
|
|
156
|
+
|
|
145
157
|
.pro6pp-item__chevron {
|
|
146
158
|
color: #d1d5db;
|
|
147
159
|
display: flex;
|
|
148
160
|
align-items: center;
|
|
149
161
|
margin-left: auto;
|
|
162
|
+
padding-left: 8px;
|
|
150
163
|
}
|
|
151
164
|
|
|
152
165
|
.pro6pp-no-results {
|
|
@@ -170,6 +183,24 @@
|
|
|
170
183
|
touch-action: manipulation;
|
|
171
184
|
}
|
|
172
185
|
|
|
186
|
+
@media (max-width: 640px) {
|
|
187
|
+
.pro6pp-input {
|
|
188
|
+
font-size: 16px;
|
|
189
|
+
padding: 10px 12px;
|
|
190
|
+
}
|
|
191
|
+
.pro6pp-item {
|
|
192
|
+
padding: 10px 12px;
|
|
193
|
+
font-size: 14px;
|
|
194
|
+
}
|
|
195
|
+
.pro6pp-item__subtitle {
|
|
196
|
+
font-size: 12px;
|
|
197
|
+
}
|
|
198
|
+
.pro6pp-load-more {
|
|
199
|
+
padding: 12px;
|
|
200
|
+
font-size: 13px;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
173
204
|
.pro6pp-load-more:active {
|
|
174
205
|
background-color: #f3f4f6;
|
|
175
206
|
}
|
|
@@ -177,13 +208,13 @@
|
|
|
177
208
|
@keyframes pro6pp-spin {
|
|
178
209
|
to { transform: rotate(360deg); }
|
|
179
210
|
}
|
|
180
|
-
`;var y=class{constructor(t
|
|
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.loadMoreText=t.loadMoreText||"Show more results...",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.loader=document.createElement("div"),this.loader.className="pro6pp-loader",this.loader.style.display="none",n.appendChild(this.loader),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=`
|
|
181
212
|
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
|
|
182
213
|
<line x1="18" y1="6" x2="6" y2="18"></line>
|
|
183
214
|
<line x1="6" y1="6" x2="18" y2="18"></line>
|
|
184
215
|
</svg>
|
|
185
|
-
`,
|
|
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.loadMoreButton=document.createElement("button"),this.loadMoreButton.type="button",this.loadMoreButton.className="pro6pp-load-more",this.loadMoreButton.textContent=this.loadMoreText,this.loadMoreButton.style.display="none",this.dropdown.appendChild(this.loadMoreButton),this.core=new x(b(c({},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.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=S,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()}),this.loadMoreButton.addEventListener("click",e=>{e.preventDefault(),this.core.loadMore()}),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.loader.style.display=e.isLoading?"block":"none",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",this.loadMoreButton.style.display=e.hasMore?"block":"none",e.isLoading&&!i){let s=document.createElement("li");s.className="pro6pp-no-results",s.textContent="Loading suggestions...",this.list.appendChild(s);return}if(n){let s=document.createElement("li");s.className="pro6pp-no-results",s.textContent=this.noResultsText,this.list.appendChild(s);return}t.forEach((s,d)=>{if(!s.label)return;let l=document.createElement("li");l.className="pro6pp-item",d===e.selectedSuggestionIndex&&l.classList.add("pro6pp-item--active"),l.setAttribute("role","option"),l.setAttribute("aria-selected",d===e.selectedSuggestionIndex?"true":"false");let a=document.createElement("span");a.className="pro6pp-item__label",M(s.label,e.query).forEach(({text:u,match:w})=>{if(w){let v=document.createElement("strong");v.className="pro6pp-item__label--match",v.textContent=u,a.appendChild(v)}else a.appendChild(document.createTextNode(u))}),l.appendChild(a);let p=s.count!==void 0&&s.count!==null?s.count:"",g=s.subtitle||p;if(g!==""){let u=document.createElement("span");u.className="pro6pp-item__subtitle",u.textContent=`, ${g}`,l.appendChild(u)}if(s.value===void 0||s.value===null){let u=document.createElement("div");u.className="pro6pp-item__chevron",u.innerHTML=`
|
|
186
217
|
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
|
|
187
218
|
<polyline points="9 18 15 12 9 6"></polyline>
|
|
188
219
|
</svg>
|
|
189
|
-
`,l.appendChild(u)}l.onmousedown=u=>u.preventDefault(),l.onclick=u=>{u.stopPropagation(),this.core.selectItem(
|
|
220
|
+
`,l.appendChild(u)}l.onmousedown=u=>u.preventDefault(),l.onclick=u=>{u.stopPropagation(),this.core.selectItem(s)?(this.isOpen=!1,this.dropdown.style.display="none"):setTimeout(()=>this.input.focus(),0)},this.list.appendChild(l)})}};function N(o,e){return new y(o,e)}0&&(module.exports={InferJS,attach});
|
package/dist/index.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { InferConfig } from '@pro6pp/infer-core';
|
|
1
|
+
import { InferConfig, AddressValue } from '@pro6pp/infer-core';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Configuration options for the JS Infer SDK.
|
|
@@ -59,6 +59,16 @@ declare class InferJS {
|
|
|
59
59
|
* @param config Configuration options for the API and UI.
|
|
60
60
|
*/
|
|
61
61
|
constructor(target: string | HTMLElement, config: InferJSConfig);
|
|
62
|
+
/**
|
|
63
|
+
* Retrieves the current selected address value if available.
|
|
64
|
+
* @returns The AddressValue object if valid, otherwise null.
|
|
65
|
+
*/
|
|
66
|
+
get value(): AddressValue | null;
|
|
67
|
+
/**
|
|
68
|
+
* Programmatically sets the address value.
|
|
69
|
+
* @param address The address object to set.
|
|
70
|
+
*/
|
|
71
|
+
set value(address: AddressValue);
|
|
62
72
|
private injectStyles;
|
|
63
73
|
private bindEvents;
|
|
64
74
|
private render;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { InferConfig } from '@pro6pp/infer-core';
|
|
1
|
+
import { InferConfig, AddressValue } from '@pro6pp/infer-core';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Configuration options for the JS Infer SDK.
|
|
@@ -59,6 +59,16 @@ declare class InferJS {
|
|
|
59
59
|
* @param config Configuration options for the API and UI.
|
|
60
60
|
*/
|
|
61
61
|
constructor(target: string | HTMLElement, config: InferJSConfig);
|
|
62
|
+
/**
|
|
63
|
+
* Retrieves the current selected address value if available.
|
|
64
|
+
* @returns The AddressValue object if valid, otherwise null.
|
|
65
|
+
*/
|
|
66
|
+
get value(): AddressValue | null;
|
|
67
|
+
/**
|
|
68
|
+
* Programmatically sets the address value.
|
|
69
|
+
* @param address The address object to set.
|
|
70
|
+
*/
|
|
71
|
+
set value(address: AddressValue);
|
|
62
72
|
private injectStyles;
|
|
63
73
|
private bindEvents;
|
|
64
74
|
private render;
|
package/dist/index.global.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"use strict";var Pro6PP=(()=>{var m=Object.defineProperty,A=Object.defineProperties,k=Object.getOwnPropertyDescriptor,_=Object.getOwnPropertyDescriptors,B=Object.getOwnPropertyNames,
|
|
1
|
+
"use strict";var Pro6PP=(()=>{var m=Object.defineProperty,A=Object.defineProperties,k=Object.getOwnPropertyDescriptor,_=Object.getOwnPropertyDescriptors,B=Object.getOwnPropertyNames,C=Object.getOwnPropertySymbols;var E=Object.prototype.hasOwnProperty,D=Object.prototype.propertyIsEnumerable;var I=(o,e,t)=>e in o?m(o,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):o[e]=t,c=(o,e)=>{for(var t in e||(e={}))E.call(e,t)&&I(o,t,e[t]);if(C)for(var t of C(e))D.call(e,t)&&I(o,t,e[t]);return o},b=(o,e)=>A(o,_(e));var F=(o,e)=>{for(var t in e)m(o,t,{get:e[t],enumerable:!0})},R=(o,e,t,i)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of B(e))!E.call(o,n)&&n!==t&&m(o,n,{get:()=>e[n],enumerable:!(i=k(e,n))||i.enumerable});return o};var $=o=>R(m({},"__esModule",{value:!0}),o);var U={};F(U,{InferJS:()=>y,attach:()=>N});var f={API_URL:"https://api.pro6pp.nl/v2",LIMIT:20,DEBOUNCE_MS:150,MIN_DEBOUNCE_MS:50,MAX_RETRIES:0},L={DIGITS_1_3:/^[0-9]{1,3}$/},T={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||f.LIMIT,this.currentLimit=this.baseLimit;let t=e.maxRetries!==void 0?e.maxRetries:f.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=c({},T);let i=e.debounceMs!==void 0?e.debounceMs:f.DEBOUNCE_MS,n=Math.max(i,f.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}),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 d=t;if(n&&Object.keys(n).length>0){let{street:l,street_number:a,city:h,addition:p}=n;if(l&&a&&h){let g=p?` ${p}`:"";d=`${l} ${a}${g}, ${h}`}}return this.finishSelection(d,n),!0}let s=typeof e!="string"?e.subtitle:null;return this.processSelection(i,s),!1}shouldAutoInsertComma(e){if(!e.includes(",")&&L.DIGITS_1_3.test(e.trim()))return!0;if(this.state.stage==="street_number"){let i=this.getCurrentFragment(e);return L.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);!a||!a.includes(t)?r=a?`${a} ${e}, ${t}, `:`${e}, ${t}, `:r=a?`${a} ${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,h;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=(h=this.abortController)==null?void 0:h.signal,r=this.explicitApiUrl?this.explicitApiUrl:`${f.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 d=r.includes("?")?"&":"?",l=`${r}${d}${s.toString()}`;this.fetcher(l,{signal:n}).then(p=>{if(!p.ok){if(t<this.maxRetries&&(p.status>=500||p.status===429))return this.retry(e,t,n);throw new Error("Network error")}return p.json()}).then(p=>{p&&this.mapResponseToState(p)}).catch(p=>{if(p.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 d,l;let t={stage:e.stage,isLoading:!1},i=e.suggestions||[],n=[],r=new Set;for(let a of i){let h=`${a.label}|${a.subtitle||""}|${JSON.stringify(a.value||{})}`;r.has(h)||(r.add(h),n.push(a))}let s=n.length+(((d=e.cities)==null?void 0:d.length)||0)+(((l=e.streets)==null?void 0:l.length)||0);t.hasMore=s>=this.currentLimit,e.stage==="mixed"?(t.cities=e.cities||[],t.streets=e.streets||[],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(c({},T),{query:this.state.query}))}updateState(e){this.state=c(c({},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 S=`
|
|
2
2
|
.pro6pp-wrapper {
|
|
3
3
|
position: relative;
|
|
4
4
|
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
|
|
@@ -20,6 +20,12 @@
|
|
|
20
20
|
appearance: none;
|
|
21
21
|
transition: border-color 0.2s, box-shadow 0.2s;
|
|
22
22
|
}
|
|
23
|
+
|
|
24
|
+
.pro6pp-input::placeholder {
|
|
25
|
+
font-size: 16px;
|
|
26
|
+
color: #a3a3a3;
|
|
27
|
+
}
|
|
28
|
+
|
|
23
29
|
.pro6pp-input:focus {
|
|
24
30
|
outline: none;
|
|
25
31
|
border-color: #3b82f6;
|
|
@@ -42,8 +48,8 @@
|
|
|
42
48
|
.pro6pp-clear-button {
|
|
43
49
|
background: none;
|
|
44
50
|
border: none;
|
|
45
|
-
width:
|
|
46
|
-
height:
|
|
51
|
+
width: 32px;
|
|
52
|
+
height: 32px;
|
|
47
53
|
cursor: pointer;
|
|
48
54
|
color: #a3a3a3;
|
|
49
55
|
display: flex;
|
|
@@ -88,7 +94,7 @@
|
|
|
88
94
|
box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
|
|
89
95
|
z-index: 9999;
|
|
90
96
|
padding: 0;
|
|
91
|
-
max-height:
|
|
97
|
+
max-height: 280px;
|
|
92
98
|
overflow-y: auto;
|
|
93
99
|
display: flex;
|
|
94
100
|
flex-direction: column;
|
|
@@ -96,7 +102,7 @@
|
|
|
96
102
|
|
|
97
103
|
@media (max-height: 500px) {
|
|
98
104
|
.pro6pp-dropdown {
|
|
99
|
-
max-height:
|
|
105
|
+
max-height: 180px;
|
|
100
106
|
}
|
|
101
107
|
}
|
|
102
108
|
|
|
@@ -112,7 +118,8 @@
|
|
|
112
118
|
cursor: pointer;
|
|
113
119
|
display: flex;
|
|
114
120
|
align-items: center;
|
|
115
|
-
font-size:
|
|
121
|
+
font-size: 15px;
|
|
122
|
+
line-height: 1.4;
|
|
116
123
|
color: #374151;
|
|
117
124
|
border-bottom: 1px solid #f3f4f6;
|
|
118
125
|
transition: background-color 0.1s;
|
|
@@ -135,18 +142,24 @@
|
|
|
135
142
|
|
|
136
143
|
.pro6pp-item__label {
|
|
137
144
|
font-weight: 500;
|
|
138
|
-
flex-shrink:
|
|
145
|
+
flex-shrink: 1;
|
|
146
|
+
overflow: hidden;
|
|
147
|
+
text-overflow: ellipsis;
|
|
148
|
+
white-space: nowrap;
|
|
139
149
|
}
|
|
150
|
+
|
|
140
151
|
.pro6pp-item__subtitle {
|
|
141
|
-
font-size:
|
|
152
|
+
font-size: 13px;
|
|
142
153
|
color: #6b7280;
|
|
143
|
-
flex-
|
|
154
|
+
flex-shrink: 0;
|
|
144
155
|
}
|
|
156
|
+
|
|
145
157
|
.pro6pp-item__chevron {
|
|
146
158
|
color: #d1d5db;
|
|
147
159
|
display: flex;
|
|
148
160
|
align-items: center;
|
|
149
161
|
margin-left: auto;
|
|
162
|
+
padding-left: 8px;
|
|
150
163
|
}
|
|
151
164
|
|
|
152
165
|
.pro6pp-no-results {
|
|
@@ -170,6 +183,24 @@
|
|
|
170
183
|
touch-action: manipulation;
|
|
171
184
|
}
|
|
172
185
|
|
|
186
|
+
@media (max-width: 640px) {
|
|
187
|
+
.pro6pp-input {
|
|
188
|
+
font-size: 16px;
|
|
189
|
+
padding: 10px 12px;
|
|
190
|
+
}
|
|
191
|
+
.pro6pp-item {
|
|
192
|
+
padding: 10px 12px;
|
|
193
|
+
font-size: 14px;
|
|
194
|
+
}
|
|
195
|
+
.pro6pp-item__subtitle {
|
|
196
|
+
font-size: 12px;
|
|
197
|
+
}
|
|
198
|
+
.pro6pp-load-more {
|
|
199
|
+
padding: 12px;
|
|
200
|
+
font-size: 13px;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
173
204
|
.pro6pp-load-more:active {
|
|
174
205
|
background-color: #f3f4f6;
|
|
175
206
|
}
|
|
@@ -177,13 +208,13 @@
|
|
|
177
208
|
@keyframes pro6pp-spin {
|
|
178
209
|
to { transform: rotate(360deg); }
|
|
179
210
|
}
|
|
180
|
-
`;var y=class{constructor(t
|
|
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.loadMoreText=t.loadMoreText||"Show more results...",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.loader=document.createElement("div"),this.loader.className="pro6pp-loader",this.loader.style.display="none",n.appendChild(this.loader),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=`
|
|
181
212
|
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
|
|
182
213
|
<line x1="18" y1="6" x2="6" y2="18"></line>
|
|
183
214
|
<line x1="6" y1="6" x2="18" y2="18"></line>
|
|
184
215
|
</svg>
|
|
185
|
-
`,
|
|
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.loadMoreButton=document.createElement("button"),this.loadMoreButton.type="button",this.loadMoreButton.className="pro6pp-load-more",this.loadMoreButton.textContent=this.loadMoreText,this.loadMoreButton.style.display="none",this.dropdown.appendChild(this.loadMoreButton),this.core=new x(b(c({},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.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=S,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()}),this.loadMoreButton.addEventListener("click",e=>{e.preventDefault(),this.core.loadMore()}),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.loader.style.display=e.isLoading?"block":"none",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",this.loadMoreButton.style.display=e.hasMore?"block":"none",e.isLoading&&!i){let s=document.createElement("li");s.className="pro6pp-no-results",s.textContent="Loading suggestions...",this.list.appendChild(s);return}if(n){let s=document.createElement("li");s.className="pro6pp-no-results",s.textContent=this.noResultsText,this.list.appendChild(s);return}t.forEach((s,d)=>{if(!s.label)return;let l=document.createElement("li");l.className="pro6pp-item",d===e.selectedSuggestionIndex&&l.classList.add("pro6pp-item--active"),l.setAttribute("role","option"),l.setAttribute("aria-selected",d===e.selectedSuggestionIndex?"true":"false");let a=document.createElement("span");a.className="pro6pp-item__label",M(s.label,e.query).forEach(({text:u,match:w})=>{if(w){let v=document.createElement("strong");v.className="pro6pp-item__label--match",v.textContent=u,a.appendChild(v)}else a.appendChild(document.createTextNode(u))}),l.appendChild(a);let p=s.count!==void 0&&s.count!==null?s.count:"",g=s.subtitle||p;if(g!==""){let u=document.createElement("span");u.className="pro6pp-item__subtitle",u.textContent=`, ${g}`,l.appendChild(u)}if(s.value===void 0||s.value===null){let u=document.createElement("div");u.className="pro6pp-item__chevron",u.innerHTML=`
|
|
186
217
|
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
|
|
187
218
|
<polyline points="9 18 15 12 9 6"></polyline>
|
|
188
219
|
</svg>
|
|
189
|
-
`,l.appendChild(u)}l.onmousedown=u=>u.preventDefault(),l.onclick=u=>{u.stopPropagation(),this.core.selectItem(
|
|
220
|
+
`,l.appendChild(u)}l.onmousedown=u=>u.preventDefault(),l.onclick=u=>{u.stopPropagation(),this.core.selectItem(s)?(this.isOpen=!1,this.dropdown.style.display="none"):setTimeout(()=>this.input.focus(),0)},this.list.appendChild(l)})}};function N(o,e){return new y(o,e)}return $(U);})();
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
var T=Object.defineProperty,M=Object.defineProperties;var A=Object.getOwnPropertyDescriptors;var
|
|
1
|
+
var T=Object.defineProperty,M=Object.defineProperties;var A=Object.getOwnPropertyDescriptors;var w=Object.getOwnPropertySymbols;var k=Object.prototype.hasOwnProperty,_=Object.prototype.propertyIsEnumerable;var C=(a,e,t)=>e in a?T(a,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):a[e]=t,c=(a,e)=>{for(var t in e||(e={}))k.call(e,t)&&C(a,t,e[t]);if(w)for(var t of w(e))_.call(e,t)&&C(a,t,e[t]);return a},m=(a,e)=>M(a,A(e));var f={API_URL:"https://api.pro6pp.nl/v2",LIMIT:20,DEBOUNCE_MS:150,MIN_DEBOUNCE_MS:50,MAX_RETRIES:0},I={DIGITS_1_3:/^[0-9]{1,3}$/},E={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||f.LIMIT,this.currentLimit=this.baseLimit;let t=e.maxRetries!==void 0?e.maxRetries:f.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=c({},E);let i=e.debounceMs!==void 0?e.debounceMs:f.DEBOUNCE_MS,r=Math.max(i,f.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}),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 d=t;if(r&&Object.keys(r).length>0){let{street:l,street_number:o,city:h,addition:p}=r;if(l&&o&&h){let g=p?` ${p}`:"";d=`${l} ${o}${g}, ${h}`}}return this.finishSelection(d,r),!0}let s=typeof e!="string"?e.subtitle:null;return this.processSelection(i,s),!1}shouldAutoInsertComma(e){if(!e.includes(",")&&I.DIGITS_1_3.test(e.trim()))return!0;if(this.state.stage==="street_number"){let i=this.getCurrentFragment(e);return I.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);!o||!o.includes(t)?n=o?`${o} ${e}, ${t}, `:`${e}, ${t}, `:n=o?`${o} ${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,h;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=(h=this.abortController)==null?void 0:h.signal,n=this.explicitApiUrl?this.explicitApiUrl:`${f.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 d=n.includes("?")?"&":"?",l=`${n}${d}${s.toString()}`;this.fetcher(l,{signal:r}).then(p=>{if(!p.ok){if(t<this.maxRetries&&(p.status>=500||p.status===429))return this.retry(e,t,r);throw new Error("Network error")}return p.json()}).then(p=>{p&&this.mapResponseToState(p)}).catch(p=>{if(p.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 d,l;let t={stage:e.stage,isLoading:!1},i=e.suggestions||[],r=[],n=new Set;for(let o of i){let h=`${o.label}|${o.subtitle||""}|${JSON.stringify(o.value||{})}`;n.has(h)||(n.add(h),r.push(o))}let s=r.length+(((d=e.cities)==null?void 0:d.length)||0)+(((l=e.streets)==null?void 0:l.length)||0);t.hasMore=s>=this.currentLimit,e.stage==="mixed"?(t.cities=e.cities||[],t.streets=e.streets||[],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(c({},E),{query:this.state.query}))}updateState(e){this.state=c(c({},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 L(a,e){if(!e||!a)return[{text:a,match:!1}];let t=[],i=a.toLowerCase(),r=e.toLowerCase(),n=0,s=0;for(let o=0;o<a.length;o++){if(!(n<e.length&&i[o]===r[n]))continue;o>s&&t.push({text:a.slice(s,o),match:!1}),t.push({text:a[o],match:!0}),n++,s=o+1}return s<a.length&&t.push({text:a.slice(s),match:!1}),n===e.length?t:[{text:a,match:!1}]}var y=`
|
|
2
2
|
.pro6pp-wrapper {
|
|
3
3
|
position: relative;
|
|
4
4
|
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
|
|
@@ -20,6 +20,12 @@ var T=Object.defineProperty,M=Object.defineProperties;var A=Object.getOwnPropert
|
|
|
20
20
|
appearance: none;
|
|
21
21
|
transition: border-color 0.2s, box-shadow 0.2s;
|
|
22
22
|
}
|
|
23
|
+
|
|
24
|
+
.pro6pp-input::placeholder {
|
|
25
|
+
font-size: 16px;
|
|
26
|
+
color: #a3a3a3;
|
|
27
|
+
}
|
|
28
|
+
|
|
23
29
|
.pro6pp-input:focus {
|
|
24
30
|
outline: none;
|
|
25
31
|
border-color: #3b82f6;
|
|
@@ -42,8 +48,8 @@ var T=Object.defineProperty,M=Object.defineProperties;var A=Object.getOwnPropert
|
|
|
42
48
|
.pro6pp-clear-button {
|
|
43
49
|
background: none;
|
|
44
50
|
border: none;
|
|
45
|
-
width:
|
|
46
|
-
height:
|
|
51
|
+
width: 32px;
|
|
52
|
+
height: 32px;
|
|
47
53
|
cursor: pointer;
|
|
48
54
|
color: #a3a3a3;
|
|
49
55
|
display: flex;
|
|
@@ -88,7 +94,7 @@ var T=Object.defineProperty,M=Object.defineProperties;var A=Object.getOwnPropert
|
|
|
88
94
|
box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
|
|
89
95
|
z-index: 9999;
|
|
90
96
|
padding: 0;
|
|
91
|
-
max-height:
|
|
97
|
+
max-height: 280px;
|
|
92
98
|
overflow-y: auto;
|
|
93
99
|
display: flex;
|
|
94
100
|
flex-direction: column;
|
|
@@ -96,7 +102,7 @@ var T=Object.defineProperty,M=Object.defineProperties;var A=Object.getOwnPropert
|
|
|
96
102
|
|
|
97
103
|
@media (max-height: 500px) {
|
|
98
104
|
.pro6pp-dropdown {
|
|
99
|
-
max-height:
|
|
105
|
+
max-height: 180px;
|
|
100
106
|
}
|
|
101
107
|
}
|
|
102
108
|
|
|
@@ -112,7 +118,8 @@ var T=Object.defineProperty,M=Object.defineProperties;var A=Object.getOwnPropert
|
|
|
112
118
|
cursor: pointer;
|
|
113
119
|
display: flex;
|
|
114
120
|
align-items: center;
|
|
115
|
-
font-size:
|
|
121
|
+
font-size: 15px;
|
|
122
|
+
line-height: 1.4;
|
|
116
123
|
color: #374151;
|
|
117
124
|
border-bottom: 1px solid #f3f4f6;
|
|
118
125
|
transition: background-color 0.1s;
|
|
@@ -135,18 +142,24 @@ var T=Object.defineProperty,M=Object.defineProperties;var A=Object.getOwnPropert
|
|
|
135
142
|
|
|
136
143
|
.pro6pp-item__label {
|
|
137
144
|
font-weight: 500;
|
|
138
|
-
flex-shrink:
|
|
145
|
+
flex-shrink: 1;
|
|
146
|
+
overflow: hidden;
|
|
147
|
+
text-overflow: ellipsis;
|
|
148
|
+
white-space: nowrap;
|
|
139
149
|
}
|
|
150
|
+
|
|
140
151
|
.pro6pp-item__subtitle {
|
|
141
|
-
font-size:
|
|
152
|
+
font-size: 13px;
|
|
142
153
|
color: #6b7280;
|
|
143
|
-
flex-
|
|
154
|
+
flex-shrink: 0;
|
|
144
155
|
}
|
|
156
|
+
|
|
145
157
|
.pro6pp-item__chevron {
|
|
146
158
|
color: #d1d5db;
|
|
147
159
|
display: flex;
|
|
148
160
|
align-items: center;
|
|
149
161
|
margin-left: auto;
|
|
162
|
+
padding-left: 8px;
|
|
150
163
|
}
|
|
151
164
|
|
|
152
165
|
.pro6pp-no-results {
|
|
@@ -170,6 +183,24 @@ var T=Object.defineProperty,M=Object.defineProperties;var A=Object.getOwnPropert
|
|
|
170
183
|
touch-action: manipulation;
|
|
171
184
|
}
|
|
172
185
|
|
|
186
|
+
@media (max-width: 640px) {
|
|
187
|
+
.pro6pp-input {
|
|
188
|
+
font-size: 16px;
|
|
189
|
+
padding: 10px 12px;
|
|
190
|
+
}
|
|
191
|
+
.pro6pp-item {
|
|
192
|
+
padding: 10px 12px;
|
|
193
|
+
font-size: 14px;
|
|
194
|
+
}
|
|
195
|
+
.pro6pp-item__subtitle {
|
|
196
|
+
font-size: 12px;
|
|
197
|
+
}
|
|
198
|
+
.pro6pp-load-more {
|
|
199
|
+
padding: 12px;
|
|
200
|
+
font-size: 13px;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
173
204
|
.pro6pp-load-more:active {
|
|
174
205
|
background-color: #f3f4f6;
|
|
175
206
|
}
|
|
@@ -177,13 +208,13 @@ var T=Object.defineProperty,M=Object.defineProperties;var A=Object.getOwnPropert
|
|
|
177
208
|
@keyframes pro6pp-spin {
|
|
178
209
|
to { transform: rotate(360deg); }
|
|
179
210
|
}
|
|
180
|
-
`;var
|
|
211
|
+
`;var v=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.loadMoreText=t.loadMoreText||"Show more results...",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.loader=document.createElement("div"),this.loader.className="pro6pp-loader",this.loader.style.display="none",r.appendChild(this.loader),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=`
|
|
181
212
|
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
|
|
182
213
|
<line x1="18" y1="6" x2="6" y2="18"></line>
|
|
183
214
|
<line x1="6" y1="6" x2="18" y2="18"></line>
|
|
184
215
|
</svg>
|
|
185
|
-
`,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.loadMoreButton=document.createElement("button"),this.loadMoreButton.type="button",this.loadMoreButton.className="pro6pp-load-more",this.loadMoreButton.textContent=this.loadMoreText,this.loadMoreButton.style.display="none",this.dropdown.appendChild(this.loadMoreButton),this.core=new b(m(c({},
|
|
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.loadMoreButton=document.createElement("button"),this.loadMoreButton.type="button",this.loadMoreButton.className="pro6pp-load-more",this.loadMoreButton.textContent=this.loadMoreText,this.loadMoreButton.style.display="none",this.dropdown.appendChild(this.loadMoreButton),this.core=new b(m(c({},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.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=y,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()}),this.loadMoreButton.addEventListener("click",e=>{e.preventDefault(),this.core.loadMore()}),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.loader.style.display=e.isLoading?"block":"none",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",this.loadMoreButton.style.display=e.hasMore?"block":"none",e.isLoading&&!i){let s=document.createElement("li");s.className="pro6pp-no-results",s.textContent="Loading suggestions...",this.list.appendChild(s);return}if(r){let s=document.createElement("li");s.className="pro6pp-no-results",s.textContent=this.noResultsText,this.list.appendChild(s);return}t.forEach((s,d)=>{if(!s.label)return;let l=document.createElement("li");l.className="pro6pp-item",d===e.selectedSuggestionIndex&&l.classList.add("pro6pp-item--active"),l.setAttribute("role","option"),l.setAttribute("aria-selected",d===e.selectedSuggestionIndex?"true":"false");let o=document.createElement("span");o.className="pro6pp-item__label",L(s.label,e.query).forEach(({text:u,match:S})=>{if(S){let x=document.createElement("strong");x.className="pro6pp-item__label--match",x.textContent=u,o.appendChild(x)}else o.appendChild(document.createTextNode(u))}),l.appendChild(o);let p=s.count!==void 0&&s.count!==null?s.count:"",g=s.subtitle||p;if(g!==""){let u=document.createElement("span");u.className="pro6pp-item__subtitle",u.textContent=`, ${g}`,l.appendChild(u)}if(s.value===void 0||s.value===null){let u=document.createElement("div");u.className="pro6pp-item__chevron",u.innerHTML=`
|
|
186
217
|
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
|
|
187
218
|
<polyline points="9 18 15 12 9 6"></polyline>
|
|
188
219
|
</svg>
|
|
189
|
-
`,l.appendChild(u)}l.onmousedown=u=>u.preventDefault(),l.onclick=u=>{u.stopPropagation(),this.core.selectItem(s)?(this.isOpen=!1,this.dropdown.style.display="none"):setTimeout(()=>this.input.focus(),0)},this.list.appendChild(l)})}};function
|
|
220
|
+
`,l.appendChild(u)}l.onmousedown=u=>u.preventDefault(),l.onclick=u=>{u.stopPropagation(),this.core.selectItem(s)?(this.isOpen=!1,this.dropdown.style.display="none"):setTimeout(()=>this.input.focus(),0)},this.list.appendChild(l)})}};function Q(a,e){return new v(a,e)}export{v 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.13",
|
|
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.12"
|
|
47
47
|
},
|
|
48
48
|
"devDependencies": {
|
|
49
49
|
"@testing-library/dom": "^10.4.1",
|