@pro6pp/infer-react 0.0.2-beta.13 → 0.0.2-beta.14
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 +47 -9
- package/dist/index.d.cts +18 -7
- package/dist/index.d.ts +18 -7
- package/dist/index.js +47 -9
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -56,6 +56,7 @@ var INITIAL_STATE = {
|
|
|
56
56
|
streets: [],
|
|
57
57
|
suggestions: [],
|
|
58
58
|
isValid: false,
|
|
59
|
+
value: null,
|
|
59
60
|
isError: false,
|
|
60
61
|
isLoading: false,
|
|
61
62
|
hasMore: false,
|
|
@@ -111,6 +112,7 @@ var InferCore = class {
|
|
|
111
112
|
this.updateState({
|
|
112
113
|
query: value,
|
|
113
114
|
isValid: false,
|
|
115
|
+
value: null,
|
|
114
116
|
isLoading: !!value.trim(),
|
|
115
117
|
selectedSuggestionIndex: -1,
|
|
116
118
|
hasMore: false
|
|
@@ -228,6 +230,7 @@ var InferCore = class {
|
|
|
228
230
|
cities: [],
|
|
229
231
|
streets: [],
|
|
230
232
|
isValid: true,
|
|
233
|
+
value: value || null,
|
|
231
234
|
stage: "final",
|
|
232
235
|
hasMore: false
|
|
233
236
|
});
|
|
@@ -283,10 +286,12 @@ var InferCore = class {
|
|
|
283
286
|
const currentSignal = this.abortController?.signal;
|
|
284
287
|
const baseUrl = this.explicitApiUrl ? this.explicitApiUrl : `${DEFAULTS.API_URL}/infer/${this.country.toLowerCase()}`;
|
|
285
288
|
const params = new URLSearchParams({
|
|
286
|
-
country: this.country.toLowerCase(),
|
|
287
289
|
query: text,
|
|
288
290
|
limit: this.currentLimit.toString()
|
|
289
291
|
});
|
|
292
|
+
if (this.explicitApiUrl) {
|
|
293
|
+
params.append("country", this.country.toLowerCase());
|
|
294
|
+
}
|
|
290
295
|
if (this.authKey) {
|
|
291
296
|
params.set("authKey", this.authKey);
|
|
292
297
|
}
|
|
@@ -352,8 +357,16 @@ var InferCore = class {
|
|
|
352
357
|
}
|
|
353
358
|
}
|
|
354
359
|
updateQueryAndFetch(nextQuery) {
|
|
355
|
-
this.updateState({
|
|
356
|
-
|
|
360
|
+
this.updateState({
|
|
361
|
+
query: nextQuery,
|
|
362
|
+
suggestions: [],
|
|
363
|
+
cities: [],
|
|
364
|
+
streets: [],
|
|
365
|
+
isValid: false,
|
|
366
|
+
value: null,
|
|
367
|
+
isLoading: true,
|
|
368
|
+
hasMore: false
|
|
369
|
+
});
|
|
357
370
|
this.debouncedFetch(nextQuery);
|
|
358
371
|
}
|
|
359
372
|
replaceLastSegment(fullText, newSegment) {
|
|
@@ -642,7 +655,18 @@ var HighlightedText = ({ text, query }) => {
|
|
|
642
655
|
));
|
|
643
656
|
};
|
|
644
657
|
function useInfer(config) {
|
|
645
|
-
const [state, setState] = (0, import_react.useState)(
|
|
658
|
+
const [state, setState] = (0, import_react.useState)(() => {
|
|
659
|
+
if (config.initialValue) {
|
|
660
|
+
return {
|
|
661
|
+
...INITIAL_STATE,
|
|
662
|
+
value: config.initialValue,
|
|
663
|
+
query: `${config.initialValue.street} ${config.initialValue.street_number}, ${config.initialValue.city}`,
|
|
664
|
+
isValid: true,
|
|
665
|
+
stage: "final"
|
|
666
|
+
};
|
|
667
|
+
}
|
|
668
|
+
return INITIAL_STATE;
|
|
669
|
+
});
|
|
646
670
|
const callbacksRef = (0, import_react.useRef)({
|
|
647
671
|
onStateChange: config.onStateChange,
|
|
648
672
|
onSelect: config.onSelect
|
|
@@ -654,7 +678,7 @@ function useInfer(config) {
|
|
|
654
678
|
};
|
|
655
679
|
}, [config.onStateChange, config.onSelect]);
|
|
656
680
|
const core = (0, import_react.useMemo)(() => {
|
|
657
|
-
|
|
681
|
+
const instance = new InferCore({
|
|
658
682
|
...config,
|
|
659
683
|
onStateChange: (newState) => {
|
|
660
684
|
setState({ ...newState });
|
|
@@ -664,6 +688,12 @@ function useInfer(config) {
|
|
|
664
688
|
callbacksRef.current.onSelect?.(selection);
|
|
665
689
|
}
|
|
666
690
|
});
|
|
691
|
+
if (config.initialValue) {
|
|
692
|
+
const address = config.initialValue;
|
|
693
|
+
const label = `${address.street} ${address.street_number}, ${address.city}`;
|
|
694
|
+
instance.selectItem({ label, value: address });
|
|
695
|
+
}
|
|
696
|
+
return instance;
|
|
667
697
|
}, [
|
|
668
698
|
config.country,
|
|
669
699
|
config.authKey,
|
|
@@ -671,10 +701,16 @@ function useInfer(config) {
|
|
|
671
701
|
config.fetcher,
|
|
672
702
|
config.limit,
|
|
673
703
|
config.debounceMs,
|
|
674
|
-
config.maxRetries
|
|
704
|
+
config.maxRetries,
|
|
705
|
+
config.initialValue
|
|
675
706
|
]);
|
|
707
|
+
const setValue = (address) => {
|
|
708
|
+
if (!address) return;
|
|
709
|
+
const label = `${address.street} ${address.street_number}, ${address.city}`;
|
|
710
|
+
core.selectItem({ label, value: address });
|
|
711
|
+
};
|
|
676
712
|
return {
|
|
677
|
-
/** The current UI state (suggestions, loading status, query, etc.). */
|
|
713
|
+
/** The current UI state (suggestions, loading status, query, value, etc.). */
|
|
678
714
|
state,
|
|
679
715
|
/** The raw InferCore instance for manual control. */
|
|
680
716
|
core,
|
|
@@ -684,9 +720,11 @@ function useInfer(config) {
|
|
|
684
720
|
onChange: (e) => core.handleInput(e.target.value),
|
|
685
721
|
onKeyDown: (e) => core.handleKeyDown(e)
|
|
686
722
|
},
|
|
687
|
-
/**
|
|
723
|
+
/** Manually select a specific suggestion. */
|
|
688
724
|
selectItem: (item) => core.selectItem(item),
|
|
689
|
-
/**
|
|
725
|
+
/** Programmatically set the address value. */
|
|
726
|
+
setValue,
|
|
727
|
+
/** Load more results. */
|
|
690
728
|
loadMore: () => core.loadMore()
|
|
691
729
|
};
|
|
692
730
|
}
|
package/dist/index.d.cts
CHANGED
|
@@ -1,14 +1,23 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
|
-
import { InferConfig, InferState, InferCore, InferResult } from '@pro6pp/infer-core';
|
|
2
|
+
import { InferConfig, AddressValue, InferState, InferCore, InferResult } from '@pro6pp/infer-core';
|
|
3
3
|
export { AddressValue, CountryCode, Fetcher, InferConfig, InferResult, InferState, Stage } from '@pro6pp/infer-core';
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* Extended configuration for the React hook.
|
|
7
|
+
*/
|
|
8
|
+
interface UseInferConfig extends InferConfig {
|
|
9
|
+
/**
|
|
10
|
+
* Initial address value to pre-fill the state with.
|
|
11
|
+
*/
|
|
12
|
+
initialValue?: AddressValue;
|
|
13
|
+
}
|
|
5
14
|
/**
|
|
6
15
|
* A headless React hook that provides the logic for address search using the Infer API.
|
|
7
16
|
* @param config The engine configuration (authKey, country, etc.).
|
|
8
17
|
* @returns An object containing the current state, the core instance, and pre-bound input props.
|
|
9
18
|
*/
|
|
10
|
-
declare function useInfer(config:
|
|
11
|
-
/** The current UI state (suggestions, loading status, query, etc.). */
|
|
19
|
+
declare function useInfer(config: UseInferConfig): {
|
|
20
|
+
/** The current UI state (suggestions, loading status, query, value, etc.). */
|
|
12
21
|
state: InferState;
|
|
13
22
|
/** The raw InferCore instance for manual control. */
|
|
14
23
|
core: InferCore;
|
|
@@ -18,15 +27,17 @@ declare function useInfer(config: InferConfig): {
|
|
|
18
27
|
onChange: (e: React.ChangeEvent<HTMLInputElement>) => void;
|
|
19
28
|
onKeyDown: (e: React.KeyboardEvent<HTMLInputElement>) => void;
|
|
20
29
|
};
|
|
21
|
-
/**
|
|
30
|
+
/** Manually select a specific suggestion. */
|
|
22
31
|
selectItem: (item: InferResult | string) => boolean;
|
|
23
|
-
/**
|
|
32
|
+
/** Programmatically set the address value. */
|
|
33
|
+
setValue: (address: AddressValue) => void;
|
|
34
|
+
/** Load more results. */
|
|
24
35
|
loadMore: () => void;
|
|
25
36
|
};
|
|
26
37
|
/**
|
|
27
38
|
* Props for the Pro6PPInfer component.
|
|
28
39
|
*/
|
|
29
|
-
interface Pro6PPInferProps extends
|
|
40
|
+
interface Pro6PPInferProps extends UseInferConfig {
|
|
30
41
|
/** Optional CSS class for the wrapper div. */
|
|
31
42
|
className?: string;
|
|
32
43
|
/** Optional inline styles for the wrapper div. */
|
|
@@ -65,4 +76,4 @@ interface Pro6PPInferProps extends InferConfig {
|
|
|
65
76
|
*/
|
|
66
77
|
declare const Pro6PPInfer: React.ForwardRefExoticComponent<Pro6PPInferProps & React.RefAttributes<HTMLInputElement>>;
|
|
67
78
|
|
|
68
|
-
export { Pro6PPInfer, type Pro6PPInferProps, useInfer };
|
|
79
|
+
export { Pro6PPInfer, type Pro6PPInferProps, type UseInferConfig, useInfer };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,14 +1,23 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
|
-
import { InferConfig, InferState, InferCore, InferResult } from '@pro6pp/infer-core';
|
|
2
|
+
import { InferConfig, AddressValue, InferState, InferCore, InferResult } from '@pro6pp/infer-core';
|
|
3
3
|
export { AddressValue, CountryCode, Fetcher, InferConfig, InferResult, InferState, Stage } from '@pro6pp/infer-core';
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* Extended configuration for the React hook.
|
|
7
|
+
*/
|
|
8
|
+
interface UseInferConfig extends InferConfig {
|
|
9
|
+
/**
|
|
10
|
+
* Initial address value to pre-fill the state with.
|
|
11
|
+
*/
|
|
12
|
+
initialValue?: AddressValue;
|
|
13
|
+
}
|
|
5
14
|
/**
|
|
6
15
|
* A headless React hook that provides the logic for address search using the Infer API.
|
|
7
16
|
* @param config The engine configuration (authKey, country, etc.).
|
|
8
17
|
* @returns An object containing the current state, the core instance, and pre-bound input props.
|
|
9
18
|
*/
|
|
10
|
-
declare function useInfer(config:
|
|
11
|
-
/** The current UI state (suggestions, loading status, query, etc.). */
|
|
19
|
+
declare function useInfer(config: UseInferConfig): {
|
|
20
|
+
/** The current UI state (suggestions, loading status, query, value, etc.). */
|
|
12
21
|
state: InferState;
|
|
13
22
|
/** The raw InferCore instance for manual control. */
|
|
14
23
|
core: InferCore;
|
|
@@ -18,15 +27,17 @@ declare function useInfer(config: InferConfig): {
|
|
|
18
27
|
onChange: (e: React.ChangeEvent<HTMLInputElement>) => void;
|
|
19
28
|
onKeyDown: (e: React.KeyboardEvent<HTMLInputElement>) => void;
|
|
20
29
|
};
|
|
21
|
-
/**
|
|
30
|
+
/** Manually select a specific suggestion. */
|
|
22
31
|
selectItem: (item: InferResult | string) => boolean;
|
|
23
|
-
/**
|
|
32
|
+
/** Programmatically set the address value. */
|
|
33
|
+
setValue: (address: AddressValue) => void;
|
|
34
|
+
/** Load more results. */
|
|
24
35
|
loadMore: () => void;
|
|
25
36
|
};
|
|
26
37
|
/**
|
|
27
38
|
* Props for the Pro6PPInfer component.
|
|
28
39
|
*/
|
|
29
|
-
interface Pro6PPInferProps extends
|
|
40
|
+
interface Pro6PPInferProps extends UseInferConfig {
|
|
30
41
|
/** Optional CSS class for the wrapper div. */
|
|
31
42
|
className?: string;
|
|
32
43
|
/** Optional inline styles for the wrapper div. */
|
|
@@ -65,4 +76,4 @@ interface Pro6PPInferProps extends InferConfig {
|
|
|
65
76
|
*/
|
|
66
77
|
declare const Pro6PPInfer: React.ForwardRefExoticComponent<Pro6PPInferProps & React.RefAttributes<HTMLInputElement>>;
|
|
67
78
|
|
|
68
|
-
export { Pro6PPInfer, type Pro6PPInferProps, useInfer };
|
|
79
|
+
export { Pro6PPInfer, type Pro6PPInferProps, type UseInferConfig, useInfer };
|
package/dist/index.js
CHANGED
|
@@ -30,6 +30,7 @@ var INITIAL_STATE = {
|
|
|
30
30
|
streets: [],
|
|
31
31
|
suggestions: [],
|
|
32
32
|
isValid: false,
|
|
33
|
+
value: null,
|
|
33
34
|
isError: false,
|
|
34
35
|
isLoading: false,
|
|
35
36
|
hasMore: false,
|
|
@@ -85,6 +86,7 @@ var InferCore = class {
|
|
|
85
86
|
this.updateState({
|
|
86
87
|
query: value,
|
|
87
88
|
isValid: false,
|
|
89
|
+
value: null,
|
|
88
90
|
isLoading: !!value.trim(),
|
|
89
91
|
selectedSuggestionIndex: -1,
|
|
90
92
|
hasMore: false
|
|
@@ -202,6 +204,7 @@ var InferCore = class {
|
|
|
202
204
|
cities: [],
|
|
203
205
|
streets: [],
|
|
204
206
|
isValid: true,
|
|
207
|
+
value: value || null,
|
|
205
208
|
stage: "final",
|
|
206
209
|
hasMore: false
|
|
207
210
|
});
|
|
@@ -257,10 +260,12 @@ var InferCore = class {
|
|
|
257
260
|
const currentSignal = this.abortController?.signal;
|
|
258
261
|
const baseUrl = this.explicitApiUrl ? this.explicitApiUrl : `${DEFAULTS.API_URL}/infer/${this.country.toLowerCase()}`;
|
|
259
262
|
const params = new URLSearchParams({
|
|
260
|
-
country: this.country.toLowerCase(),
|
|
261
263
|
query: text,
|
|
262
264
|
limit: this.currentLimit.toString()
|
|
263
265
|
});
|
|
266
|
+
if (this.explicitApiUrl) {
|
|
267
|
+
params.append("country", this.country.toLowerCase());
|
|
268
|
+
}
|
|
264
269
|
if (this.authKey) {
|
|
265
270
|
params.set("authKey", this.authKey);
|
|
266
271
|
}
|
|
@@ -326,8 +331,16 @@ var InferCore = class {
|
|
|
326
331
|
}
|
|
327
332
|
}
|
|
328
333
|
updateQueryAndFetch(nextQuery) {
|
|
329
|
-
this.updateState({
|
|
330
|
-
|
|
334
|
+
this.updateState({
|
|
335
|
+
query: nextQuery,
|
|
336
|
+
suggestions: [],
|
|
337
|
+
cities: [],
|
|
338
|
+
streets: [],
|
|
339
|
+
isValid: false,
|
|
340
|
+
value: null,
|
|
341
|
+
isLoading: true,
|
|
342
|
+
hasMore: false
|
|
343
|
+
});
|
|
331
344
|
this.debouncedFetch(nextQuery);
|
|
332
345
|
}
|
|
333
346
|
replaceLastSegment(fullText, newSegment) {
|
|
@@ -616,7 +629,18 @@ var HighlightedText = ({ text, query }) => {
|
|
|
616
629
|
));
|
|
617
630
|
};
|
|
618
631
|
function useInfer(config) {
|
|
619
|
-
const [state, setState] = useState(
|
|
632
|
+
const [state, setState] = useState(() => {
|
|
633
|
+
if (config.initialValue) {
|
|
634
|
+
return {
|
|
635
|
+
...INITIAL_STATE,
|
|
636
|
+
value: config.initialValue,
|
|
637
|
+
query: `${config.initialValue.street} ${config.initialValue.street_number}, ${config.initialValue.city}`,
|
|
638
|
+
isValid: true,
|
|
639
|
+
stage: "final"
|
|
640
|
+
};
|
|
641
|
+
}
|
|
642
|
+
return INITIAL_STATE;
|
|
643
|
+
});
|
|
620
644
|
const callbacksRef = useRef({
|
|
621
645
|
onStateChange: config.onStateChange,
|
|
622
646
|
onSelect: config.onSelect
|
|
@@ -628,7 +652,7 @@ function useInfer(config) {
|
|
|
628
652
|
};
|
|
629
653
|
}, [config.onStateChange, config.onSelect]);
|
|
630
654
|
const core = useMemo(() => {
|
|
631
|
-
|
|
655
|
+
const instance = new InferCore({
|
|
632
656
|
...config,
|
|
633
657
|
onStateChange: (newState) => {
|
|
634
658
|
setState({ ...newState });
|
|
@@ -638,6 +662,12 @@ function useInfer(config) {
|
|
|
638
662
|
callbacksRef.current.onSelect?.(selection);
|
|
639
663
|
}
|
|
640
664
|
});
|
|
665
|
+
if (config.initialValue) {
|
|
666
|
+
const address = config.initialValue;
|
|
667
|
+
const label = `${address.street} ${address.street_number}, ${address.city}`;
|
|
668
|
+
instance.selectItem({ label, value: address });
|
|
669
|
+
}
|
|
670
|
+
return instance;
|
|
641
671
|
}, [
|
|
642
672
|
config.country,
|
|
643
673
|
config.authKey,
|
|
@@ -645,10 +675,16 @@ function useInfer(config) {
|
|
|
645
675
|
config.fetcher,
|
|
646
676
|
config.limit,
|
|
647
677
|
config.debounceMs,
|
|
648
|
-
config.maxRetries
|
|
678
|
+
config.maxRetries,
|
|
679
|
+
config.initialValue
|
|
649
680
|
]);
|
|
681
|
+
const setValue = (address) => {
|
|
682
|
+
if (!address) return;
|
|
683
|
+
const label = `${address.street} ${address.street_number}, ${address.city}`;
|
|
684
|
+
core.selectItem({ label, value: address });
|
|
685
|
+
};
|
|
650
686
|
return {
|
|
651
|
-
/** The current UI state (suggestions, loading status, query, etc.). */
|
|
687
|
+
/** The current UI state (suggestions, loading status, query, value, etc.). */
|
|
652
688
|
state,
|
|
653
689
|
/** The raw InferCore instance for manual control. */
|
|
654
690
|
core,
|
|
@@ -658,9 +694,11 @@ function useInfer(config) {
|
|
|
658
694
|
onChange: (e) => core.handleInput(e.target.value),
|
|
659
695
|
onKeyDown: (e) => core.handleKeyDown(e)
|
|
660
696
|
},
|
|
661
|
-
/**
|
|
697
|
+
/** Manually select a specific suggestion. */
|
|
662
698
|
selectItem: (item) => core.selectItem(item),
|
|
663
|
-
/**
|
|
699
|
+
/** Programmatically set the address value. */
|
|
700
|
+
setValue,
|
|
701
|
+
/** Load more results. */
|
|
664
702
|
loadMore: () => core.loadMore()
|
|
665
703
|
};
|
|
666
704
|
}
|
package/package.json
CHANGED
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
"url": "https://github.com/pro6pp/infer-sdk/issues"
|
|
21
21
|
},
|
|
22
22
|
"sideEffects": false,
|
|
23
|
-
"version": "0.0.2-beta.
|
|
23
|
+
"version": "0.0.2-beta.14",
|
|
24
24
|
"main": "./dist/index.cjs",
|
|
25
25
|
"module": "./dist/index.js",
|
|
26
26
|
"types": "./dist/index.d.ts",
|
|
@@ -46,7 +46,7 @@
|
|
|
46
46
|
"react": ">=16"
|
|
47
47
|
},
|
|
48
48
|
"dependencies": {
|
|
49
|
-
"@pro6pp/infer-core": "0.0.2-beta.
|
|
49
|
+
"@pro6pp/infer-core": "0.0.2-beta.12"
|
|
50
50
|
},
|
|
51
51
|
"devDependencies": {
|
|
52
52
|
"@testing-library/dom": "^10.4.1",
|