@pro6pp/infer-js 0.0.2-beta.0 → 0.0.2-beta.10
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 +70 -55
- package/dist/index.cjs +189 -0
- package/dist/index.d.mts +57 -3
- package/dist/index.d.ts +57 -3
- package/dist/index.global.js +159 -336
- package/dist/index.mjs +161 -128
- package/package.json +35 -6
package/dist/index.mjs
CHANGED
|
@@ -1,10 +1,79 @@
|
|
|
1
|
-
|
|
2
|
-
import { InferCore } from "@pro6pp/infer-core";
|
|
3
|
-
var DEFAULT_STYLES = `
|
|
1
|
+
var T=Object.defineProperty,M=Object.defineProperties;var k=Object.getOwnPropertyDescriptors;var C=Object.getOwnPropertySymbols;var A=Object.prototype.hasOwnProperty,_=Object.prototype.propertyIsEnumerable;var w=(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={}))A.call(t,e)&&w(a,e,t[e]);if(C)for(var e of C(t))_.call(t,e)&&w(a,e,t[e]);return a},g=(a,t)=>M(a,k(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},m=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,r=Math.max(i,f.MIN_DEBOUNCE_MS);this.debouncedFetch=this.debounce(n=>this.executeFetch(n),r)}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 r=e.value;if(t.key===" "&&this.shouldAutoInsertComma(r)){t.preventDefault();let n=`${r.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 r=typeof t!="string"&&typeof t.value=="object"?t.value:void 0,n=!!r&&Object.keys(r).length>0;if(this.state.stage==="final"||n){let u=e;if(r&&Object.keys(r).length>0){let{street:l,street_number:o,city:h}=r;l&&o&&h&&(u=`${l} ${o}, ${h}`)}return this.finishSelection(u,r),!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:r}=this.state,n=r;if(e&&(i==="city"||i==="street"||i==="mixed")){if(i==="city")n=`${e}, ${t}, `;else{let o=this.getQueryPrefix(r);n=o?`${o} ${t}, ${e}, `:`${t}, ${e}, `}this.updateQueryAndFetch(n);return}if(i==="direct"||i==="addition"){this.finishSelection(t);return}!r.includes(",")&&(i==="city"||i==="street"||i==="street_number_first")?n=`${t}, `:(n=this.replaceLastSegment(r,t),i!=="street_number"&&(n+=", ")),this.updateQueryAndFetch(n)}executeFetch(t,e=0){var o,h;let i=(t||"").toString();if(!i.trim()){(o=this.abortController)==null||o.abort(),this.resetState();return}e===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({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:r}).then(d=>{if(!d.ok){if(e<this.maxRetries&&(d.status>=500||d.status===429))return this.retry(t,e,r);throw new Error("Network error")}return d.json()}).then(d=>{d&&this.mapResponseToState(d)}).catch(d=>{if(d.name!=="AbortError"){if(e<this.maxRetries)return this.retry(t,e,r);this.updateState({isError:!0,isLoading:!1})}})}retry(t,e,i){if(i!=null&&i.aborted)return;let r=Math.pow(2,e)*200;setTimeout(()=>{i!=null&&i.aborted||this.executeFetch(t,e+1)},r)}mapResponseToState(t){var u,l;let e={stage:t.stage,isLoading:!1},i=t.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+(((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=r,e.cities=[],e.streets=[]),e.isValid=t.stage==="final",this.updateState(e)}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(g(c({},I),{query:this.state.query}))}updateState(t){this.state=c(c({},this.state),t),this.onStateChange(this.state)}debounce(t,e){let i,r=(...n)=>{i&&clearTimeout(i),i=setTimeout(()=>t.apply(this,n),e)};return r.cancel=()=>{i&&(clearTimeout(i),i=void 0)},r}};function L(a,t){if(!t||!a)return[{text:a,match:!1}];let e=[],i=a.toLowerCase(),r=t.toLowerCase(),n=0,s=0;for(let o=0;o<a.length;o++){if(!(n<t.length&&i[o]===r[n]))continue;o>s&&e.push({text:a.slice(s,o),match:!1}),e.push({text:a[o],match:!0}),n++,s=o+1}return s<a.length&&e.push({text:a.slice(s),match:!1}),n===t.length?e:[{text:a,match:!1}]}var x=`
|
|
4
2
|
.pro6pp-wrapper {
|
|
5
3
|
position: relative;
|
|
6
|
-
|
|
4
|
+
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
|
|
5
|
+
box-sizing: border-box;
|
|
7
6
|
width: 100%;
|
|
7
|
+
-webkit-tap-highlight-color: transparent;
|
|
8
|
+
}
|
|
9
|
+
.pro6pp-wrapper * {
|
|
10
|
+
box-sizing: border-box;
|
|
11
|
+
}
|
|
12
|
+
.pro6pp-input {
|
|
13
|
+
width: 100%;
|
|
14
|
+
padding: 12px 14px;
|
|
15
|
+
padding-right: 48px;
|
|
16
|
+
border: 1px solid #e0e0e0;
|
|
17
|
+
border-radius: 8px;
|
|
18
|
+
font-size: 16px;
|
|
19
|
+
line-height: 1.5;
|
|
20
|
+
appearance: none;
|
|
21
|
+
transition: border-color 0.2s, box-shadow 0.2s;
|
|
22
|
+
}
|
|
23
|
+
.pro6pp-input:focus {
|
|
24
|
+
outline: none;
|
|
25
|
+
border-color: #3b82f6;
|
|
26
|
+
box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
.pro6pp-input-addons {
|
|
30
|
+
position: absolute;
|
|
31
|
+
right: 4px;
|
|
32
|
+
top: 0;
|
|
33
|
+
bottom: 0;
|
|
34
|
+
display: flex;
|
|
35
|
+
align-items: center;
|
|
36
|
+
pointer-events: none;
|
|
37
|
+
}
|
|
38
|
+
.pro6pp-input-addons > * {
|
|
39
|
+
pointer-events: auto;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
.pro6pp-clear-button {
|
|
43
|
+
background: none;
|
|
44
|
+
border: none;
|
|
45
|
+
width: 40px;
|
|
46
|
+
height: 40px;
|
|
47
|
+
cursor: pointer;
|
|
48
|
+
color: #a3a3a3;
|
|
49
|
+
display: flex;
|
|
50
|
+
align-items: center;
|
|
51
|
+
justify-content: center;
|
|
52
|
+
border-radius: 50%;
|
|
53
|
+
transition: color 0.2s, background-color 0.2s;
|
|
54
|
+
touch-action: manipulation;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
@media (hover: hover) {
|
|
58
|
+
.pro6pp-clear-button:hover {
|
|
59
|
+
color: #1f2937;
|
|
60
|
+
background-color: #f3f4f6;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
.pro6pp-clear-button:active {
|
|
65
|
+
background-color: #f3f4f6;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
.pro6pp-loader {
|
|
69
|
+
width: 20px;
|
|
70
|
+
height: 20px;
|
|
71
|
+
margin: 0 8px;
|
|
72
|
+
border: 2px solid #e0e0e0;
|
|
73
|
+
border-top-color: #6b7280;
|
|
74
|
+
border-radius: 50%;
|
|
75
|
+
animation: pro6pp-spin 0.6s linear infinite;
|
|
76
|
+
flex-shrink: 0;
|
|
8
77
|
}
|
|
9
78
|
|
|
10
79
|
.pro6pp-dropdown {
|
|
@@ -12,145 +81,109 @@ var DEFAULT_STYLES = `
|
|
|
12
81
|
top: 100%;
|
|
13
82
|
left: 0;
|
|
14
83
|
right: 0;
|
|
15
|
-
|
|
16
|
-
background
|
|
17
|
-
border: 1px solid #
|
|
18
|
-
border-radius:
|
|
19
|
-
box-shadow: 0 4px 6px -
|
|
84
|
+
margin-top: 4px;
|
|
85
|
+
background: #ffffff;
|
|
86
|
+
border: 1px solid #e5e7eb;
|
|
87
|
+
border-radius: 6px;
|
|
88
|
+
box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
|
|
89
|
+
z-index: 9999;
|
|
90
|
+
padding: 0;
|
|
91
|
+
max-height: 260px;
|
|
92
|
+
overflow-y: auto;
|
|
93
|
+
display: flex;
|
|
94
|
+
flex-direction: column;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
@media (max-height: 500px) {
|
|
98
|
+
.pro6pp-dropdown {
|
|
99
|
+
max-height: 140px;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
.pro6pp-list {
|
|
20
104
|
list-style: none;
|
|
21
105
|
margin: 0;
|
|
22
106
|
padding: 0;
|
|
23
|
-
|
|
24
|
-
overflow-y: auto;
|
|
107
|
+
width: 100%;
|
|
25
108
|
}
|
|
26
109
|
|
|
27
110
|
.pro6pp-item {
|
|
28
|
-
padding:
|
|
111
|
+
padding: 12px 14px;
|
|
29
112
|
cursor: pointer;
|
|
30
|
-
|
|
31
|
-
|
|
113
|
+
display: flex;
|
|
114
|
+
align-items: center;
|
|
32
115
|
font-size: 14px;
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
transition: background-color 0.
|
|
116
|
+
color: #374151;
|
|
117
|
+
border-bottom: 1px solid #f3f4f6;
|
|
118
|
+
transition: background-color 0.1s;
|
|
119
|
+
flex-shrink: 0;
|
|
36
120
|
}
|
|
37
121
|
|
|
38
122
|
.pro6pp-item:last-child {
|
|
39
123
|
border-bottom: none;
|
|
40
124
|
}
|
|
41
125
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
126
|
+
@media (hover: hover) {
|
|
127
|
+
.pro6pp-item:hover, .pro6pp-item--active {
|
|
128
|
+
background-color: #f9fafb;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
.pro6pp-item:active {
|
|
133
|
+
background-color: #f3f4f6;
|
|
45
134
|
}
|
|
46
135
|
|
|
136
|
+
.pro6pp-item__label {
|
|
137
|
+
font-weight: 500;
|
|
138
|
+
flex-shrink: 0;
|
|
139
|
+
}
|
|
47
140
|
.pro6pp-item__subtitle {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
margin-top: 2px;
|
|
52
|
-
}
|
|
53
|
-
`;
|
|
54
|
-
var InferJS = class {
|
|
55
|
-
constructor(target, config) {
|
|
56
|
-
const el = typeof target === "string" ? document.querySelector(target) : target;
|
|
57
|
-
if (!el || !(el instanceof HTMLInputElement)) {
|
|
58
|
-
throw new Error(`InferJS: Target element not found or is not an input.`);
|
|
59
|
-
}
|
|
60
|
-
this.input = el;
|
|
61
|
-
this.useDefaultStyles = config.style !== "none";
|
|
62
|
-
if (this.useDefaultStyles) {
|
|
63
|
-
this.injectStyles();
|
|
64
|
-
}
|
|
65
|
-
this.core = new InferCore({
|
|
66
|
-
...config,
|
|
67
|
-
onStateChange: (state) => this.render(state),
|
|
68
|
-
onSelect: (selection) => {
|
|
69
|
-
if (typeof selection === "string") {
|
|
70
|
-
this.input.value = selection;
|
|
71
|
-
} else if (selection && typeof selection === "object") {
|
|
72
|
-
this.input.value = this.core.state.query;
|
|
73
|
-
}
|
|
74
|
-
if (config.onSelect) config.onSelect(selection);
|
|
75
|
-
}
|
|
76
|
-
});
|
|
77
|
-
this.setupDOM();
|
|
78
|
-
this.bindEvents();
|
|
79
|
-
}
|
|
80
|
-
injectStyles() {
|
|
81
|
-
const styleId = "pro6pp-infer-styles";
|
|
82
|
-
if (!document.getElementById(styleId)) {
|
|
83
|
-
const styleEl = document.createElement("style");
|
|
84
|
-
styleEl.id = styleId;
|
|
85
|
-
styleEl.textContent = DEFAULT_STYLES;
|
|
86
|
-
document.head.appendChild(styleEl);
|
|
87
|
-
}
|
|
141
|
+
font-size: 14px;
|
|
142
|
+
color: #6b7280;
|
|
143
|
+
flex-grow: 1;
|
|
88
144
|
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
labelSpan.textContent = item.label;
|
|
135
|
-
li.appendChild(labelSpan);
|
|
136
|
-
if (item.subtitle) {
|
|
137
|
-
const subSpan = document.createElement("span");
|
|
138
|
-
subSpan.className = "pro6pp-item__subtitle";
|
|
139
|
-
subSpan.textContent = item.subtitle;
|
|
140
|
-
li.appendChild(subSpan);
|
|
141
|
-
}
|
|
142
|
-
li.onclick = (e) => {
|
|
143
|
-
e.stopPropagation();
|
|
144
|
-
this.core.selectItem(item);
|
|
145
|
-
};
|
|
146
|
-
this.list.appendChild(li);
|
|
147
|
-
});
|
|
148
|
-
}
|
|
149
|
-
};
|
|
150
|
-
function attach(target, config) {
|
|
151
|
-
return new InferJS(target, config);
|
|
152
|
-
}
|
|
153
|
-
export {
|
|
154
|
-
InferJS,
|
|
155
|
-
attach
|
|
156
|
-
};
|
|
145
|
+
.pro6pp-item__chevron {
|
|
146
|
+
color: #d1d5db;
|
|
147
|
+
display: flex;
|
|
148
|
+
align-items: center;
|
|
149
|
+
margin-left: auto;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
.pro6pp-no-results {
|
|
153
|
+
padding: 24px 16px;
|
|
154
|
+
color: #6b7280;
|
|
155
|
+
font-size: 15px;
|
|
156
|
+
text-align: center;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
.pro6pp-load-more {
|
|
160
|
+
width: 100%;
|
|
161
|
+
padding: 14px;
|
|
162
|
+
background: #f9fafb;
|
|
163
|
+
border: none;
|
|
164
|
+
border-top: 1px solid #e0e0e0;
|
|
165
|
+
color: #3b82f6;
|
|
166
|
+
font-size: 14px;
|
|
167
|
+
font-weight: 600;
|
|
168
|
+
cursor: pointer;
|
|
169
|
+
flex-shrink: 0;
|
|
170
|
+
touch-action: manipulation;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
.pro6pp-load-more:active {
|
|
174
|
+
background-color: #f3f4f6;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
@keyframes pro6pp-spin {
|
|
178
|
+
to { transform: rotate(360deg); }
|
|
179
|
+
}
|
|
180
|
+
`;var y=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 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
|
+
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
|
|
182
|
+
<line x1="18" y1="6" x2="6" y2="18"></line>
|
|
183
|
+
<line x1="6" y1="6" x2="18" y2="18"></line>
|
|
184
|
+
</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 m(g(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=x,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,r=!t.isLoading&&!t.isError&&t.query.length>0&&!i&&!t.isValid;if(!(this.isOpen&&(i||t.isLoading||r))){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(r){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 o=document.createElement("span");o.className="pro6pp-item__label",L(s.label,t.query).forEach(({text:p,match:S})=>{if(S){let b=document.createElement("strong");b.className="pro6pp-item__label--match",b.textContent=p,o.appendChild(b)}else o.appendChild(document.createTextNode(p))}),l.appendChild(o);let d=s.count!==void 0&&s.count!==null?s.count:"",v=s.subtitle||d;if(v!==""){let p=document.createElement("span");p.className="pro6pp-item__subtitle",p.textContent=`, ${v}`,l.appendChild(p)}if(s.value===void 0||s.value===null){let p=document.createElement("div");p.className="pro6pp-item__chevron",p.innerHTML=`
|
|
186
|
+
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
|
|
187
|
+
<polyline points="9 18 15 12 9 6"></polyline>
|
|
188
|
+
</svg>
|
|
189
|
+
`,l.appendChild(p)}l.onmousedown=p=>p.preventDefault(),l.onclick=p=>{p.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 y(a,t)}export{y as InferJS,K as attach};
|
package/package.json
CHANGED
|
@@ -1,24 +1,52 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pro6pp/infer-js",
|
|
3
|
-
"
|
|
3
|
+
"description": "Framework-agnostic JavaScript SDK for the Pro6PP Infer API.",
|
|
4
|
+
"homepage": "https://github.com/pro6pp/infer-sdk/tree/main/packages/js",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"javascript",
|
|
7
|
+
"address",
|
|
8
|
+
"autocomplete",
|
|
9
|
+
"vanilla-js",
|
|
10
|
+
"pro6pp",
|
|
11
|
+
"netherlands",
|
|
12
|
+
"germany",
|
|
13
|
+
"sdk"
|
|
14
|
+
],
|
|
15
|
+
"bugs": {
|
|
16
|
+
"url": "https://github.com/pro6pp/infer-sdk/issues"
|
|
17
|
+
},
|
|
18
|
+
"sideEffects": [
|
|
19
|
+
"*.css"
|
|
20
|
+
],
|
|
21
|
+
"version": "0.0.2-beta.10",
|
|
4
22
|
"main": "./dist/index.js",
|
|
5
23
|
"module": "./dist/index.mjs",
|
|
6
24
|
"types": "./dist/index.d.ts",
|
|
7
25
|
"unpkg": "./dist/index.global.js",
|
|
8
26
|
"jsdelivr": "./dist/index.global.js",
|
|
27
|
+
"exports": {
|
|
28
|
+
".": {
|
|
29
|
+
"types": "./dist/index.d.ts",
|
|
30
|
+
"import": "./dist/index.mjs",
|
|
31
|
+
"require": "./dist/index.js"
|
|
32
|
+
}
|
|
33
|
+
},
|
|
9
34
|
"files": [
|
|
10
35
|
"dist",
|
|
11
36
|
"README.md"
|
|
12
37
|
],
|
|
13
38
|
"scripts": {
|
|
14
|
-
"build": "tsup
|
|
15
|
-
"dev": "tsup
|
|
16
|
-
"type-check": "tsc --noEmit"
|
|
39
|
+
"build": "tsup",
|
|
40
|
+
"dev": "tsup --watch",
|
|
41
|
+
"type-check": "tsc --noEmit",
|
|
42
|
+
"test": "vitest",
|
|
43
|
+
"test:coverage": "vitest run --coverage"
|
|
17
44
|
},
|
|
18
45
|
"dependencies": {
|
|
19
|
-
"@pro6pp/infer-core": "0.0.2-beta.
|
|
46
|
+
"@pro6pp/infer-core": "0.0.2-beta.9"
|
|
20
47
|
},
|
|
21
48
|
"devDependencies": {
|
|
49
|
+
"@testing-library/dom": "^10.4.1",
|
|
22
50
|
"tsup": "^8.0.0",
|
|
23
51
|
"typescript": "^5.0.0"
|
|
24
52
|
},
|
|
@@ -28,6 +56,7 @@
|
|
|
28
56
|
"license": "MIT",
|
|
29
57
|
"repository": {
|
|
30
58
|
"type": "git",
|
|
31
|
-
"url": "https://
|
|
59
|
+
"url": "git+https://github.com/pro6pp/infer-sdk.git",
|
|
60
|
+
"directory": "packages/js"
|
|
32
61
|
}
|
|
33
62
|
}
|