@pro6pp/infer-js 0.0.2-beta.10 → 0.0.2-beta.12
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/dist/index.cjs +43 -12
- package/dist/index.global.js +43 -12
- package/dist/index.mjs +43 -12
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var m=Object.defineProperty,A=Object.defineProperties,k=Object.getOwnPropertyDescriptor,_=Object.getOwnPropertyDescriptors,B=Object.getOwnPropertyNames,C=Object.getOwnPropertySymbols;var I=Object.prototype.hasOwnProperty,D=Object.prototype.propertyIsEnumerable;var E=(r,t,e)=>t in r?m(r,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[t]=e,c=(r,t)=>{for(var e in t||(t={}))I.call(t,e)&&E(r,e,t[e]);if(C)for(var e of C(t))D.call(t,e)&&E(r,e,t[e]);return r},b=(r,t)=>A(r,_(t));var F=(r,t)=>{for(var e in t)m(r,e,{get:t[e],enumerable:!0})},R=(r,t,e,i)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of B(t))!I.call(r,s)&&s!==e&&m(r,s,{get:()=>t[s],enumerable:!(i=k(t,s))||i.enumerable});return r};var N=r=>R(m({},"__esModule",{value:!0}),r);var U={};F(U,{InferJS:()=>y,attach:()=>$});module.exports=N(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,isError:!1,isLoading:!1,hasMore:!1,selectedSuggestionIndex:-1},x=class{constructor(t){this.abortController=null;this.country=t.country,this.authKey=t.authKey,this.explicitApiUrl=t.apiUrl,this.baseLimit=t.limit||f.LIMIT,this.currentLimit=this.baseLimit;let e=t.maxRetries!==void 0?t.maxRetries:f.MAX_RETRIES;this.maxRetries=Math.max(0,Math.min(e,10)),this.fetcher=t.fetcher||((o,n)=>fetch(o,n)),this.onStateChange=t.onStateChange||(()=>{}),this.onSelect=t.onSelect||(()=>{}),this.state=c({},T);let i=t.debounceMs!==void 0?t.debounceMs:f.DEBOUNCE_MS,s=Math.max(i,f.MIN_DEBOUNCE_MS);this.debouncedFetch=this.debounce(o=>this.executeFetch(o),s)}handleInput(t){this.currentLimit=this.baseLimit;let e=this.state.stage==="final"&&t!==this.state.query;this.updateState({query:t,isValid:!1,isLoading:!!t.trim(),selectedSuggestionIndex:-1,hasMore:!1}),e&&this.onSelect(null),this.debouncedFetch(t)}loadMore(){this.state.isLoading||(this.currentLimit+=this.baseLimit,this.updateState({isLoading:!0}),this.executeFetch(this.state.query))}handleKeyDown(t){let e=t.target;if(!e)return;let i=this.state.cities.length+this.state.streets.length+this.state.suggestions.length;if(i>0){if(t.key==="ArrowDown"){t.preventDefault();let o=this.state.selectedSuggestionIndex+1;o>=i&&(o=0),this.updateState({selectedSuggestionIndex:o});return}if(t.key==="ArrowUp"){t.preventDefault();let o=this.state.selectedSuggestionIndex-1;o<0&&(o=i-1),this.updateState({selectedSuggestionIndex:o});return}if(t.key==="Enter"&&this.state.selectedSuggestionIndex>=0){t.preventDefault();let n=[...this.state.cities,...this.state.streets,...this.state.suggestions][this.state.selectedSuggestionIndex];n&&(this.selectItem(n),this.updateState({selectedSuggestionIndex:-1}));return}}let s=e.value;if(t.key===" "&&this.shouldAutoInsertComma(s)){t.preventDefault();let o=`${s.trim()}, `;this.updateQueryAndFetch(o)}}selectItem(t){this.debouncedFetch.cancel(),this.abortController&&this.abortController.abort();let e=typeof t=="string"?t:t.label,i=e;typeof t!="string"&&typeof t.value=="string"&&(i=t.value);let s=typeof t!="string"&&typeof t.value=="object"?t.value:void 0,o=!!s&&Object.keys(s).length>0;if(this.state.stage==="final"||o){let u=e;if(s&&Object.keys(s).length>0){let{street:l,street_number:a,city:h,addition:p}=s;if(l&&a&&h){let g=p?` ${p}`:"";u=`${l} ${a}${g}, ${h}`}}return this.finishSelection(u,s),!0}let n=typeof t!="string"?t.subtitle:null;return this.processSelection(i,n),!1}shouldAutoInsertComma(t){if(!t.includes(",")&&L.DIGITS_1_3.test(t.trim()))return!0;if(this.state.stage==="street_number"){let i=this.getCurrentFragment(t);return L.DIGITS_1_3.test(i)}return!1}finishSelection(t,e){this.updateState({query:t,suggestions:[],cities:[],streets:[],isValid:!0,stage:"final",hasMore:!1}),this.onSelect(e||t)}processSelection(t,e){let{stage:i,query:s}=this.state,o=s;if(e&&(i==="city"||i==="street"||i==="mixed")){if(i==="city")o=`${e}, ${t}, `;else{let a=this.getQueryPrefix(s);!a||!a.includes(e)?o=a?`${a} ${t}, ${e}, `:`${t}, ${e}, `:o=a?`${a} ${t}, `:`${t}, `}this.updateQueryAndFetch(o);return}if(i==="direct"||i==="addition"){this.finishSelection(t);return}!s.includes(",")&&(i==="city"||i==="street"||i==="street_number_first")?o=`${t}, `:(o=this.replaceLastSegment(s,t),i!=="street_number"&&(o+=", ")),this.updateQueryAndFetch(o)}executeFetch(t,e=0){var a,h;let i=(t||"").toString();if(!i.trim()){(a=this.abortController)==null||a.abort(),this.resetState();return}e===0&&(this.updateState({isError:!1}),this.abortController&&this.abortController.abort(),this.abortController=new AbortController);let s=(h=this.abortController)==null?void 0:h.signal,o=this.explicitApiUrl?this.explicitApiUrl:`${f.API_URL}/infer/${this.country.toLowerCase()}`,n=new URLSearchParams({country:this.country.toLowerCase(),query:i,limit:this.currentLimit.toString()});this.authKey&&n.set("authKey",this.authKey);let u=o.includes("?")?"&":"?",l=`${o}${u}${n.toString()}`;this.fetcher(l,{signal:s}).then(p=>{if(!p.ok){if(e<this.maxRetries&&(p.status>=500||p.status===429))return this.retry(t,e,s);throw new Error("Network error")}return p.json()}).then(p=>{p&&this.mapResponseToState(p)}).catch(p=>{if(p.name!=="AbortError"){if(e<this.maxRetries)return this.retry(t,e,s);this.updateState({isError:!0,isLoading:!1})}})}retry(t,e,i){if(i!=null&&i.aborted)return;let s=Math.pow(2,e)*200;setTimeout(()=>{i!=null&&i.aborted||this.executeFetch(t,e+1)},s)}mapResponseToState(t){var u,l;let e={stage:t.stage,isLoading:!1},i=t.suggestions||[],s=[],o=new Set;for(let a of i){let h=`${a.label}|${a.subtitle||""}|${JSON.stringify(a.value||{})}`;o.has(h)||(o.add(h),s.push(a))}let n=s.length+(((u=t.cities)==null?void 0:u.length)||0)+(((l=t.streets)==null?void 0:l.length)||0);e.hasMore=n>=this.currentLimit,t.stage==="mixed"?(e.cities=t.cities||[],e.streets=t.streets||[],e.suggestions=[]):(e.suggestions=s,e.cities=[],e.streets=[]),e.isValid=t.stage==="final",this.updateState(e),e.isValid&&s.length===1&&this.selectItem(s[0])}updateQueryAndFetch(t){this.updateState({query:t,suggestions:[],cities:[],streets:[]}),this.updateState({isLoading:!0,isValid:!1,hasMore:!1}),this.debouncedFetch(t)}replaceLastSegment(t,e){let i=t.lastIndexOf(",");return i===-1?e:`${t.slice(0,i+1)} ${e}`.trim()}getQueryPrefix(t){let e=t.lastIndexOf(",");return e===-1?"":t.slice(0,e+1).trimEnd()}getCurrentFragment(t){var e;return((e=t.split(",").slice(-1)[0])!=null?e:"").trim()}resetState(){this.updateState(b(c({},T),{query:this.state.query}))}updateState(t){this.state=c(c({},this.state),t),this.onStateChange(this.state)}debounce(t,e){let i,s=(...o)=>{i&&clearTimeout(i),i=setTimeout(()=>t.apply(this,o),e)};return s.cancel=()=>{i&&(clearTimeout(i),i=void 0)},s}};function M(r,t){if(!t||!r)return[{text:r,match:!1}];let e=[],i=r.toLowerCase(),s=t.toLowerCase(),o=0,n=0;for(let a=0;a<r.length;a++){if(!(o<t.length&&i[a]===s[o]))continue;a>n&&e.push({text:r.slice(n,a),match:!1}),e.push({text:r[a],match:!0}),o++,n=a+1}return n<r.length&&e.push({text:r.slice(n),match:!1}),o===t.length?e:[{text:r,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
|
|
211
|
+
`;var y=class{constructor(t,e){this.isOpen=!1;var o;let i=typeof t=="string"?document.querySelector(t):t;if(!i)throw new Error("InferJS: Target element not found.");if(this.noResultsText=e.noResultsText||"No results found",this.loadMoreText=e.loadMoreText||"Show more results...",this.showClearButton=e.showClearButton!==!1,this.useDefaultStyles=e.style!=="none",this.useDefaultStyles&&this.injectStyles(),this.wrapper=document.createElement("div"),this.wrapper.className="pro6pp-wrapper",i instanceof HTMLInputElement?(this.input=i,(o=this.input.parentNode)==null||o.insertBefore(this.wrapper,this.input),this.wrapper.appendChild(this.input)):(i.appendChild(this.wrapper),this.input=document.createElement("input"),this.input.type="text",e.placeholder&&(this.input.placeholder=e.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"),e.inputClass){let n=e.inputClass.split(" ");this.input.classList.add(...n)}let s=document.createElement("div");s.className="pro6pp-input-addons",this.wrapper.appendChild(s),this.loader=document.createElement("div"),this.loader.className="pro6pp-loader",this.loader.style.display="none",s.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
|
+
`,s.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({},e),{onStateChange:n=>{this.render(n),e.onStateChange&&e.onStateChange(n)},onSelect:n=>{typeof n=="string"?this.input.value=n:n&&typeof n=="object"&&(this.input.value=this.core.state.query),e.onSelect&&e.onSelect(n)}})),this.bindEvents()}injectStyles(){let t="pro6pp-styles";if(!document.getElementById(t)){let e=document.createElement("style");e.id=t,e.textContent=S,document.head.appendChild(e)}}bindEvents(){this.input.addEventListener("input",t=>{let e=t.target.value;this.isOpen=!0,this.core.handleInput(e)}),this.input.addEventListener("keydown",t=>{this.core.handleKeyDown(t)}),this.clearButton.addEventListener("click",()=>{this.core.handleInput(""),this.input.focus()}),this.loadMoreButton.addEventListener("click",t=>{t.preventDefault(),this.core.loadMore()}),document.addEventListener("mousedown",t=>{this.wrapper.contains(t.target)||(this.isOpen=!1,this.dropdown.style.display="none")}),this.input.addEventListener("focus",()=>{this.isOpen=!0,this.render(this.core.state)})}render(t){this.input.value!==t.query&&(this.input.value=t.query),this.loader.style.display=t.isLoading?"block":"none",this.showClearButton&&(this.clearButton.style.display=t.query.length>0?"flex":"none"),this.list.innerHTML="";let e=[...t.cities,...t.streets,...t.suggestions],i=e.length>0,s=!t.isLoading&&!t.isError&&t.query.length>0&&!i&&!t.isValid;if(!(this.isOpen&&(i||t.isLoading||s))){this.dropdown.style.display="none";return}if(this.dropdown.style.display="block",this.loadMoreButton.style.display=t.hasMore?"block":"none",t.isLoading&&!i){let n=document.createElement("li");n.className="pro6pp-no-results",n.textContent="Loading suggestions...",this.list.appendChild(n);return}if(s){let n=document.createElement("li");n.className="pro6pp-no-results",n.textContent=this.noResultsText,this.list.appendChild(n);return}e.forEach((n,u)=>{if(!n.label)return;let l=document.createElement("li");l.className="pro6pp-item",u===t.selectedSuggestionIndex&&l.classList.add("pro6pp-item--active"),l.setAttribute("role","option"),l.setAttribute("aria-selected",u===t.selectedSuggestionIndex?"true":"false");let a=document.createElement("span");a.className="pro6pp-item__label",M(n.label,t.query).forEach(({text:d,match:w})=>{if(w){let v=document.createElement("strong");v.className="pro6pp-item__label--match",v.textContent=d,a.appendChild(v)}else a.appendChild(document.createTextNode(d))}),l.appendChild(a);let p=n.count!==void 0&&n.count!==null?n.count:"",g=n.subtitle||p;if(g!==""){let d=document.createElement("span");d.className="pro6pp-item__subtitle",d.textContent=`, ${g}`,l.appendChild(d)}if(n.value===void 0||n.value===null){let d=document.createElement("div");d.className="pro6pp-item__chevron",d.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(
|
|
220
|
+
`,l.appendChild(d)}l.onmousedown=d=>d.preventDefault(),l.onclick=d=>{d.stopPropagation(),this.core.selectItem(n)?(this.isOpen=!1,this.dropdown.style.display="none"):setTimeout(()=>this.input.focus(),0)},this.list.appendChild(l)})}};function $(r,t){return new y(r,t)}0&&(module.exports={InferJS,attach});
|
package/dist/index.global.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"use strict";var Pro6PP=(()=>{var
|
|
1
|
+
"use strict";var Pro6PP=(()=>{var m=Object.defineProperty,A=Object.defineProperties,k=Object.getOwnPropertyDescriptor,_=Object.getOwnPropertyDescriptors,B=Object.getOwnPropertyNames,C=Object.getOwnPropertySymbols;var I=Object.prototype.hasOwnProperty,D=Object.prototype.propertyIsEnumerable;var E=(r,t,e)=>t in r?m(r,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[t]=e,c=(r,t)=>{for(var e in t||(t={}))I.call(t,e)&&E(r,e,t[e]);if(C)for(var e of C(t))D.call(t,e)&&E(r,e,t[e]);return r},b=(r,t)=>A(r,_(t));var F=(r,t)=>{for(var e in t)m(r,e,{get:t[e],enumerable:!0})},R=(r,t,e,i)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of B(t))!I.call(r,s)&&s!==e&&m(r,s,{get:()=>t[s],enumerable:!(i=k(t,s))||i.enumerable});return r};var N=r=>R(m({},"__esModule",{value:!0}),r);var U={};F(U,{InferJS:()=>y,attach:()=>$});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,isError:!1,isLoading:!1,hasMore:!1,selectedSuggestionIndex:-1},x=class{constructor(t){this.abortController=null;this.country=t.country,this.authKey=t.authKey,this.explicitApiUrl=t.apiUrl,this.baseLimit=t.limit||f.LIMIT,this.currentLimit=this.baseLimit;let e=t.maxRetries!==void 0?t.maxRetries:f.MAX_RETRIES;this.maxRetries=Math.max(0,Math.min(e,10)),this.fetcher=t.fetcher||((o,n)=>fetch(o,n)),this.onStateChange=t.onStateChange||(()=>{}),this.onSelect=t.onSelect||(()=>{}),this.state=c({},T);let i=t.debounceMs!==void 0?t.debounceMs:f.DEBOUNCE_MS,s=Math.max(i,f.MIN_DEBOUNCE_MS);this.debouncedFetch=this.debounce(o=>this.executeFetch(o),s)}handleInput(t){this.currentLimit=this.baseLimit;let e=this.state.stage==="final"&&t!==this.state.query;this.updateState({query:t,isValid:!1,isLoading:!!t.trim(),selectedSuggestionIndex:-1,hasMore:!1}),e&&this.onSelect(null),this.debouncedFetch(t)}loadMore(){this.state.isLoading||(this.currentLimit+=this.baseLimit,this.updateState({isLoading:!0}),this.executeFetch(this.state.query))}handleKeyDown(t){let e=t.target;if(!e)return;let i=this.state.cities.length+this.state.streets.length+this.state.suggestions.length;if(i>0){if(t.key==="ArrowDown"){t.preventDefault();let o=this.state.selectedSuggestionIndex+1;o>=i&&(o=0),this.updateState({selectedSuggestionIndex:o});return}if(t.key==="ArrowUp"){t.preventDefault();let o=this.state.selectedSuggestionIndex-1;o<0&&(o=i-1),this.updateState({selectedSuggestionIndex:o});return}if(t.key==="Enter"&&this.state.selectedSuggestionIndex>=0){t.preventDefault();let n=[...this.state.cities,...this.state.streets,...this.state.suggestions][this.state.selectedSuggestionIndex];n&&(this.selectItem(n),this.updateState({selectedSuggestionIndex:-1}));return}}let s=e.value;if(t.key===" "&&this.shouldAutoInsertComma(s)){t.preventDefault();let o=`${s.trim()}, `;this.updateQueryAndFetch(o)}}selectItem(t){this.debouncedFetch.cancel(),this.abortController&&this.abortController.abort();let e=typeof t=="string"?t:t.label,i=e;typeof t!="string"&&typeof t.value=="string"&&(i=t.value);let s=typeof t!="string"&&typeof t.value=="object"?t.value:void 0,o=!!s&&Object.keys(s).length>0;if(this.state.stage==="final"||o){let u=e;if(s&&Object.keys(s).length>0){let{street:l,street_number:a,city:h,addition:p}=s;if(l&&a&&h){let g=p?` ${p}`:"";u=`${l} ${a}${g}, ${h}`}}return this.finishSelection(u,s),!0}let n=typeof t!="string"?t.subtitle:null;return this.processSelection(i,n),!1}shouldAutoInsertComma(t){if(!t.includes(",")&&L.DIGITS_1_3.test(t.trim()))return!0;if(this.state.stage==="street_number"){let i=this.getCurrentFragment(t);return L.DIGITS_1_3.test(i)}return!1}finishSelection(t,e){this.updateState({query:t,suggestions:[],cities:[],streets:[],isValid:!0,stage:"final",hasMore:!1}),this.onSelect(e||t)}processSelection(t,e){let{stage:i,query:s}=this.state,o=s;if(e&&(i==="city"||i==="street"||i==="mixed")){if(i==="city")o=`${e}, ${t}, `;else{let a=this.getQueryPrefix(s);!a||!a.includes(e)?o=a?`${a} ${t}, ${e}, `:`${t}, ${e}, `:o=a?`${a} ${t}, `:`${t}, `}this.updateQueryAndFetch(o);return}if(i==="direct"||i==="addition"){this.finishSelection(t);return}!s.includes(",")&&(i==="city"||i==="street"||i==="street_number_first")?o=`${t}, `:(o=this.replaceLastSegment(s,t),i!=="street_number"&&(o+=", ")),this.updateQueryAndFetch(o)}executeFetch(t,e=0){var a,h;let i=(t||"").toString();if(!i.trim()){(a=this.abortController)==null||a.abort(),this.resetState();return}e===0&&(this.updateState({isError:!1}),this.abortController&&this.abortController.abort(),this.abortController=new AbortController);let s=(h=this.abortController)==null?void 0:h.signal,o=this.explicitApiUrl?this.explicitApiUrl:`${f.API_URL}/infer/${this.country.toLowerCase()}`,n=new URLSearchParams({country:this.country.toLowerCase(),query:i,limit:this.currentLimit.toString()});this.authKey&&n.set("authKey",this.authKey);let u=o.includes("?")?"&":"?",l=`${o}${u}${n.toString()}`;this.fetcher(l,{signal:s}).then(p=>{if(!p.ok){if(e<this.maxRetries&&(p.status>=500||p.status===429))return this.retry(t,e,s);throw new Error("Network error")}return p.json()}).then(p=>{p&&this.mapResponseToState(p)}).catch(p=>{if(p.name!=="AbortError"){if(e<this.maxRetries)return this.retry(t,e,s);this.updateState({isError:!0,isLoading:!1})}})}retry(t,e,i){if(i!=null&&i.aborted)return;let s=Math.pow(2,e)*200;setTimeout(()=>{i!=null&&i.aborted||this.executeFetch(t,e+1)},s)}mapResponseToState(t){var u,l;let e={stage:t.stage,isLoading:!1},i=t.suggestions||[],s=[],o=new Set;for(let a of i){let h=`${a.label}|${a.subtitle||""}|${JSON.stringify(a.value||{})}`;o.has(h)||(o.add(h),s.push(a))}let n=s.length+(((u=t.cities)==null?void 0:u.length)||0)+(((l=t.streets)==null?void 0:l.length)||0);e.hasMore=n>=this.currentLimit,t.stage==="mixed"?(e.cities=t.cities||[],e.streets=t.streets||[],e.suggestions=[]):(e.suggestions=s,e.cities=[],e.streets=[]),e.isValid=t.stage==="final",this.updateState(e),e.isValid&&s.length===1&&this.selectItem(s[0])}updateQueryAndFetch(t){this.updateState({query:t,suggestions:[],cities:[],streets:[]}),this.updateState({isLoading:!0,isValid:!1,hasMore:!1}),this.debouncedFetch(t)}replaceLastSegment(t,e){let i=t.lastIndexOf(",");return i===-1?e:`${t.slice(0,i+1)} ${e}`.trim()}getQueryPrefix(t){let e=t.lastIndexOf(",");return e===-1?"":t.slice(0,e+1).trimEnd()}getCurrentFragment(t){var e;return((e=t.split(",").slice(-1)[0])!=null?e:"").trim()}resetState(){this.updateState(b(c({},T),{query:this.state.query}))}updateState(t){this.state=c(c({},this.state),t),this.onStateChange(this.state)}debounce(t,e){let i,s=(...o)=>{i&&clearTimeout(i),i=setTimeout(()=>t.apply(this,o),e)};return s.cancel=()=>{i&&(clearTimeout(i),i=void 0)},s}};function M(r,t){if(!t||!r)return[{text:r,match:!1}];let e=[],i=r.toLowerCase(),s=t.toLowerCase(),o=0,n=0;for(let a=0;a<r.length;a++){if(!(o<t.length&&i[a]===s[o]))continue;a>n&&e.push({text:r.slice(n,a),match:!1}),e.push({text:r[a],match:!0}),o++,n=a+1}return n<r.length&&e.push({text:r.slice(n),match:!1}),o===t.length?e:[{text:r,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
|
|
211
|
+
`;var y=class{constructor(t,e){this.isOpen=!1;var o;let i=typeof t=="string"?document.querySelector(t):t;if(!i)throw new Error("InferJS: Target element not found.");if(this.noResultsText=e.noResultsText||"No results found",this.loadMoreText=e.loadMoreText||"Show more results...",this.showClearButton=e.showClearButton!==!1,this.useDefaultStyles=e.style!=="none",this.useDefaultStyles&&this.injectStyles(),this.wrapper=document.createElement("div"),this.wrapper.className="pro6pp-wrapper",i instanceof HTMLInputElement?(this.input=i,(o=this.input.parentNode)==null||o.insertBefore(this.wrapper,this.input),this.wrapper.appendChild(this.input)):(i.appendChild(this.wrapper),this.input=document.createElement("input"),this.input.type="text",e.placeholder&&(this.input.placeholder=e.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"),e.inputClass){let n=e.inputClass.split(" ");this.input.classList.add(...n)}let s=document.createElement("div");s.className="pro6pp-input-addons",this.wrapper.appendChild(s),this.loader=document.createElement("div"),this.loader.className="pro6pp-loader",this.loader.style.display="none",s.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
|
+
`,s.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({},e),{onStateChange:n=>{this.render(n),e.onStateChange&&e.onStateChange(n)},onSelect:n=>{typeof n=="string"?this.input.value=n:n&&typeof n=="object"&&(this.input.value=this.core.state.query),e.onSelect&&e.onSelect(n)}})),this.bindEvents()}injectStyles(){let t="pro6pp-styles";if(!document.getElementById(t)){let e=document.createElement("style");e.id=t,e.textContent=S,document.head.appendChild(e)}}bindEvents(){this.input.addEventListener("input",t=>{let e=t.target.value;this.isOpen=!0,this.core.handleInput(e)}),this.input.addEventListener("keydown",t=>{this.core.handleKeyDown(t)}),this.clearButton.addEventListener("click",()=>{this.core.handleInput(""),this.input.focus()}),this.loadMoreButton.addEventListener("click",t=>{t.preventDefault(),this.core.loadMore()}),document.addEventListener("mousedown",t=>{this.wrapper.contains(t.target)||(this.isOpen=!1,this.dropdown.style.display="none")}),this.input.addEventListener("focus",()=>{this.isOpen=!0,this.render(this.core.state)})}render(t){this.input.value!==t.query&&(this.input.value=t.query),this.loader.style.display=t.isLoading?"block":"none",this.showClearButton&&(this.clearButton.style.display=t.query.length>0?"flex":"none"),this.list.innerHTML="";let e=[...t.cities,...t.streets,...t.suggestions],i=e.length>0,s=!t.isLoading&&!t.isError&&t.query.length>0&&!i&&!t.isValid;if(!(this.isOpen&&(i||t.isLoading||s))){this.dropdown.style.display="none";return}if(this.dropdown.style.display="block",this.loadMoreButton.style.display=t.hasMore?"block":"none",t.isLoading&&!i){let n=document.createElement("li");n.className="pro6pp-no-results",n.textContent="Loading suggestions...",this.list.appendChild(n);return}if(s){let n=document.createElement("li");n.className="pro6pp-no-results",n.textContent=this.noResultsText,this.list.appendChild(n);return}e.forEach((n,u)=>{if(!n.label)return;let l=document.createElement("li");l.className="pro6pp-item",u===t.selectedSuggestionIndex&&l.classList.add("pro6pp-item--active"),l.setAttribute("role","option"),l.setAttribute("aria-selected",u===t.selectedSuggestionIndex?"true":"false");let a=document.createElement("span");a.className="pro6pp-item__label",M(n.label,t.query).forEach(({text:d,match:w})=>{if(w){let v=document.createElement("strong");v.className="pro6pp-item__label--match",v.textContent=d,a.appendChild(v)}else a.appendChild(document.createTextNode(d))}),l.appendChild(a);let p=n.count!==void 0&&n.count!==null?n.count:"",g=n.subtitle||p;if(g!==""){let d=document.createElement("span");d.className="pro6pp-item__subtitle",d.textContent=`, ${g}`,l.appendChild(d)}if(n.value===void 0||n.value===null){let d=document.createElement("div");d.className="pro6pp-item__chevron",d.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(
|
|
220
|
+
`,l.appendChild(d)}l.onmousedown=d=>d.preventDefault(),l.onclick=d=>{d.stopPropagation(),this.core.selectItem(n)?(this.isOpen=!1,this.dropdown.style.display="none"):setTimeout(()=>this.input.focus(),0)},this.list.appendChild(l)})}};function $(r,t){return new y(r,t)}return N(U);})();
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
var T=Object.defineProperty,M=Object.defineProperties;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,t,e)=>t in a?T(a,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):a[t]=e,c=(a,t)=>{for(var e in t||(t={}))k.call(t,e)&&C(a,e,t[e]);if(w)for(var e of w(t))_.call(t,e)&&C(a,e,t[e]);return a},m=(a,t)=>M(a,A(t));var f={API_URL:"https://api.pro6pp.nl/v2",LIMIT:20,DEBOUNCE_MS:150,MIN_DEBOUNCE_MS:50,MAX_RETRIES:0},E={DIGITS_1_3:/^[0-9]{1,3}$/},I={query:"",stage:null,cities:[],streets:[],suggestions:[],isValid:!1,isError:!1,isLoading:!1,hasMore:!1,selectedSuggestionIndex:-1},b=class{constructor(t){this.abortController=null;this.country=t.country,this.authKey=t.authKey,this.explicitApiUrl=t.apiUrl,this.baseLimit=t.limit||f.LIMIT,this.currentLimit=this.baseLimit;let e=t.maxRetries!==void 0?t.maxRetries:f.MAX_RETRIES;this.maxRetries=Math.max(0,Math.min(e,10)),this.fetcher=t.fetcher||((n,s)=>fetch(n,s)),this.onStateChange=t.onStateChange||(()=>{}),this.onSelect=t.onSelect||(()=>{}),this.state=c({},I);let i=t.debounceMs!==void 0?t.debounceMs:f.DEBOUNCE_MS,o=Math.max(i,f.MIN_DEBOUNCE_MS);this.debouncedFetch=this.debounce(n=>this.executeFetch(n),o)}handleInput(t){this.currentLimit=this.baseLimit;let e=this.state.stage==="final"&&t!==this.state.query;this.updateState({query:t,isValid:!1,isLoading:!!t.trim(),selectedSuggestionIndex:-1,hasMore:!1}),e&&this.onSelect(null),this.debouncedFetch(t)}loadMore(){this.state.isLoading||(this.currentLimit+=this.baseLimit,this.updateState({isLoading:!0}),this.executeFetch(this.state.query))}handleKeyDown(t){let e=t.target;if(!e)return;let i=this.state.cities.length+this.state.streets.length+this.state.suggestions.length;if(i>0){if(t.key==="ArrowDown"){t.preventDefault();let n=this.state.selectedSuggestionIndex+1;n>=i&&(n=0),this.updateState({selectedSuggestionIndex:n});return}if(t.key==="ArrowUp"){t.preventDefault();let n=this.state.selectedSuggestionIndex-1;n<0&&(n=i-1),this.updateState({selectedSuggestionIndex:n});return}if(t.key==="Enter"&&this.state.selectedSuggestionIndex>=0){t.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 o=e.value;if(t.key===" "&&this.shouldAutoInsertComma(o)){t.preventDefault();let n=`${o.trim()}, `;this.updateQueryAndFetch(n)}}selectItem(t){this.debouncedFetch.cancel(),this.abortController&&this.abortController.abort();let e=typeof t=="string"?t:t.label,i=e;typeof t!="string"&&typeof t.value=="string"&&(i=t.value);let o=typeof t!="string"&&typeof t.value=="object"?t.value:void 0,n=!!o&&Object.keys(o).length>0;if(this.state.stage==="final"||n){let u=e;if(o&&Object.keys(o).length>0){let{street:l,street_number:r,city:h,addition:p}=o;if(l&&r&&h){let g=p?` ${p}`:"";u=`${l} ${r}${g}, ${h}`}}return this.finishSelection(u,o),!0}let s=typeof t!="string"?t.subtitle:null;return this.processSelection(i,s),!1}shouldAutoInsertComma(t){if(!t.includes(",")&&E.DIGITS_1_3.test(t.trim()))return!0;if(this.state.stage==="street_number"){let i=this.getCurrentFragment(t);return E.DIGITS_1_3.test(i)}return!1}finishSelection(t,e){this.updateState({query:t,suggestions:[],cities:[],streets:[],isValid:!0,stage:"final",hasMore:!1}),this.onSelect(e||t)}processSelection(t,e){let{stage:i,query:o}=this.state,n=o;if(e&&(i==="city"||i==="street"||i==="mixed")){if(i==="city")n=`${e}, ${t}, `;else{let r=this.getQueryPrefix(o);!r||!r.includes(e)?n=r?`${r} ${t}, ${e}, `:`${t}, ${e}, `:n=r?`${r} ${t}, `:`${t}, `}this.updateQueryAndFetch(n);return}if(i==="direct"||i==="addition"){this.finishSelection(t);return}!o.includes(",")&&(i==="city"||i==="street"||i==="street_number_first")?n=`${t}, `:(n=this.replaceLastSegment(o,t),i!=="street_number"&&(n+=", ")),this.updateQueryAndFetch(n)}executeFetch(t,e=0){var r,h;let i=(t||"").toString();if(!i.trim()){(r=this.abortController)==null||r.abort(),this.resetState();return}e===0&&(this.updateState({isError:!1}),this.abortController&&this.abortController.abort(),this.abortController=new AbortController);let o=(h=this.abortController)==null?void 0:h.signal,n=this.explicitApiUrl?this.explicitApiUrl:`${f.API_URL}/infer/${this.country.toLowerCase()}`,s=new URLSearchParams({country:this.country.toLowerCase(),query:i,limit:this.currentLimit.toString()});this.authKey&&s.set("authKey",this.authKey);let u=n.includes("?")?"&":"?",l=`${n}${u}${s.toString()}`;this.fetcher(l,{signal:o}).then(p=>{if(!p.ok){if(e<this.maxRetries&&(p.status>=500||p.status===429))return this.retry(t,e,o);throw new Error("Network error")}return p.json()}).then(p=>{p&&this.mapResponseToState(p)}).catch(p=>{if(p.name!=="AbortError"){if(e<this.maxRetries)return this.retry(t,e,o);this.updateState({isError:!0,isLoading:!1})}})}retry(t,e,i){if(i!=null&&i.aborted)return;let o=Math.pow(2,e)*200;setTimeout(()=>{i!=null&&i.aborted||this.executeFetch(t,e+1)},o)}mapResponseToState(t){var u,l;let e={stage:t.stage,isLoading:!1},i=t.suggestions||[],o=[],n=new Set;for(let r of i){let h=`${r.label}|${r.subtitle||""}|${JSON.stringify(r.value||{})}`;n.has(h)||(n.add(h),o.push(r))}let s=o.length+(((u=t.cities)==null?void 0:u.length)||0)+(((l=t.streets)==null?void 0:l.length)||0);e.hasMore=s>=this.currentLimit,t.stage==="mixed"?(e.cities=t.cities||[],e.streets=t.streets||[],e.suggestions=[]):(e.suggestions=o,e.cities=[],e.streets=[]),e.isValid=t.stage==="final",this.updateState(e),e.isValid&&o.length===1&&this.selectItem(o[0])}updateQueryAndFetch(t){this.updateState({query:t,suggestions:[],cities:[],streets:[]}),this.updateState({isLoading:!0,isValid:!1,hasMore:!1}),this.debouncedFetch(t)}replaceLastSegment(t,e){let i=t.lastIndexOf(",");return i===-1?e:`${t.slice(0,i+1)} ${e}`.trim()}getQueryPrefix(t){let e=t.lastIndexOf(",");return e===-1?"":t.slice(0,e+1).trimEnd()}getCurrentFragment(t){var e;return((e=t.split(",").slice(-1)[0])!=null?e:"").trim()}resetState(){this.updateState(m(c({},I),{query:this.state.query}))}updateState(t){this.state=c(c({},this.state),t),this.onStateChange(this.state)}debounce(t,e){let i,o=(...n)=>{i&&clearTimeout(i),i=setTimeout(()=>t.apply(this,n),e)};return o.cancel=()=>{i&&(clearTimeout(i),i=void 0)},o}};function L(a,t){if(!t||!a)return[{text:a,match:!1}];let e=[],i=a.toLowerCase(),o=t.toLowerCase(),n=0,s=0;for(let r=0;r<a.length;r++){if(!(n<t.length&&i[r]===o[n]))continue;r>s&&e.push({text:a.slice(s,r),match:!1}),e.push({text:a[r],match:!0}),n++,s=r+1}return s<a.length&&e.push({text:a.slice(s),match:!1}),n===t.length?e:[{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 k=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 k=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 k=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 k=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 k=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 k=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 k=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 k=Object.getOwnPropert
|
|
|
177
208
|
@keyframes pro6pp-spin {
|
|
178
209
|
to { transform: rotate(360deg); }
|
|
179
210
|
}
|
|
180
|
-
`;var
|
|
211
|
+
`;var v=class{constructor(t,e){this.isOpen=!1;var n;let i=typeof t=="string"?document.querySelector(t):t;if(!i)throw new Error("InferJS: Target element not found.");if(this.noResultsText=e.noResultsText||"No results found",this.loadMoreText=e.loadMoreText||"Show more results...",this.showClearButton=e.showClearButton!==!1,this.useDefaultStyles=e.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",e.placeholder&&(this.input.placeholder=e.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"),e.inputClass){let s=e.inputClass.split(" ");this.input.classList.add(...s)}let o=document.createElement("div");o.className="pro6pp-input-addons",this.wrapper.appendChild(o),this.loader=document.createElement("div"),this.loader.className="pro6pp-loader",this.loader.style.display="none",o.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
|
+
`,o.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({},e),{onStateChange:s=>{this.render(s),e.onStateChange&&e.onStateChange(s)},onSelect:s=>{typeof s=="string"?this.input.value=s:s&&typeof s=="object"&&(this.input.value=this.core.state.query),e.onSelect&&e.onSelect(s)}})),this.bindEvents()}injectStyles(){let t="pro6pp-styles";if(!document.getElementById(t)){let e=document.createElement("style");e.id=t,e.textContent=y,document.head.appendChild(e)}}bindEvents(){this.input.addEventListener("input",t=>{let e=t.target.value;this.isOpen=!0,this.core.handleInput(e)}),this.input.addEventListener("keydown",t=>{this.core.handleKeyDown(t)}),this.clearButton.addEventListener("click",()=>{this.core.handleInput(""),this.input.focus()}),this.loadMoreButton.addEventListener("click",t=>{t.preventDefault(),this.core.loadMore()}),document.addEventListener("mousedown",t=>{this.wrapper.contains(t.target)||(this.isOpen=!1,this.dropdown.style.display="none")}),this.input.addEventListener("focus",()=>{this.isOpen=!0,this.render(this.core.state)})}render(t){this.input.value!==t.query&&(this.input.value=t.query),this.loader.style.display=t.isLoading?"block":"none",this.showClearButton&&(this.clearButton.style.display=t.query.length>0?"flex":"none"),this.list.innerHTML="";let e=[...t.cities,...t.streets,...t.suggestions],i=e.length>0,o=!t.isLoading&&!t.isError&&t.query.length>0&&!i&&!t.isValid;if(!(this.isOpen&&(i||t.isLoading||o))){this.dropdown.style.display="none";return}if(this.dropdown.style.display="block",this.loadMoreButton.style.display=t.hasMore?"block":"none",t.isLoading&&!i){let s=document.createElement("li");s.className="pro6pp-no-results",s.textContent="Loading suggestions...",this.list.appendChild(s);return}if(o){let s=document.createElement("li");s.className="pro6pp-no-results",s.textContent=this.noResultsText,this.list.appendChild(s);return}e.forEach((s,u)=>{if(!s.label)return;let l=document.createElement("li");l.className="pro6pp-item",u===t.selectedSuggestionIndex&&l.classList.add("pro6pp-item--active"),l.setAttribute("role","option"),l.setAttribute("aria-selected",u===t.selectedSuggestionIndex?"true":"false");let r=document.createElement("span");r.className="pro6pp-item__label",L(s.label,t.query).forEach(({text:d,match:S})=>{if(S){let x=document.createElement("strong");x.className="pro6pp-item__label--match",x.textContent=d,r.appendChild(x)}else r.appendChild(document.createTextNode(d))}),l.appendChild(r);let p=s.count!==void 0&&s.count!==null?s.count:"",g=s.subtitle||p;if(g!==""){let d=document.createElement("span");d.className="pro6pp-item__subtitle",d.textContent=`, ${g}`,l.appendChild(d)}if(s.value===void 0||s.value===null){let d=document.createElement("div");d.className="pro6pp-item__chevron",d.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(
|
|
220
|
+
`,l.appendChild(d)}l.onmousedown=d=>d.preventDefault(),l.onclick=d=>{d.stopPropagation(),this.core.selectItem(s)?(this.isOpen=!1,this.dropdown.style.display="none"):setTimeout(()=>this.input.focus(),0)},this.list.appendChild(l)})}};function K(a,t){return new v(a,t)}export{v as InferJS,K 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.12",
|
|
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.11"
|
|
47
47
|
},
|
|
48
48
|
"devDependencies": {
|
|
49
49
|
"@testing-library/dom": "^10.4.1",
|