@pro6pp/infer-core 0.0.2-beta.5 → 0.0.2-beta.7
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 +84 -28
- package/dist/index.d.cts +106 -33
- package/dist/index.d.ts +106 -33
- package/dist/index.global.js +85 -29
- package/dist/index.js +85 -29
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var h=Object.defineProperty;var y=Object.getOwnPropertyDescriptor;var v=Object.getOwnPropertyNames;var I=Object.prototype.hasOwnProperty;var w=(a,e,t)=>e in a?h(a,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):a[e]=t;var T=(a,e)=>{for(var t in e)h(a,t,{get:e[t],enumerable:!0})},C=(a,e,t,i)=>{if(e&&typeof e=="object"||typeof e=="function")for(let s of v(e))!I.call(a,s)&&s!==t&&h(a,s,{get:()=>e[s],enumerable:!(i=y(e,s))||i.enumerable});return a};var L=a=>C(h({},"__esModule",{value:!0}),a);var l=(a,e,t)=>w(a,typeof e!="symbol"?e+"":e,t);var _={};T(_,{DEFAULT_STYLES:()=>S,INITIAL_STATE:()=>g,InferCore:()=>f});module.exports=L(_);var c={API_URL:"https://api.pro6pp.nl/v2",LIMIT:20,DEBOUNCE_MS:150,MIN_DEBOUNCE_MS:50,MAX_RETRIES:0},x={DIGITS_1_3:/^[0-9]{1,3}$/},g={query:"",stage:null,cities:[],streets:[],suggestions:[],isValid:!1,isError:!1,isLoading:!1,hasMore:!1,selectedSuggestionIndex:-1},f=class{constructor(e){l(this,"country");l(this,"authKey");l(this,"apiUrl");l(this,"baseLimit");l(this,"currentLimit");l(this,"maxRetries");l(this,"fetcher");l(this,"onStateChange");l(this,"onSelect");l(this,"state");l(this,"abortController",null);l(this,"debouncedFetch");l(this,"isSelecting",!1);this.country=e.country,this.authKey=e.authKey,this.apiUrl=e.apiUrl||c.API_URL,this.baseLimit=e.limit||c.LIMIT,this.currentLimit=this.baseLimit;let t=e.maxRetries!==void 0?e.maxRetries:c.MAX_RETRIES;this.maxRetries=Math.max(0,Math.min(t,10)),this.fetcher=e.fetcher||((r,o)=>fetch(r,o)),this.onStateChange=e.onStateChange||(()=>{}),this.onSelect=e.onSelect||(()=>{}),this.state={...g};let i=e.debounceMs!==void 0?e.debounceMs:c.DEBOUNCE_MS,s=Math.max(i,c.MIN_DEBOUNCE_MS);this.debouncedFetch=this.debounce(r=>this.executeFetch(r),s)}handleInput(e){if(this.isSelecting){this.isSelecting=!1;return}this.currentLimit=this.baseLimit;let t=this.state.stage==="final"&&e!==this.state.query;this.updateState({query:e,isValid:!1,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 o=[...this.state.cities,...this.state.streets,...this.state.suggestions][this.state.selectedSuggestionIndex];o&&(this.selectItem(o),this.updateState({selectedSuggestionIndex:-1}));return}}let s=t.value;if(e.key===" "&&this.shouldAutoInsertComma(s)){e.preventDefault();let r=`${s.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 s=typeof e!="string"&&typeof e.value=="object"?e.value:void 0,r=!!s&&Object.keys(s).length>0;if(this.isSelecting=!0,this.state.stage==="final"||r){let n=t;if(s&&Object.keys(s).length>0){let{street:p,street_number:u,house_number:d,city:b}=s,m=u||d;p&&m&&b&&(n=`${p} ${m}, ${b}`)}this.finishSelection(n,s);return}let o=typeof e!="string"?e.subtitle:null;this.processSelection(i,o)}shouldAutoInsertComma(e){if(!e.includes(",")&&x.DIGITS_1_3.test(e.trim()))return!0;if(this.state.stage==="house_number"){let i=this.getCurrentFragment(e);return x.DIGITS_1_3.test(i)}return!1}finishSelection(e,t){this.updateState({query:e,suggestions:[],cities:[],streets:[],isValid:!0,stage:"final",hasMore:!1}),this.onSelect(t||e),setTimeout(()=>{this.isSelecting=!1},0)}processSelection(e,t){let{stage:i,query:s}=this.state,r=s;if(t&&(i==="city"||i==="street"||i==="mixed")){if(i==="city")r=`${t}, ${e}, `;else{let u=this.getQueryPrefix(s);r=u?`${u} ${e}, ${t}, `:`${e}, ${t}, `}this.updateQueryAndFetch(r);return}if(i==="direct"||i==="addition"){this.finishSelection(e);return}!s.includes(",")&&(i==="city"||i==="street"||i==="house_number_first")?r=`${e}, `:(r=this.replaceLastSegment(s,e),i!=="house_number"&&(r+=", ")),this.updateQueryAndFetch(r)}executeFetch(e,t=0){let i=(e||"").toString();if(!i.trim()){this.abortController?.abort(),this.resetState();return}t===0&&(this.updateState({isError:!1}),this.abortController&&this.abortController.abort(),this.abortController=new AbortController);let s=this.abortController?.signal,r=new URL(`${this.apiUrl}/infer/${this.country.toLowerCase()}`),o={authKey:this.authKey,query:i,limit:this.currentLimit.toString()};r.search=new URLSearchParams(o).toString(),this.fetcher(r.toString(),{signal:s}).then(n=>{if(!n.ok){if(t<this.maxRetries&&(n.status>=500||n.status===429))return this.retry(e,t,s);throw new Error("Network error")}return n.json()}).then(n=>{n&&this.mapResponseToState(n)}).catch(n=>{if(n.name!=="AbortError"){if(t<this.maxRetries)return this.retry(e,t,s);this.updateState({isError:!0,isLoading:!1})}})}retry(e,t,i){if(i?.aborted)return;let s=Math.pow(2,t)*200;setTimeout(()=>{i?.aborted||this.executeFetch(e,t+1)},s)}mapResponseToState(e){let t={stage:e.stage,isLoading:!1},i=!1,s=null,r=e.suggestions||[],o=[],n=new Set;for(let u of r){let d=`${u.label}|${u.subtitle||""}|${JSON.stringify(u.value||{})}`;n.has(d)||(n.add(d),o.push(u))}let p=o.length+(e.cities?.length||0)+(e.streets?.length||0);if(t.hasMore=p>=this.currentLimit,e.stage==="mixed"?(t.cities=e.cities||[],t.streets=e.streets||[],t.suggestions=[]):(t.suggestions=o,t.cities=[],t.streets=[],e.stage==="final"&&o.length===1&&(i=!0,s=o[0])),t.isValid=e.stage==="final",i&&s){t.query=s.label,t.suggestions=[],t.cities=[],t.streets=[],t.isValid=!0,t.hasMore=!1,this.updateState(t);let u=typeof s.value=="object"?s.value:s.label;this.onSelect(u)}else this.updateState(t)}updateQueryAndFetch(e){this.updateState({query:e,suggestions:[],cities:[],streets:[]}),this.updateState({isLoading:!0,isValid:!1,hasMore:!1}),this.debouncedFetch(e),setTimeout(()=>{this.isSelecting=!1},0)}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){return(e.split(",").slice(-1)[0]??"").trim()}resetState(){this.updateState({...g,query:this.state.query})}updateState(e){this.state={...this.state,...e},this.onStateChange(this.state)}debounce(e,t){let i,s=(...r)=>{i&&clearTimeout(i),i=setTimeout(()=>e.apply(this,r),t)};return s.cancel=()=>{i&&(clearTimeout(i),i=void 0)},s}};var S=`
|
|
2
2
|
.pro6pp-wrapper {
|
|
3
3
|
position: relative;
|
|
4
4
|
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
|
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
.pro6pp-input {
|
|
12
12
|
width: 100%;
|
|
13
13
|
padding: 10px 12px;
|
|
14
|
+
padding-right: 48px;
|
|
14
15
|
border: 1px solid #e0e0e0;
|
|
15
16
|
border-radius: 4px;
|
|
16
17
|
font-size: 16px;
|
|
@@ -22,6 +23,57 @@
|
|
|
22
23
|
border-color: #3b82f6;
|
|
23
24
|
box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
|
|
24
25
|
}
|
|
26
|
+
|
|
27
|
+
.pro6pp-input-addons {
|
|
28
|
+
position: absolute;
|
|
29
|
+
right: 6px;
|
|
30
|
+
top: 0;
|
|
31
|
+
bottom: 0;
|
|
32
|
+
display: flex;
|
|
33
|
+
align-items: center;
|
|
34
|
+
gap: 2px;
|
|
35
|
+
pointer-events: none;
|
|
36
|
+
}
|
|
37
|
+
.pro6pp-input-addons > * {
|
|
38
|
+
pointer-events: auto;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
.pro6pp-clear-button {
|
|
42
|
+
background: none;
|
|
43
|
+
border: none;
|
|
44
|
+
width: 28px;
|
|
45
|
+
height: 28px;
|
|
46
|
+
cursor: pointer;
|
|
47
|
+
color: #a3a3a3;
|
|
48
|
+
display: flex;
|
|
49
|
+
align-items: center;
|
|
50
|
+
justify-content: center;
|
|
51
|
+
border-radius: 50%;
|
|
52
|
+
transition: color 0.2s, background-color 0.2s, transform 0.1s;
|
|
53
|
+
}
|
|
54
|
+
.pro6pp-clear-button:hover {
|
|
55
|
+
color: #1f2937;
|
|
56
|
+
background-color: #f3f4f6;
|
|
57
|
+
}
|
|
58
|
+
.pro6pp-clear-button:active {
|
|
59
|
+
transform: scale(0.92);
|
|
60
|
+
}
|
|
61
|
+
.pro6pp-clear-button svg {
|
|
62
|
+
width: 18px;
|
|
63
|
+
height: 18px;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
.pro6pp-loader {
|
|
67
|
+
width: 18px;
|
|
68
|
+
height: 18px;
|
|
69
|
+
margin: 0 4px;
|
|
70
|
+
border: 2px solid #e0e0e0;
|
|
71
|
+
border-top-color: #6b7280;
|
|
72
|
+
border-radius: 50%;
|
|
73
|
+
animation: pro6pp-spin 0.6s linear infinite;
|
|
74
|
+
flex-shrink: 0;
|
|
75
|
+
}
|
|
76
|
+
|
|
25
77
|
.pro6pp-dropdown {
|
|
26
78
|
position: absolute;
|
|
27
79
|
top: 100%;
|
|
@@ -32,30 +84,32 @@
|
|
|
32
84
|
background: white;
|
|
33
85
|
border: 1px solid #e0e0e0;
|
|
34
86
|
border-radius: 4px;
|
|
35
|
-
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.
|
|
87
|
+
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
|
|
36
88
|
max-height: 300px;
|
|
37
89
|
overflow-y: auto;
|
|
90
|
+
display: flex;
|
|
91
|
+
flex-direction: column;
|
|
92
|
+
}
|
|
93
|
+
.pro6pp-list {
|
|
38
94
|
list-style: none !important;
|
|
39
95
|
padding: 0 !important;
|
|
40
96
|
margin: 0 !important;
|
|
41
|
-
|
|
97
|
+
flex-grow: 1;
|
|
42
98
|
}
|
|
43
99
|
.pro6pp-item {
|
|
44
|
-
padding: 12px
|
|
100
|
+
padding: 12px 16px;
|
|
45
101
|
cursor: pointer;
|
|
46
102
|
display: flex;
|
|
47
103
|
flex-direction: row;
|
|
48
104
|
align-items: center;
|
|
49
|
-
color: #
|
|
105
|
+
color: #111827;
|
|
50
106
|
font-size: 14px;
|
|
51
|
-
line-height: 1;
|
|
107
|
+
line-height: 1.2;
|
|
52
108
|
white-space: nowrap;
|
|
53
109
|
overflow: hidden;
|
|
54
|
-
border-radius: 0 !important;
|
|
55
|
-
margin: 0 !important;
|
|
56
110
|
}
|
|
57
111
|
.pro6pp-item:hover, .pro6pp-item--active {
|
|
58
|
-
background-color: #
|
|
112
|
+
background-color: #f9fafb;
|
|
59
113
|
}
|
|
60
114
|
.pro6pp-item__label {
|
|
61
115
|
font-weight: 500;
|
|
@@ -63,7 +117,7 @@
|
|
|
63
117
|
}
|
|
64
118
|
.pro6pp-item__subtitle {
|
|
65
119
|
font-size: 14px;
|
|
66
|
-
color: #
|
|
120
|
+
color: #6b7280;
|
|
67
121
|
overflow: hidden;
|
|
68
122
|
text-overflow: ellipsis;
|
|
69
123
|
flex-shrink: 1;
|
|
@@ -72,31 +126,33 @@
|
|
|
72
126
|
margin-left: auto;
|
|
73
127
|
display: flex;
|
|
74
128
|
align-items: center;
|
|
75
|
-
color: #
|
|
129
|
+
color: #9ca3af;
|
|
76
130
|
padding-left: 8px;
|
|
77
131
|
}
|
|
78
132
|
.pro6pp-no-results {
|
|
79
|
-
padding:
|
|
80
|
-
color: #
|
|
133
|
+
padding: 16px;
|
|
134
|
+
color: #6b7280;
|
|
81
135
|
font-size: 14px;
|
|
82
136
|
text-align: center;
|
|
83
|
-
user-select: none;
|
|
84
|
-
pointer-events: none;
|
|
85
137
|
}
|
|
86
|
-
.pro6pp-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
138
|
+
.pro6pp-load-more {
|
|
139
|
+
width: 100%;
|
|
140
|
+
padding: 10px;
|
|
141
|
+
background: #f9fafb;
|
|
142
|
+
border: none;
|
|
143
|
+
border-top: 1px solid #e0e0e0;
|
|
144
|
+
color: #3b82f6;
|
|
145
|
+
font-size: 13px;
|
|
146
|
+
font-weight: 600;
|
|
147
|
+
cursor: pointer;
|
|
148
|
+
transition: background-color 0.2s;
|
|
149
|
+
flex-shrink: 0;
|
|
150
|
+
}
|
|
151
|
+
.pro6pp-load-more:hover {
|
|
152
|
+
background-color: #f3f4f6;
|
|
98
153
|
}
|
|
154
|
+
|
|
99
155
|
@keyframes pro6pp-spin {
|
|
100
|
-
to { transform:
|
|
156
|
+
to { transform: rotate(360deg); }
|
|
101
157
|
}
|
|
102
158
|
`;0&&(module.exports={DEFAULT_STYLES,INITIAL_STATE,InferCore});
|
package/dist/index.d.cts
CHANGED
|
@@ -4,130 +4,203 @@
|
|
|
4
4
|
type CountryCode = 'NL' | 'DE';
|
|
5
5
|
/**
|
|
6
6
|
* The current step in the address inference process.
|
|
7
|
+
* - `empty`: No input yet.
|
|
8
|
+
* - `mixed`: User is prompted to choose between cities and streets.
|
|
9
|
+
* - `street`: User is selecting a street.
|
|
10
|
+
* - `city`: User is selecting a city.
|
|
11
|
+
* - `postcode`: User is entering a postcode.
|
|
12
|
+
* - `house_number`: User is entering a house number.
|
|
13
|
+
* - `house_number_first`: Specialized mode where number is entered before street.
|
|
14
|
+
* - `addition`: Selecting a house number addition (e.g., 'A', 'III').
|
|
15
|
+
* - `direct`: Direct address hit (often via postcode).
|
|
16
|
+
* - `final`: A complete, valid address has been identified.
|
|
7
17
|
*/
|
|
8
18
|
type Stage = 'empty' | 'mixed' | 'street' | 'city' | 'postcode' | 'house_number' | 'house_number_first' | 'addition' | 'direct' | 'final';
|
|
9
19
|
/**
|
|
10
|
-
* The standardized address object returned upon selection.
|
|
20
|
+
* The standardized address object returned upon a successful final selection.
|
|
11
21
|
*/
|
|
12
22
|
interface AddressValue {
|
|
23
|
+
/** The name of the street. */
|
|
13
24
|
street: string;
|
|
25
|
+
/** The name of the city/locality. */
|
|
14
26
|
city: string;
|
|
27
|
+
/** The house number (formatted). */
|
|
15
28
|
street_number?: string | number;
|
|
29
|
+
/** The house number (numeric part). */
|
|
16
30
|
house_number?: string | number;
|
|
31
|
+
/** The postal code. */
|
|
17
32
|
postcode?: string;
|
|
33
|
+
/** The full postal code including letters (country-specific). */
|
|
18
34
|
postcode_full?: string;
|
|
35
|
+
/** The house number addition or suffix. */
|
|
19
36
|
addition?: string;
|
|
20
37
|
/** Allow for extra fields if API expands. */
|
|
21
38
|
[key: string]: unknown;
|
|
22
39
|
}
|
|
23
40
|
/**
|
|
24
|
-
* A single item in the
|
|
41
|
+
* A single item in the suggestion list.
|
|
25
42
|
*/
|
|
26
43
|
interface InferResult {
|
|
27
|
-
/** The text to display in the UI (e.g. "
|
|
44
|
+
/** The text to display in the UI (e.g. "Main Street"). */
|
|
28
45
|
label: string;
|
|
29
|
-
/** The actual address data.
|
|
30
|
-
|
|
31
|
-
|
|
46
|
+
/** The actual address data.
|
|
47
|
+
* Only present if this result completes an address or represents a specific entity.
|
|
48
|
+
*/
|
|
49
|
+
value?: AddressValue | string;
|
|
50
|
+
/** Secondary information (e.g., city name when suggesting a street). */
|
|
32
51
|
subtitle?: string | null;
|
|
33
|
-
/** Number of underlying results
|
|
52
|
+
/** Number of underlying results found for this suggestion. */
|
|
34
53
|
count?: number | string;
|
|
35
54
|
}
|
|
36
55
|
/**
|
|
37
|
-
* The complete state
|
|
56
|
+
* The complete UI state managed by InferCore.
|
|
38
57
|
*/
|
|
39
58
|
interface InferState {
|
|
40
|
-
/** The current value of the input
|
|
59
|
+
/** The current text value of the search input. */
|
|
41
60
|
query: string;
|
|
42
|
-
/** The current
|
|
61
|
+
/** The current logical stage of the address lookup. */
|
|
43
62
|
stage: Stage | null;
|
|
44
|
-
/** List of
|
|
63
|
+
/** List of city suggestions (used in `mixed` stage). */
|
|
45
64
|
cities: InferResult[];
|
|
46
|
-
/** List of
|
|
65
|
+
/** List of street suggestions (used in `mixed` stage). */
|
|
47
66
|
streets: InferResult[];
|
|
48
|
-
/** General suggestions
|
|
67
|
+
/** General list of suggestions for the current stage. */
|
|
49
68
|
suggestions: InferResult[];
|
|
50
|
-
/**
|
|
69
|
+
/** Flag indicating if the current selection is a complete, valid address. */
|
|
51
70
|
isValid: boolean;
|
|
52
|
-
/**
|
|
71
|
+
/** Flag indicating if the last API request failed. */
|
|
53
72
|
isError: boolean;
|
|
54
|
-
/**
|
|
73
|
+
/** Flag indicating if a network request is currently in progress. */
|
|
55
74
|
isLoading: boolean;
|
|
75
|
+
/** Flag indicating if more results are available to load. */
|
|
76
|
+
hasMore: boolean;
|
|
56
77
|
/**
|
|
57
|
-
* The index of the currently highlighted suggestion
|
|
58
|
-
* -
|
|
59
|
-
* -
|
|
78
|
+
* The index of the currently highlighted suggestion.
|
|
79
|
+
* - `0` to `n`: An item is highlighted via keyboard navigation.
|
|
80
|
+
* - `-1`: No item is highlighted.
|
|
60
81
|
*/
|
|
61
82
|
selectedSuggestionIndex: number;
|
|
62
83
|
}
|
|
63
84
|
/**
|
|
64
|
-
* Custom fetch implementation, compatible with
|
|
65
|
-
* Useful for
|
|
85
|
+
* Custom fetch implementation, compatible with the Web Fetch API.
|
|
86
|
+
* Useful for Node.js environments or proxying requests.
|
|
66
87
|
*/
|
|
67
88
|
type Fetcher = (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>;
|
|
68
89
|
/**
|
|
69
|
-
* Configuration for Infer
|
|
90
|
+
* Configuration options for the Infer engine.
|
|
70
91
|
*/
|
|
71
92
|
interface InferConfig {
|
|
72
93
|
/**
|
|
73
|
-
* Pro6PP Authorization Key.
|
|
94
|
+
* Your Pro6PP Authorization Key.
|
|
74
95
|
*/
|
|
75
96
|
authKey: string;
|
|
76
97
|
/**
|
|
77
|
-
*
|
|
98
|
+
* The country to perform address lookups in.
|
|
78
99
|
*/
|
|
79
100
|
country: CountryCode;
|
|
80
101
|
/**
|
|
81
102
|
* Base URL for the Pro6PP API.
|
|
82
|
-
* Useful for proxying requests through your own backend.
|
|
83
103
|
* @default 'https://api.pro6pp.nl/v2'
|
|
84
104
|
*/
|
|
85
105
|
apiUrl?: string;
|
|
86
106
|
/**
|
|
87
|
-
* Custom fetch implementation.
|
|
88
|
-
*
|
|
107
|
+
* Custom fetch implementation for network requests.
|
|
108
|
+
* @default window.fetch
|
|
89
109
|
*/
|
|
90
110
|
fetcher?: Fetcher;
|
|
91
111
|
/**
|
|
92
|
-
*
|
|
93
|
-
* @default
|
|
112
|
+
* The number of results to fetch per batch.
|
|
113
|
+
* @default 20
|
|
94
114
|
*/
|
|
95
115
|
limit?: number;
|
|
96
116
|
/**
|
|
97
|
-
*
|
|
117
|
+
* The delay in milliseconds before performing the API search.
|
|
118
|
+
* Note: A lower bound of 50ms is enforced to protect API stability.
|
|
119
|
+
* @default 150
|
|
120
|
+
*/
|
|
121
|
+
debounceMs?: number;
|
|
122
|
+
/**
|
|
123
|
+
* Maximum number of retry attempts for transient network errors.
|
|
124
|
+
* @default 0
|
|
125
|
+
*/
|
|
126
|
+
maxRetries?: number;
|
|
127
|
+
/**
|
|
128
|
+
* Callback triggered whenever the internal state (suggestions, loading status, etc.) updates.
|
|
98
129
|
*/
|
|
99
130
|
onStateChange?: (state: InferState) => void;
|
|
100
131
|
/**
|
|
101
|
-
* Callback
|
|
132
|
+
* Callback triggered when a user selects an item.
|
|
133
|
+
* If the address is complete, returns an `AddressValue` object.
|
|
134
|
+
* If the selection is partial, returns a `string`.
|
|
102
135
|
*/
|
|
103
136
|
onSelect?: (selection: AddressValue | string | null) => void;
|
|
104
137
|
}
|
|
105
138
|
|
|
139
|
+
/**
|
|
140
|
+
* The initial state of the address inference engine.
|
|
141
|
+
*/
|
|
106
142
|
declare const INITIAL_STATE: InferState;
|
|
143
|
+
/**
|
|
144
|
+
* The core logic engine for Pro6PP Infer.
|
|
145
|
+
* Manages API communication, state transitions, and keyboard interaction logic.
|
|
146
|
+
*/
|
|
107
147
|
declare class InferCore {
|
|
108
148
|
private country;
|
|
109
149
|
private authKey;
|
|
110
150
|
private apiUrl;
|
|
111
|
-
private
|
|
151
|
+
private baseLimit;
|
|
152
|
+
private currentLimit;
|
|
153
|
+
private maxRetries;
|
|
112
154
|
private fetcher;
|
|
113
155
|
private onStateChange;
|
|
114
156
|
private onSelect;
|
|
157
|
+
/**
|
|
158
|
+
* The current read-only state of the engine.
|
|
159
|
+
* Use `onStateChange` to react to updates.
|
|
160
|
+
*/
|
|
115
161
|
state: InferState;
|
|
116
162
|
private abortController;
|
|
117
163
|
private debouncedFetch;
|
|
118
164
|
private isSelecting;
|
|
165
|
+
/**
|
|
166
|
+
* Initializes a new instance of the Infer engine.
|
|
167
|
+
* @param config The configuration object including API keys and callbacks.
|
|
168
|
+
*/
|
|
119
169
|
constructor(config: InferConfig);
|
|
170
|
+
/**
|
|
171
|
+
* Processes new text input from the user.
|
|
172
|
+
* Triggers a debounced API request and updates the internal state.
|
|
173
|
+
* @param value The raw string from the input field.
|
|
174
|
+
*/
|
|
120
175
|
handleInput(value: string): void;
|
|
176
|
+
/**
|
|
177
|
+
* Increases the current limit and re-fetches the query to show more results.
|
|
178
|
+
*/
|
|
179
|
+
loadMore(): void;
|
|
180
|
+
/**
|
|
181
|
+
* Handles keyboard events for the input field.
|
|
182
|
+
* Supports:
|
|
183
|
+
* - `ArrowUp`/`ArrowDown`: Navigate through the suggestion list.
|
|
184
|
+
* - `Enter`: Select the currently highlighted suggestion.
|
|
185
|
+
* - `Space`: Automatically inserts a comma if a numeric house number is detected.
|
|
186
|
+
* @param event The keyboard event from the input element.
|
|
187
|
+
*/
|
|
121
188
|
handleKeyDown(event: KeyboardEvent | {
|
|
122
189
|
key: string;
|
|
123
190
|
target: EventTarget | null;
|
|
124
191
|
preventDefault: () => void;
|
|
125
192
|
}): void;
|
|
193
|
+
/**
|
|
194
|
+
* Manually selects a suggestion or a string value.
|
|
195
|
+
* This is typically called when a user clicks a suggestion in the UI.
|
|
196
|
+
* @param item The suggestion object or string to select.
|
|
197
|
+
*/
|
|
126
198
|
selectItem(item: InferResult | string): void;
|
|
127
199
|
private shouldAutoInsertComma;
|
|
128
200
|
private finishSelection;
|
|
129
201
|
private processSelection;
|
|
130
202
|
private executeFetch;
|
|
203
|
+
private retry;
|
|
131
204
|
private mapResponseToState;
|
|
132
205
|
private updateQueryAndFetch;
|
|
133
206
|
private replaceLastSegment;
|
|
@@ -138,6 +211,6 @@ declare class InferCore {
|
|
|
138
211
|
private debounce;
|
|
139
212
|
}
|
|
140
213
|
|
|
141
|
-
declare const DEFAULT_STYLES = "\n .pro6pp-wrapper {\n position: relative;\n font-family: -apple-system, BlinkMacSystemFont, \"Segoe UI\", Roboto, Helvetica, Arial, sans-serif;\n box-sizing: border-box;\n width: 100%;\n }\n .pro6pp-wrapper * {\n box-sizing: border-box;\n }\n .pro6pp-input {\n width: 100%;\n padding: 10px 12px;\n border: 1px solid #e0e0e0;\n border-radius: 4px;\n font-size: 16px;\n line-height: 1.5;\n transition: border-color 0.2s, box-shadow 0.2s;\n }\n .pro6pp-input:focus {\n outline: none;\n border-color: #3b82f6;\n box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);\n }\n .pro6pp-dropdown {\n position: absolute;\n top: 100%;\n left: 0;\n right: 0;\n z-index: 9999;\n margin-top: 4px;\n background: white;\n border: 1px solid #e0e0e0;\n border-radius: 4px;\n box-shadow: 0 4px 12px rgba(0, 0, 0, 0.
|
|
214
|
+
declare const DEFAULT_STYLES = "\n .pro6pp-wrapper {\n position: relative;\n font-family: -apple-system, BlinkMacSystemFont, \"Segoe UI\", Roboto, Helvetica, Arial, sans-serif;\n box-sizing: border-box;\n width: 100%;\n }\n .pro6pp-wrapper * {\n box-sizing: border-box;\n }\n .pro6pp-input {\n width: 100%;\n padding: 10px 12px;\n padding-right: 48px;\n border: 1px solid #e0e0e0;\n border-radius: 4px;\n font-size: 16px;\n line-height: 1.5;\n transition: border-color 0.2s, box-shadow 0.2s;\n }\n .pro6pp-input:focus {\n outline: none;\n border-color: #3b82f6;\n box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);\n }\n\n .pro6pp-input-addons {\n position: absolute;\n right: 6px;\n top: 0;\n bottom: 0;\n display: flex;\n align-items: center;\n gap: 2px;\n pointer-events: none;\n }\n .pro6pp-input-addons > * {\n pointer-events: auto;\n }\n\n .pro6pp-clear-button {\n background: none;\n border: none;\n width: 28px;\n height: 28px;\n cursor: pointer;\n color: #a3a3a3;\n display: flex;\n align-items: center;\n justify-content: center;\n border-radius: 50%;\n transition: color 0.2s, background-color 0.2s, transform 0.1s;\n }\n .pro6pp-clear-button:hover {\n color: #1f2937;\n background-color: #f3f4f6;\n }\n .pro6pp-clear-button:active {\n transform: scale(0.92);\n }\n .pro6pp-clear-button svg {\n width: 18px;\n height: 18px;\n }\n\n .pro6pp-loader {\n width: 18px;\n height: 18px;\n margin: 0 4px;\n border: 2px solid #e0e0e0;\n border-top-color: #6b7280;\n border-radius: 50%;\n animation: pro6pp-spin 0.6s linear infinite;\n flex-shrink: 0;\n }\n\n .pro6pp-dropdown {\n position: absolute;\n top: 100%;\n left: 0;\n right: 0;\n z-index: 9999;\n margin-top: 4px;\n background: white;\n border: 1px solid #e0e0e0;\n border-radius: 4px;\n box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);\n max-height: 300px;\n overflow-y: auto;\n display: flex;\n flex-direction: column;\n }\n .pro6pp-list {\n list-style: none !important;\n padding: 0 !important;\n margin: 0 !important;\n flex-grow: 1;\n }\n .pro6pp-item {\n padding: 12px 16px;\n cursor: pointer;\n display: flex;\n flex-direction: row;\n align-items: center;\n color: #111827;\n font-size: 14px;\n line-height: 1.2;\n white-space: nowrap;\n overflow: hidden;\n }\n .pro6pp-item:hover, .pro6pp-item--active {\n background-color: #f9fafb;\n }\n .pro6pp-item__label {\n font-weight: 500;\n flex-shrink: 0;\n }\n .pro6pp-item__subtitle {\n font-size: 14px;\n color: #6b7280;\n overflow: hidden;\n text-overflow: ellipsis;\n flex-shrink: 1;\n }\n .pro6pp-item__chevron {\n margin-left: auto;\n display: flex;\n align-items: center;\n color: #9ca3af;\n padding-left: 8px;\n }\n .pro6pp-no-results {\n padding: 16px;\n color: #6b7280;\n font-size: 14px;\n text-align: center;\n }\n .pro6pp-load-more {\n width: 100%;\n padding: 10px;\n background: #f9fafb;\n border: none;\n border-top: 1px solid #e0e0e0;\n color: #3b82f6;\n font-size: 13px;\n font-weight: 600;\n cursor: pointer;\n transition: background-color 0.2s;\n flex-shrink: 0;\n }\n .pro6pp-load-more:hover {\n background-color: #f3f4f6;\n }\n\n @keyframes pro6pp-spin {\n to { transform: rotate(360deg); }\n }\n";
|
|
142
215
|
|
|
143
216
|
export { type AddressValue, type CountryCode, DEFAULT_STYLES, type Fetcher, INITIAL_STATE, type InferConfig, InferCore, type InferResult, type InferState, type Stage };
|
package/dist/index.d.ts
CHANGED
|
@@ -4,130 +4,203 @@
|
|
|
4
4
|
type CountryCode = 'NL' | 'DE';
|
|
5
5
|
/**
|
|
6
6
|
* The current step in the address inference process.
|
|
7
|
+
* - `empty`: No input yet.
|
|
8
|
+
* - `mixed`: User is prompted to choose between cities and streets.
|
|
9
|
+
* - `street`: User is selecting a street.
|
|
10
|
+
* - `city`: User is selecting a city.
|
|
11
|
+
* - `postcode`: User is entering a postcode.
|
|
12
|
+
* - `house_number`: User is entering a house number.
|
|
13
|
+
* - `house_number_first`: Specialized mode where number is entered before street.
|
|
14
|
+
* - `addition`: Selecting a house number addition (e.g., 'A', 'III').
|
|
15
|
+
* - `direct`: Direct address hit (often via postcode).
|
|
16
|
+
* - `final`: A complete, valid address has been identified.
|
|
7
17
|
*/
|
|
8
18
|
type Stage = 'empty' | 'mixed' | 'street' | 'city' | 'postcode' | 'house_number' | 'house_number_first' | 'addition' | 'direct' | 'final';
|
|
9
19
|
/**
|
|
10
|
-
* The standardized address object returned upon selection.
|
|
20
|
+
* The standardized address object returned upon a successful final selection.
|
|
11
21
|
*/
|
|
12
22
|
interface AddressValue {
|
|
23
|
+
/** The name of the street. */
|
|
13
24
|
street: string;
|
|
25
|
+
/** The name of the city/locality. */
|
|
14
26
|
city: string;
|
|
27
|
+
/** The house number (formatted). */
|
|
15
28
|
street_number?: string | number;
|
|
29
|
+
/** The house number (numeric part). */
|
|
16
30
|
house_number?: string | number;
|
|
31
|
+
/** The postal code. */
|
|
17
32
|
postcode?: string;
|
|
33
|
+
/** The full postal code including letters (country-specific). */
|
|
18
34
|
postcode_full?: string;
|
|
35
|
+
/** The house number addition or suffix. */
|
|
19
36
|
addition?: string;
|
|
20
37
|
/** Allow for extra fields if API expands. */
|
|
21
38
|
[key: string]: unknown;
|
|
22
39
|
}
|
|
23
40
|
/**
|
|
24
|
-
* A single item in the
|
|
41
|
+
* A single item in the suggestion list.
|
|
25
42
|
*/
|
|
26
43
|
interface InferResult {
|
|
27
|
-
/** The text to display in the UI (e.g. "
|
|
44
|
+
/** The text to display in the UI (e.g. "Main Street"). */
|
|
28
45
|
label: string;
|
|
29
|
-
/** The actual address data.
|
|
30
|
-
|
|
31
|
-
|
|
46
|
+
/** The actual address data.
|
|
47
|
+
* Only present if this result completes an address or represents a specific entity.
|
|
48
|
+
*/
|
|
49
|
+
value?: AddressValue | string;
|
|
50
|
+
/** Secondary information (e.g., city name when suggesting a street). */
|
|
32
51
|
subtitle?: string | null;
|
|
33
|
-
/** Number of underlying results
|
|
52
|
+
/** Number of underlying results found for this suggestion. */
|
|
34
53
|
count?: number | string;
|
|
35
54
|
}
|
|
36
55
|
/**
|
|
37
|
-
* The complete state
|
|
56
|
+
* The complete UI state managed by InferCore.
|
|
38
57
|
*/
|
|
39
58
|
interface InferState {
|
|
40
|
-
/** The current value of the input
|
|
59
|
+
/** The current text value of the search input. */
|
|
41
60
|
query: string;
|
|
42
|
-
/** The current
|
|
61
|
+
/** The current logical stage of the address lookup. */
|
|
43
62
|
stage: Stage | null;
|
|
44
|
-
/** List of
|
|
63
|
+
/** List of city suggestions (used in `mixed` stage). */
|
|
45
64
|
cities: InferResult[];
|
|
46
|
-
/** List of
|
|
65
|
+
/** List of street suggestions (used in `mixed` stage). */
|
|
47
66
|
streets: InferResult[];
|
|
48
|
-
/** General suggestions
|
|
67
|
+
/** General list of suggestions for the current stage. */
|
|
49
68
|
suggestions: InferResult[];
|
|
50
|
-
/**
|
|
69
|
+
/** Flag indicating if the current selection is a complete, valid address. */
|
|
51
70
|
isValid: boolean;
|
|
52
|
-
/**
|
|
71
|
+
/** Flag indicating if the last API request failed. */
|
|
53
72
|
isError: boolean;
|
|
54
|
-
/**
|
|
73
|
+
/** Flag indicating if a network request is currently in progress. */
|
|
55
74
|
isLoading: boolean;
|
|
75
|
+
/** Flag indicating if more results are available to load. */
|
|
76
|
+
hasMore: boolean;
|
|
56
77
|
/**
|
|
57
|
-
* The index of the currently highlighted suggestion
|
|
58
|
-
* -
|
|
59
|
-
* -
|
|
78
|
+
* The index of the currently highlighted suggestion.
|
|
79
|
+
* - `0` to `n`: An item is highlighted via keyboard navigation.
|
|
80
|
+
* - `-1`: No item is highlighted.
|
|
60
81
|
*/
|
|
61
82
|
selectedSuggestionIndex: number;
|
|
62
83
|
}
|
|
63
84
|
/**
|
|
64
|
-
* Custom fetch implementation, compatible with
|
|
65
|
-
* Useful for
|
|
85
|
+
* Custom fetch implementation, compatible with the Web Fetch API.
|
|
86
|
+
* Useful for Node.js environments or proxying requests.
|
|
66
87
|
*/
|
|
67
88
|
type Fetcher = (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>;
|
|
68
89
|
/**
|
|
69
|
-
* Configuration for Infer
|
|
90
|
+
* Configuration options for the Infer engine.
|
|
70
91
|
*/
|
|
71
92
|
interface InferConfig {
|
|
72
93
|
/**
|
|
73
|
-
* Pro6PP Authorization Key.
|
|
94
|
+
* Your Pro6PP Authorization Key.
|
|
74
95
|
*/
|
|
75
96
|
authKey: string;
|
|
76
97
|
/**
|
|
77
|
-
*
|
|
98
|
+
* The country to perform address lookups in.
|
|
78
99
|
*/
|
|
79
100
|
country: CountryCode;
|
|
80
101
|
/**
|
|
81
102
|
* Base URL for the Pro6PP API.
|
|
82
|
-
* Useful for proxying requests through your own backend.
|
|
83
103
|
* @default 'https://api.pro6pp.nl/v2'
|
|
84
104
|
*/
|
|
85
105
|
apiUrl?: string;
|
|
86
106
|
/**
|
|
87
|
-
* Custom fetch implementation.
|
|
88
|
-
*
|
|
107
|
+
* Custom fetch implementation for network requests.
|
|
108
|
+
* @default window.fetch
|
|
89
109
|
*/
|
|
90
110
|
fetcher?: Fetcher;
|
|
91
111
|
/**
|
|
92
|
-
*
|
|
93
|
-
* @default
|
|
112
|
+
* The number of results to fetch per batch.
|
|
113
|
+
* @default 20
|
|
94
114
|
*/
|
|
95
115
|
limit?: number;
|
|
96
116
|
/**
|
|
97
|
-
*
|
|
117
|
+
* The delay in milliseconds before performing the API search.
|
|
118
|
+
* Note: A lower bound of 50ms is enforced to protect API stability.
|
|
119
|
+
* @default 150
|
|
120
|
+
*/
|
|
121
|
+
debounceMs?: number;
|
|
122
|
+
/**
|
|
123
|
+
* Maximum number of retry attempts for transient network errors.
|
|
124
|
+
* @default 0
|
|
125
|
+
*/
|
|
126
|
+
maxRetries?: number;
|
|
127
|
+
/**
|
|
128
|
+
* Callback triggered whenever the internal state (suggestions, loading status, etc.) updates.
|
|
98
129
|
*/
|
|
99
130
|
onStateChange?: (state: InferState) => void;
|
|
100
131
|
/**
|
|
101
|
-
* Callback
|
|
132
|
+
* Callback triggered when a user selects an item.
|
|
133
|
+
* If the address is complete, returns an `AddressValue` object.
|
|
134
|
+
* If the selection is partial, returns a `string`.
|
|
102
135
|
*/
|
|
103
136
|
onSelect?: (selection: AddressValue | string | null) => void;
|
|
104
137
|
}
|
|
105
138
|
|
|
139
|
+
/**
|
|
140
|
+
* The initial state of the address inference engine.
|
|
141
|
+
*/
|
|
106
142
|
declare const INITIAL_STATE: InferState;
|
|
143
|
+
/**
|
|
144
|
+
* The core logic engine for Pro6PP Infer.
|
|
145
|
+
* Manages API communication, state transitions, and keyboard interaction logic.
|
|
146
|
+
*/
|
|
107
147
|
declare class InferCore {
|
|
108
148
|
private country;
|
|
109
149
|
private authKey;
|
|
110
150
|
private apiUrl;
|
|
111
|
-
private
|
|
151
|
+
private baseLimit;
|
|
152
|
+
private currentLimit;
|
|
153
|
+
private maxRetries;
|
|
112
154
|
private fetcher;
|
|
113
155
|
private onStateChange;
|
|
114
156
|
private onSelect;
|
|
157
|
+
/**
|
|
158
|
+
* The current read-only state of the engine.
|
|
159
|
+
* Use `onStateChange` to react to updates.
|
|
160
|
+
*/
|
|
115
161
|
state: InferState;
|
|
116
162
|
private abortController;
|
|
117
163
|
private debouncedFetch;
|
|
118
164
|
private isSelecting;
|
|
165
|
+
/**
|
|
166
|
+
* Initializes a new instance of the Infer engine.
|
|
167
|
+
* @param config The configuration object including API keys and callbacks.
|
|
168
|
+
*/
|
|
119
169
|
constructor(config: InferConfig);
|
|
170
|
+
/**
|
|
171
|
+
* Processes new text input from the user.
|
|
172
|
+
* Triggers a debounced API request and updates the internal state.
|
|
173
|
+
* @param value The raw string from the input field.
|
|
174
|
+
*/
|
|
120
175
|
handleInput(value: string): void;
|
|
176
|
+
/**
|
|
177
|
+
* Increases the current limit and re-fetches the query to show more results.
|
|
178
|
+
*/
|
|
179
|
+
loadMore(): void;
|
|
180
|
+
/**
|
|
181
|
+
* Handles keyboard events for the input field.
|
|
182
|
+
* Supports:
|
|
183
|
+
* - `ArrowUp`/`ArrowDown`: Navigate through the suggestion list.
|
|
184
|
+
* - `Enter`: Select the currently highlighted suggestion.
|
|
185
|
+
* - `Space`: Automatically inserts a comma if a numeric house number is detected.
|
|
186
|
+
* @param event The keyboard event from the input element.
|
|
187
|
+
*/
|
|
121
188
|
handleKeyDown(event: KeyboardEvent | {
|
|
122
189
|
key: string;
|
|
123
190
|
target: EventTarget | null;
|
|
124
191
|
preventDefault: () => void;
|
|
125
192
|
}): void;
|
|
193
|
+
/**
|
|
194
|
+
* Manually selects a suggestion or a string value.
|
|
195
|
+
* This is typically called when a user clicks a suggestion in the UI.
|
|
196
|
+
* @param item The suggestion object or string to select.
|
|
197
|
+
*/
|
|
126
198
|
selectItem(item: InferResult | string): void;
|
|
127
199
|
private shouldAutoInsertComma;
|
|
128
200
|
private finishSelection;
|
|
129
201
|
private processSelection;
|
|
130
202
|
private executeFetch;
|
|
203
|
+
private retry;
|
|
131
204
|
private mapResponseToState;
|
|
132
205
|
private updateQueryAndFetch;
|
|
133
206
|
private replaceLastSegment;
|
|
@@ -138,6 +211,6 @@ declare class InferCore {
|
|
|
138
211
|
private debounce;
|
|
139
212
|
}
|
|
140
213
|
|
|
141
|
-
declare const DEFAULT_STYLES = "\n .pro6pp-wrapper {\n position: relative;\n font-family: -apple-system, BlinkMacSystemFont, \"Segoe UI\", Roboto, Helvetica, Arial, sans-serif;\n box-sizing: border-box;\n width: 100%;\n }\n .pro6pp-wrapper * {\n box-sizing: border-box;\n }\n .pro6pp-input {\n width: 100%;\n padding: 10px 12px;\n border: 1px solid #e0e0e0;\n border-radius: 4px;\n font-size: 16px;\n line-height: 1.5;\n transition: border-color 0.2s, box-shadow 0.2s;\n }\n .pro6pp-input:focus {\n outline: none;\n border-color: #3b82f6;\n box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);\n }\n .pro6pp-dropdown {\n position: absolute;\n top: 100%;\n left: 0;\n right: 0;\n z-index: 9999;\n margin-top: 4px;\n background: white;\n border: 1px solid #e0e0e0;\n border-radius: 4px;\n box-shadow: 0 4px 12px rgba(0, 0, 0, 0.
|
|
214
|
+
declare const DEFAULT_STYLES = "\n .pro6pp-wrapper {\n position: relative;\n font-family: -apple-system, BlinkMacSystemFont, \"Segoe UI\", Roboto, Helvetica, Arial, sans-serif;\n box-sizing: border-box;\n width: 100%;\n }\n .pro6pp-wrapper * {\n box-sizing: border-box;\n }\n .pro6pp-input {\n width: 100%;\n padding: 10px 12px;\n padding-right: 48px;\n border: 1px solid #e0e0e0;\n border-radius: 4px;\n font-size: 16px;\n line-height: 1.5;\n transition: border-color 0.2s, box-shadow 0.2s;\n }\n .pro6pp-input:focus {\n outline: none;\n border-color: #3b82f6;\n box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);\n }\n\n .pro6pp-input-addons {\n position: absolute;\n right: 6px;\n top: 0;\n bottom: 0;\n display: flex;\n align-items: center;\n gap: 2px;\n pointer-events: none;\n }\n .pro6pp-input-addons > * {\n pointer-events: auto;\n }\n\n .pro6pp-clear-button {\n background: none;\n border: none;\n width: 28px;\n height: 28px;\n cursor: pointer;\n color: #a3a3a3;\n display: flex;\n align-items: center;\n justify-content: center;\n border-radius: 50%;\n transition: color 0.2s, background-color 0.2s, transform 0.1s;\n }\n .pro6pp-clear-button:hover {\n color: #1f2937;\n background-color: #f3f4f6;\n }\n .pro6pp-clear-button:active {\n transform: scale(0.92);\n }\n .pro6pp-clear-button svg {\n width: 18px;\n height: 18px;\n }\n\n .pro6pp-loader {\n width: 18px;\n height: 18px;\n margin: 0 4px;\n border: 2px solid #e0e0e0;\n border-top-color: #6b7280;\n border-radius: 50%;\n animation: pro6pp-spin 0.6s linear infinite;\n flex-shrink: 0;\n }\n\n .pro6pp-dropdown {\n position: absolute;\n top: 100%;\n left: 0;\n right: 0;\n z-index: 9999;\n margin-top: 4px;\n background: white;\n border: 1px solid #e0e0e0;\n border-radius: 4px;\n box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);\n max-height: 300px;\n overflow-y: auto;\n display: flex;\n flex-direction: column;\n }\n .pro6pp-list {\n list-style: none !important;\n padding: 0 !important;\n margin: 0 !important;\n flex-grow: 1;\n }\n .pro6pp-item {\n padding: 12px 16px;\n cursor: pointer;\n display: flex;\n flex-direction: row;\n align-items: center;\n color: #111827;\n font-size: 14px;\n line-height: 1.2;\n white-space: nowrap;\n overflow: hidden;\n }\n .pro6pp-item:hover, .pro6pp-item--active {\n background-color: #f9fafb;\n }\n .pro6pp-item__label {\n font-weight: 500;\n flex-shrink: 0;\n }\n .pro6pp-item__subtitle {\n font-size: 14px;\n color: #6b7280;\n overflow: hidden;\n text-overflow: ellipsis;\n flex-shrink: 1;\n }\n .pro6pp-item__chevron {\n margin-left: auto;\n display: flex;\n align-items: center;\n color: #9ca3af;\n padding-left: 8px;\n }\n .pro6pp-no-results {\n padding: 16px;\n color: #6b7280;\n font-size: 14px;\n text-align: center;\n }\n .pro6pp-load-more {\n width: 100%;\n padding: 10px;\n background: #f9fafb;\n border: none;\n border-top: 1px solid #e0e0e0;\n color: #3b82f6;\n font-size: 13px;\n font-weight: 600;\n cursor: pointer;\n transition: background-color 0.2s;\n flex-shrink: 0;\n }\n .pro6pp-load-more:hover {\n background-color: #f3f4f6;\n }\n\n @keyframes pro6pp-spin {\n to { transform: rotate(360deg); }\n }\n";
|
|
142
215
|
|
|
143
216
|
export { type AddressValue, type CountryCode, DEFAULT_STYLES, type Fetcher, INITIAL_STATE, type InferConfig, InferCore, type InferResult, type InferState, type Stage };
|
package/dist/index.global.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"use strict";var Pro6PPCore=(()=>{var
|
|
1
|
+
"use strict";var Pro6PPCore=(()=>{var h=Object.defineProperty;var y=Object.getOwnPropertyDescriptor;var v=Object.getOwnPropertyNames;var I=Object.prototype.hasOwnProperty;var w=(a,e,t)=>e in a?h(a,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):a[e]=t;var T=(a,e)=>{for(var t in e)h(a,t,{get:e[t],enumerable:!0})},C=(a,e,t,i)=>{if(e&&typeof e=="object"||typeof e=="function")for(let s of v(e))!I.call(a,s)&&s!==t&&h(a,s,{get:()=>e[s],enumerable:!(i=y(e,s))||i.enumerable});return a};var L=a=>C(h({},"__esModule",{value:!0}),a);var l=(a,e,t)=>w(a,typeof e!="symbol"?e+"":e,t);var _={};T(_,{DEFAULT_STYLES:()=>S,INITIAL_STATE:()=>g,InferCore:()=>f});var c={API_URL:"https://api.pro6pp.nl/v2",LIMIT:20,DEBOUNCE_MS:150,MIN_DEBOUNCE_MS:50,MAX_RETRIES:0},x={DIGITS_1_3:/^[0-9]{1,3}$/},g={query:"",stage:null,cities:[],streets:[],suggestions:[],isValid:!1,isError:!1,isLoading:!1,hasMore:!1,selectedSuggestionIndex:-1},f=class{constructor(e){l(this,"country");l(this,"authKey");l(this,"apiUrl");l(this,"baseLimit");l(this,"currentLimit");l(this,"maxRetries");l(this,"fetcher");l(this,"onStateChange");l(this,"onSelect");l(this,"state");l(this,"abortController",null);l(this,"debouncedFetch");l(this,"isSelecting",!1);this.country=e.country,this.authKey=e.authKey,this.apiUrl=e.apiUrl||c.API_URL,this.baseLimit=e.limit||c.LIMIT,this.currentLimit=this.baseLimit;let t=e.maxRetries!==void 0?e.maxRetries:c.MAX_RETRIES;this.maxRetries=Math.max(0,Math.min(t,10)),this.fetcher=e.fetcher||((r,o)=>fetch(r,o)),this.onStateChange=e.onStateChange||(()=>{}),this.onSelect=e.onSelect||(()=>{}),this.state={...g};let i=e.debounceMs!==void 0?e.debounceMs:c.DEBOUNCE_MS,s=Math.max(i,c.MIN_DEBOUNCE_MS);this.debouncedFetch=this.debounce(r=>this.executeFetch(r),s)}handleInput(e){if(this.isSelecting){this.isSelecting=!1;return}this.currentLimit=this.baseLimit;let t=this.state.stage==="final"&&e!==this.state.query;this.updateState({query:e,isValid:!1,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 o=[...this.state.cities,...this.state.streets,...this.state.suggestions][this.state.selectedSuggestionIndex];o&&(this.selectItem(o),this.updateState({selectedSuggestionIndex:-1}));return}}let s=t.value;if(e.key===" "&&this.shouldAutoInsertComma(s)){e.preventDefault();let r=`${s.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 s=typeof e!="string"&&typeof e.value=="object"?e.value:void 0,r=!!s&&Object.keys(s).length>0;if(this.isSelecting=!0,this.state.stage==="final"||r){let n=t;if(s&&Object.keys(s).length>0){let{street:p,street_number:u,house_number:d,city:b}=s,m=u||d;p&&m&&b&&(n=`${p} ${m}, ${b}`)}this.finishSelection(n,s);return}let o=typeof e!="string"?e.subtitle:null;this.processSelection(i,o)}shouldAutoInsertComma(e){if(!e.includes(",")&&x.DIGITS_1_3.test(e.trim()))return!0;if(this.state.stage==="house_number"){let i=this.getCurrentFragment(e);return x.DIGITS_1_3.test(i)}return!1}finishSelection(e,t){this.updateState({query:e,suggestions:[],cities:[],streets:[],isValid:!0,stage:"final",hasMore:!1}),this.onSelect(t||e),setTimeout(()=>{this.isSelecting=!1},0)}processSelection(e,t){let{stage:i,query:s}=this.state,r=s;if(t&&(i==="city"||i==="street"||i==="mixed")){if(i==="city")r=`${t}, ${e}, `;else{let u=this.getQueryPrefix(s);r=u?`${u} ${e}, ${t}, `:`${e}, ${t}, `}this.updateQueryAndFetch(r);return}if(i==="direct"||i==="addition"){this.finishSelection(e);return}!s.includes(",")&&(i==="city"||i==="street"||i==="house_number_first")?r=`${e}, `:(r=this.replaceLastSegment(s,e),i!=="house_number"&&(r+=", ")),this.updateQueryAndFetch(r)}executeFetch(e,t=0){let i=(e||"").toString();if(!i.trim()){this.abortController?.abort(),this.resetState();return}t===0&&(this.updateState({isError:!1}),this.abortController&&this.abortController.abort(),this.abortController=new AbortController);let s=this.abortController?.signal,r=new URL(`${this.apiUrl}/infer/${this.country.toLowerCase()}`),o={authKey:this.authKey,query:i,limit:this.currentLimit.toString()};r.search=new URLSearchParams(o).toString(),this.fetcher(r.toString(),{signal:s}).then(n=>{if(!n.ok){if(t<this.maxRetries&&(n.status>=500||n.status===429))return this.retry(e,t,s);throw new Error("Network error")}return n.json()}).then(n=>{n&&this.mapResponseToState(n)}).catch(n=>{if(n.name!=="AbortError"){if(t<this.maxRetries)return this.retry(e,t,s);this.updateState({isError:!0,isLoading:!1})}})}retry(e,t,i){if(i?.aborted)return;let s=Math.pow(2,t)*200;setTimeout(()=>{i?.aborted||this.executeFetch(e,t+1)},s)}mapResponseToState(e){let t={stage:e.stage,isLoading:!1},i=!1,s=null,r=e.suggestions||[],o=[],n=new Set;for(let u of r){let d=`${u.label}|${u.subtitle||""}|${JSON.stringify(u.value||{})}`;n.has(d)||(n.add(d),o.push(u))}let p=o.length+(e.cities?.length||0)+(e.streets?.length||0);if(t.hasMore=p>=this.currentLimit,e.stage==="mixed"?(t.cities=e.cities||[],t.streets=e.streets||[],t.suggestions=[]):(t.suggestions=o,t.cities=[],t.streets=[],e.stage==="final"&&o.length===1&&(i=!0,s=o[0])),t.isValid=e.stage==="final",i&&s){t.query=s.label,t.suggestions=[],t.cities=[],t.streets=[],t.isValid=!0,t.hasMore=!1,this.updateState(t);let u=typeof s.value=="object"?s.value:s.label;this.onSelect(u)}else this.updateState(t)}updateQueryAndFetch(e){this.updateState({query:e,suggestions:[],cities:[],streets:[]}),this.updateState({isLoading:!0,isValid:!1,hasMore:!1}),this.debouncedFetch(e),setTimeout(()=>{this.isSelecting=!1},0)}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){return(e.split(",").slice(-1)[0]??"").trim()}resetState(){this.updateState({...g,query:this.state.query})}updateState(e){this.state={...this.state,...e},this.onStateChange(this.state)}debounce(e,t){let i,s=(...r)=>{i&&clearTimeout(i),i=setTimeout(()=>e.apply(this,r),t)};return s.cancel=()=>{i&&(clearTimeout(i),i=void 0)},s}};var S=`
|
|
2
2
|
.pro6pp-wrapper {
|
|
3
3
|
position: relative;
|
|
4
4
|
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
|
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
.pro6pp-input {
|
|
12
12
|
width: 100%;
|
|
13
13
|
padding: 10px 12px;
|
|
14
|
+
padding-right: 48px;
|
|
14
15
|
border: 1px solid #e0e0e0;
|
|
15
16
|
border-radius: 4px;
|
|
16
17
|
font-size: 16px;
|
|
@@ -22,6 +23,57 @@
|
|
|
22
23
|
border-color: #3b82f6;
|
|
23
24
|
box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
|
|
24
25
|
}
|
|
26
|
+
|
|
27
|
+
.pro6pp-input-addons {
|
|
28
|
+
position: absolute;
|
|
29
|
+
right: 6px;
|
|
30
|
+
top: 0;
|
|
31
|
+
bottom: 0;
|
|
32
|
+
display: flex;
|
|
33
|
+
align-items: center;
|
|
34
|
+
gap: 2px;
|
|
35
|
+
pointer-events: none;
|
|
36
|
+
}
|
|
37
|
+
.pro6pp-input-addons > * {
|
|
38
|
+
pointer-events: auto;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
.pro6pp-clear-button {
|
|
42
|
+
background: none;
|
|
43
|
+
border: none;
|
|
44
|
+
width: 28px;
|
|
45
|
+
height: 28px;
|
|
46
|
+
cursor: pointer;
|
|
47
|
+
color: #a3a3a3;
|
|
48
|
+
display: flex;
|
|
49
|
+
align-items: center;
|
|
50
|
+
justify-content: center;
|
|
51
|
+
border-radius: 50%;
|
|
52
|
+
transition: color 0.2s, background-color 0.2s, transform 0.1s;
|
|
53
|
+
}
|
|
54
|
+
.pro6pp-clear-button:hover {
|
|
55
|
+
color: #1f2937;
|
|
56
|
+
background-color: #f3f4f6;
|
|
57
|
+
}
|
|
58
|
+
.pro6pp-clear-button:active {
|
|
59
|
+
transform: scale(0.92);
|
|
60
|
+
}
|
|
61
|
+
.pro6pp-clear-button svg {
|
|
62
|
+
width: 18px;
|
|
63
|
+
height: 18px;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
.pro6pp-loader {
|
|
67
|
+
width: 18px;
|
|
68
|
+
height: 18px;
|
|
69
|
+
margin: 0 4px;
|
|
70
|
+
border: 2px solid #e0e0e0;
|
|
71
|
+
border-top-color: #6b7280;
|
|
72
|
+
border-radius: 50%;
|
|
73
|
+
animation: pro6pp-spin 0.6s linear infinite;
|
|
74
|
+
flex-shrink: 0;
|
|
75
|
+
}
|
|
76
|
+
|
|
25
77
|
.pro6pp-dropdown {
|
|
26
78
|
position: absolute;
|
|
27
79
|
top: 100%;
|
|
@@ -32,30 +84,32 @@
|
|
|
32
84
|
background: white;
|
|
33
85
|
border: 1px solid #e0e0e0;
|
|
34
86
|
border-radius: 4px;
|
|
35
|
-
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.
|
|
87
|
+
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
|
|
36
88
|
max-height: 300px;
|
|
37
89
|
overflow-y: auto;
|
|
90
|
+
display: flex;
|
|
91
|
+
flex-direction: column;
|
|
92
|
+
}
|
|
93
|
+
.pro6pp-list {
|
|
38
94
|
list-style: none !important;
|
|
39
95
|
padding: 0 !important;
|
|
40
96
|
margin: 0 !important;
|
|
41
|
-
|
|
97
|
+
flex-grow: 1;
|
|
42
98
|
}
|
|
43
99
|
.pro6pp-item {
|
|
44
|
-
padding: 12px
|
|
100
|
+
padding: 12px 16px;
|
|
45
101
|
cursor: pointer;
|
|
46
102
|
display: flex;
|
|
47
103
|
flex-direction: row;
|
|
48
104
|
align-items: center;
|
|
49
|
-
color: #
|
|
105
|
+
color: #111827;
|
|
50
106
|
font-size: 14px;
|
|
51
|
-
line-height: 1;
|
|
107
|
+
line-height: 1.2;
|
|
52
108
|
white-space: nowrap;
|
|
53
109
|
overflow: hidden;
|
|
54
|
-
border-radius: 0 !important;
|
|
55
|
-
margin: 0 !important;
|
|
56
110
|
}
|
|
57
111
|
.pro6pp-item:hover, .pro6pp-item--active {
|
|
58
|
-
background-color: #
|
|
112
|
+
background-color: #f9fafb;
|
|
59
113
|
}
|
|
60
114
|
.pro6pp-item__label {
|
|
61
115
|
font-weight: 500;
|
|
@@ -63,7 +117,7 @@
|
|
|
63
117
|
}
|
|
64
118
|
.pro6pp-item__subtitle {
|
|
65
119
|
font-size: 14px;
|
|
66
|
-
color: #
|
|
120
|
+
color: #6b7280;
|
|
67
121
|
overflow: hidden;
|
|
68
122
|
text-overflow: ellipsis;
|
|
69
123
|
flex-shrink: 1;
|
|
@@ -72,31 +126,33 @@
|
|
|
72
126
|
margin-left: auto;
|
|
73
127
|
display: flex;
|
|
74
128
|
align-items: center;
|
|
75
|
-
color: #
|
|
129
|
+
color: #9ca3af;
|
|
76
130
|
padding-left: 8px;
|
|
77
131
|
}
|
|
78
132
|
.pro6pp-no-results {
|
|
79
|
-
padding:
|
|
80
|
-
color: #
|
|
133
|
+
padding: 16px;
|
|
134
|
+
color: #6b7280;
|
|
81
135
|
font-size: 14px;
|
|
82
136
|
text-align: center;
|
|
83
|
-
user-select: none;
|
|
84
|
-
pointer-events: none;
|
|
85
137
|
}
|
|
86
|
-
.pro6pp-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
138
|
+
.pro6pp-load-more {
|
|
139
|
+
width: 100%;
|
|
140
|
+
padding: 10px;
|
|
141
|
+
background: #f9fafb;
|
|
142
|
+
border: none;
|
|
143
|
+
border-top: 1px solid #e0e0e0;
|
|
144
|
+
color: #3b82f6;
|
|
145
|
+
font-size: 13px;
|
|
146
|
+
font-weight: 600;
|
|
147
|
+
cursor: pointer;
|
|
148
|
+
transition: background-color 0.2s;
|
|
149
|
+
flex-shrink: 0;
|
|
150
|
+
}
|
|
151
|
+
.pro6pp-load-more:hover {
|
|
152
|
+
background-color: #f3f4f6;
|
|
98
153
|
}
|
|
154
|
+
|
|
99
155
|
@keyframes pro6pp-spin {
|
|
100
|
-
to { transform:
|
|
156
|
+
to { transform: rotate(360deg); }
|
|
101
157
|
}
|
|
102
|
-
`;return
|
|
158
|
+
`;return L(_);})();
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
var
|
|
1
|
+
var x=Object.defineProperty;var S=(u,e,t)=>e in u?x(u,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):u[e]=t;var a=(u,e,t)=>S(u,typeof e!="symbol"?e+"":e,t);var c={API_URL:"https://api.pro6pp.nl/v2",LIMIT:20,DEBOUNCE_MS:150,MIN_DEBOUNCE_MS:50,MAX_RETRIES:0},f={DIGITS_1_3:/^[0-9]{1,3}$/},b={query:"",stage:null,cities:[],streets:[],suggestions:[],isValid:!1,isError:!1,isLoading:!1,hasMore:!1,selectedSuggestionIndex:-1},m=class{constructor(e){a(this,"country");a(this,"authKey");a(this,"apiUrl");a(this,"baseLimit");a(this,"currentLimit");a(this,"maxRetries");a(this,"fetcher");a(this,"onStateChange");a(this,"onSelect");a(this,"state");a(this,"abortController",null);a(this,"debouncedFetch");a(this,"isSelecting",!1);this.country=e.country,this.authKey=e.authKey,this.apiUrl=e.apiUrl||c.API_URL,this.baseLimit=e.limit||c.LIMIT,this.currentLimit=this.baseLimit;let t=e.maxRetries!==void 0?e.maxRetries:c.MAX_RETRIES;this.maxRetries=Math.max(0,Math.min(t,10)),this.fetcher=e.fetcher||((r,o)=>fetch(r,o)),this.onStateChange=e.onStateChange||(()=>{}),this.onSelect=e.onSelect||(()=>{}),this.state={...b};let i=e.debounceMs!==void 0?e.debounceMs:c.DEBOUNCE_MS,s=Math.max(i,c.MIN_DEBOUNCE_MS);this.debouncedFetch=this.debounce(r=>this.executeFetch(r),s)}handleInput(e){if(this.isSelecting){this.isSelecting=!1;return}this.currentLimit=this.baseLimit;let t=this.state.stage==="final"&&e!==this.state.query;this.updateState({query:e,isValid:!1,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 o=[...this.state.cities,...this.state.streets,...this.state.suggestions][this.state.selectedSuggestionIndex];o&&(this.selectItem(o),this.updateState({selectedSuggestionIndex:-1}));return}}let s=t.value;if(e.key===" "&&this.shouldAutoInsertComma(s)){e.preventDefault();let r=`${s.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 s=typeof e!="string"&&typeof e.value=="object"?e.value:void 0,r=!!s&&Object.keys(s).length>0;if(this.isSelecting=!0,this.state.stage==="final"||r){let n=t;if(s&&Object.keys(s).length>0){let{street:p,street_number:l,house_number:d,city:h}=s,g=l||d;p&&g&&h&&(n=`${p} ${g}, ${h}`)}this.finishSelection(n,s);return}let o=typeof e!="string"?e.subtitle:null;this.processSelection(i,o)}shouldAutoInsertComma(e){if(!e.includes(",")&&f.DIGITS_1_3.test(e.trim()))return!0;if(this.state.stage==="house_number"){let i=this.getCurrentFragment(e);return f.DIGITS_1_3.test(i)}return!1}finishSelection(e,t){this.updateState({query:e,suggestions:[],cities:[],streets:[],isValid:!0,stage:"final",hasMore:!1}),this.onSelect(t||e),setTimeout(()=>{this.isSelecting=!1},0)}processSelection(e,t){let{stage:i,query:s}=this.state,r=s;if(t&&(i==="city"||i==="street"||i==="mixed")){if(i==="city")r=`${t}, ${e}, `;else{let l=this.getQueryPrefix(s);r=l?`${l} ${e}, ${t}, `:`${e}, ${t}, `}this.updateQueryAndFetch(r);return}if(i==="direct"||i==="addition"){this.finishSelection(e);return}!s.includes(",")&&(i==="city"||i==="street"||i==="house_number_first")?r=`${e}, `:(r=this.replaceLastSegment(s,e),i!=="house_number"&&(r+=", ")),this.updateQueryAndFetch(r)}executeFetch(e,t=0){let i=(e||"").toString();if(!i.trim()){this.abortController?.abort(),this.resetState();return}t===0&&(this.updateState({isError:!1}),this.abortController&&this.abortController.abort(),this.abortController=new AbortController);let s=this.abortController?.signal,r=new URL(`${this.apiUrl}/infer/${this.country.toLowerCase()}`),o={authKey:this.authKey,query:i,limit:this.currentLimit.toString()};r.search=new URLSearchParams(o).toString(),this.fetcher(r.toString(),{signal:s}).then(n=>{if(!n.ok){if(t<this.maxRetries&&(n.status>=500||n.status===429))return this.retry(e,t,s);throw new Error("Network error")}return n.json()}).then(n=>{n&&this.mapResponseToState(n)}).catch(n=>{if(n.name!=="AbortError"){if(t<this.maxRetries)return this.retry(e,t,s);this.updateState({isError:!0,isLoading:!1})}})}retry(e,t,i){if(i?.aborted)return;let s=Math.pow(2,t)*200;setTimeout(()=>{i?.aborted||this.executeFetch(e,t+1)},s)}mapResponseToState(e){let t={stage:e.stage,isLoading:!1},i=!1,s=null,r=e.suggestions||[],o=[],n=new Set;for(let l of r){let d=`${l.label}|${l.subtitle||""}|${JSON.stringify(l.value||{})}`;n.has(d)||(n.add(d),o.push(l))}let p=o.length+(e.cities?.length||0)+(e.streets?.length||0);if(t.hasMore=p>=this.currentLimit,e.stage==="mixed"?(t.cities=e.cities||[],t.streets=e.streets||[],t.suggestions=[]):(t.suggestions=o,t.cities=[],t.streets=[],e.stage==="final"&&o.length===1&&(i=!0,s=o[0])),t.isValid=e.stage==="final",i&&s){t.query=s.label,t.suggestions=[],t.cities=[],t.streets=[],t.isValid=!0,t.hasMore=!1,this.updateState(t);let l=typeof s.value=="object"?s.value:s.label;this.onSelect(l)}else this.updateState(t)}updateQueryAndFetch(e){this.updateState({query:e,suggestions:[],cities:[],streets:[]}),this.updateState({isLoading:!0,isValid:!1,hasMore:!1}),this.debouncedFetch(e),setTimeout(()=>{this.isSelecting=!1},0)}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){return(e.split(",").slice(-1)[0]??"").trim()}resetState(){this.updateState({...b,query:this.state.query})}updateState(e){this.state={...this.state,...e},this.onStateChange(this.state)}debounce(e,t){let i,s=(...r)=>{i&&clearTimeout(i),i=setTimeout(()=>e.apply(this,r),t)};return s.cancel=()=>{i&&(clearTimeout(i),i=void 0)},s}};var y=`
|
|
2
2
|
.pro6pp-wrapper {
|
|
3
3
|
position: relative;
|
|
4
4
|
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
|
|
@@ -11,6 +11,7 @@ var b=Object.defineProperty;var x=(u,t,e)=>t in u?b(u,t,{enumerable:!0,configura
|
|
|
11
11
|
.pro6pp-input {
|
|
12
12
|
width: 100%;
|
|
13
13
|
padding: 10px 12px;
|
|
14
|
+
padding-right: 48px;
|
|
14
15
|
border: 1px solid #e0e0e0;
|
|
15
16
|
border-radius: 4px;
|
|
16
17
|
font-size: 16px;
|
|
@@ -22,6 +23,57 @@ var b=Object.defineProperty;var x=(u,t,e)=>t in u?b(u,t,{enumerable:!0,configura
|
|
|
22
23
|
border-color: #3b82f6;
|
|
23
24
|
box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
|
|
24
25
|
}
|
|
26
|
+
|
|
27
|
+
.pro6pp-input-addons {
|
|
28
|
+
position: absolute;
|
|
29
|
+
right: 6px;
|
|
30
|
+
top: 0;
|
|
31
|
+
bottom: 0;
|
|
32
|
+
display: flex;
|
|
33
|
+
align-items: center;
|
|
34
|
+
gap: 2px;
|
|
35
|
+
pointer-events: none;
|
|
36
|
+
}
|
|
37
|
+
.pro6pp-input-addons > * {
|
|
38
|
+
pointer-events: auto;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
.pro6pp-clear-button {
|
|
42
|
+
background: none;
|
|
43
|
+
border: none;
|
|
44
|
+
width: 28px;
|
|
45
|
+
height: 28px;
|
|
46
|
+
cursor: pointer;
|
|
47
|
+
color: #a3a3a3;
|
|
48
|
+
display: flex;
|
|
49
|
+
align-items: center;
|
|
50
|
+
justify-content: center;
|
|
51
|
+
border-radius: 50%;
|
|
52
|
+
transition: color 0.2s, background-color 0.2s, transform 0.1s;
|
|
53
|
+
}
|
|
54
|
+
.pro6pp-clear-button:hover {
|
|
55
|
+
color: #1f2937;
|
|
56
|
+
background-color: #f3f4f6;
|
|
57
|
+
}
|
|
58
|
+
.pro6pp-clear-button:active {
|
|
59
|
+
transform: scale(0.92);
|
|
60
|
+
}
|
|
61
|
+
.pro6pp-clear-button svg {
|
|
62
|
+
width: 18px;
|
|
63
|
+
height: 18px;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
.pro6pp-loader {
|
|
67
|
+
width: 18px;
|
|
68
|
+
height: 18px;
|
|
69
|
+
margin: 0 4px;
|
|
70
|
+
border: 2px solid #e0e0e0;
|
|
71
|
+
border-top-color: #6b7280;
|
|
72
|
+
border-radius: 50%;
|
|
73
|
+
animation: pro6pp-spin 0.6s linear infinite;
|
|
74
|
+
flex-shrink: 0;
|
|
75
|
+
}
|
|
76
|
+
|
|
25
77
|
.pro6pp-dropdown {
|
|
26
78
|
position: absolute;
|
|
27
79
|
top: 100%;
|
|
@@ -32,30 +84,32 @@ var b=Object.defineProperty;var x=(u,t,e)=>t in u?b(u,t,{enumerable:!0,configura
|
|
|
32
84
|
background: white;
|
|
33
85
|
border: 1px solid #e0e0e0;
|
|
34
86
|
border-radius: 4px;
|
|
35
|
-
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.
|
|
87
|
+
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
|
|
36
88
|
max-height: 300px;
|
|
37
89
|
overflow-y: auto;
|
|
90
|
+
display: flex;
|
|
91
|
+
flex-direction: column;
|
|
92
|
+
}
|
|
93
|
+
.pro6pp-list {
|
|
38
94
|
list-style: none !important;
|
|
39
95
|
padding: 0 !important;
|
|
40
96
|
margin: 0 !important;
|
|
41
|
-
|
|
97
|
+
flex-grow: 1;
|
|
42
98
|
}
|
|
43
99
|
.pro6pp-item {
|
|
44
|
-
padding: 12px
|
|
100
|
+
padding: 12px 16px;
|
|
45
101
|
cursor: pointer;
|
|
46
102
|
display: flex;
|
|
47
103
|
flex-direction: row;
|
|
48
104
|
align-items: center;
|
|
49
|
-
color: #
|
|
105
|
+
color: #111827;
|
|
50
106
|
font-size: 14px;
|
|
51
|
-
line-height: 1;
|
|
107
|
+
line-height: 1.2;
|
|
52
108
|
white-space: nowrap;
|
|
53
109
|
overflow: hidden;
|
|
54
|
-
border-radius: 0 !important;
|
|
55
|
-
margin: 0 !important;
|
|
56
110
|
}
|
|
57
111
|
.pro6pp-item:hover, .pro6pp-item--active {
|
|
58
|
-
background-color: #
|
|
112
|
+
background-color: #f9fafb;
|
|
59
113
|
}
|
|
60
114
|
.pro6pp-item__label {
|
|
61
115
|
font-weight: 500;
|
|
@@ -63,7 +117,7 @@ var b=Object.defineProperty;var x=(u,t,e)=>t in u?b(u,t,{enumerable:!0,configura
|
|
|
63
117
|
}
|
|
64
118
|
.pro6pp-item__subtitle {
|
|
65
119
|
font-size: 14px;
|
|
66
|
-
color: #
|
|
120
|
+
color: #6b7280;
|
|
67
121
|
overflow: hidden;
|
|
68
122
|
text-overflow: ellipsis;
|
|
69
123
|
flex-shrink: 1;
|
|
@@ -72,31 +126,33 @@ var b=Object.defineProperty;var x=(u,t,e)=>t in u?b(u,t,{enumerable:!0,configura
|
|
|
72
126
|
margin-left: auto;
|
|
73
127
|
display: flex;
|
|
74
128
|
align-items: center;
|
|
75
|
-
color: #
|
|
129
|
+
color: #9ca3af;
|
|
76
130
|
padding-left: 8px;
|
|
77
131
|
}
|
|
78
132
|
.pro6pp-no-results {
|
|
79
|
-
padding:
|
|
80
|
-
color: #
|
|
133
|
+
padding: 16px;
|
|
134
|
+
color: #6b7280;
|
|
81
135
|
font-size: 14px;
|
|
82
136
|
text-align: center;
|
|
83
|
-
user-select: none;
|
|
84
|
-
pointer-events: none;
|
|
85
137
|
}
|
|
86
|
-
.pro6pp-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
138
|
+
.pro6pp-load-more {
|
|
139
|
+
width: 100%;
|
|
140
|
+
padding: 10px;
|
|
141
|
+
background: #f9fafb;
|
|
142
|
+
border: none;
|
|
143
|
+
border-top: 1px solid #e0e0e0;
|
|
144
|
+
color: #3b82f6;
|
|
145
|
+
font-size: 13px;
|
|
146
|
+
font-weight: 600;
|
|
147
|
+
cursor: pointer;
|
|
148
|
+
transition: background-color 0.2s;
|
|
149
|
+
flex-shrink: 0;
|
|
150
|
+
}
|
|
151
|
+
.pro6pp-load-more:hover {
|
|
152
|
+
background-color: #f3f4f6;
|
|
98
153
|
}
|
|
154
|
+
|
|
99
155
|
@keyframes pro6pp-spin {
|
|
100
|
-
to { transform:
|
|
156
|
+
to { transform: rotate(360deg); }
|
|
101
157
|
}
|
|
102
|
-
`;export{y as DEFAULT_STYLES,
|
|
158
|
+
`;export{y as DEFAULT_STYLES,b as INITIAL_STATE,m as InferCore};
|
package/package.json
CHANGED