@swan-io/shared-business 8.16.4 → 8.17.1
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/package.json
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { PKOptions } from "@placekit/client-js";
|
|
1
2
|
import { CountryCCA3 } from "@swan-io/shared-business/src/constants/countries";
|
|
2
3
|
import { MutableRefObject } from "react";
|
|
3
4
|
import { StyleProp, ViewStyle } from "react-native";
|
|
@@ -5,11 +6,14 @@ export type AddressDetail = {
|
|
|
5
6
|
completeAddress: string;
|
|
6
7
|
city: string;
|
|
7
8
|
postalCode?: string;
|
|
9
|
+
country: string;
|
|
10
|
+
countryCode: string;
|
|
8
11
|
};
|
|
9
12
|
type Props = {
|
|
10
13
|
inputRef?: MutableRefObject<unknown>;
|
|
11
14
|
id?: string;
|
|
12
|
-
|
|
15
|
+
types?: PKOptions["types"];
|
|
16
|
+
country?: CountryCCA3;
|
|
13
17
|
disabled?: boolean;
|
|
14
18
|
error?: string;
|
|
15
19
|
onSuggestion?: (suggestion: AddressDetail) => void;
|
|
@@ -22,5 +26,5 @@ type Props = {
|
|
|
22
26
|
emptyResultText: string;
|
|
23
27
|
apiKey?: string;
|
|
24
28
|
};
|
|
25
|
-
export declare const PlacekitAddressSearchInput: ({ inputRef, id, country, disabled, error, value, onValueChange, onSuggestion, language, placeholder, shouldDisplaySuggestions, emptyResultText, apiKey, }: Props) => import("react/jsx-runtime").JSX.Element;
|
|
29
|
+
export declare const PlacekitAddressSearchInput: ({ inputRef, id, country, disabled, error, value, types, onValueChange, onSuggestion, language, placeholder, shouldDisplaySuggestions, emptyResultText, apiKey, }: Props) => import("react/jsx-runtime").JSX.Element;
|
|
26
30
|
export {};
|
|
@@ -4,30 +4,36 @@ import { AutocompleteSearchInput } from "@swan-io/lake/src/components/Autocomple
|
|
|
4
4
|
import { countriesWithMultipleCCA3, getCCA2forCCA3, } from "@swan-io/shared-business/src/constants/countries";
|
|
5
5
|
import { useCallback } from "react";
|
|
6
6
|
import { usePlacekit } from "../hooks/usePlacekit";
|
|
7
|
-
export const PlacekitAddressSearchInput = ({ inputRef, id, country, disabled, error, value, onValueChange, onSuggestion, language, placeholder, shouldDisplaySuggestions = true, emptyResultText, apiKey, }) => {
|
|
7
|
+
export const PlacekitAddressSearchInput = ({ inputRef, id, country, disabled, error, value, types = ["street"], onValueChange, onSuggestion, language, placeholder, shouldDisplaySuggestions = true, emptyResultText, apiKey, }) => {
|
|
8
8
|
const placekit = usePlacekit({ apiKey });
|
|
9
9
|
const loadSuggestions = useCallback((value) => {
|
|
10
|
-
var _a;
|
|
11
10
|
if (placekit != null) {
|
|
12
|
-
const countries =
|
|
11
|
+
const countries = Option.fromNullable(country)
|
|
12
|
+
.map(country => {
|
|
13
|
+
var _a;
|
|
14
|
+
return Array.filterMap((_a = countriesWithMultipleCCA3[country]) !== null && _a !== void 0 ? _a : [country], cca3 => Option.fromNullable(getCCA2forCCA3(cca3)));
|
|
15
|
+
})
|
|
16
|
+
.getOr([]);
|
|
13
17
|
const cca2Countries = countries.length === 0 ? Option.None() : Option.Some(countries);
|
|
14
18
|
return Future.fromPromise(placekit.search(value, {
|
|
15
|
-
types
|
|
19
|
+
types,
|
|
16
20
|
language,
|
|
17
21
|
countries: cca2Countries.toUndefined(),
|
|
18
22
|
})).mapOk(({ results }) => results.map(result => ({
|
|
19
23
|
title: result.name,
|
|
20
|
-
subtitle: result.city
|
|
24
|
+
subtitle: `${result.city} (${result.country})`,
|
|
21
25
|
value: result,
|
|
22
26
|
})), true);
|
|
23
27
|
}
|
|
24
28
|
return Future.value(Result.Ok([]));
|
|
25
|
-
}, [placekit, country, language]);
|
|
29
|
+
}, [placekit, country, language, types]);
|
|
26
30
|
const onSuggestionSelected = useCallback((suggestion) => {
|
|
27
31
|
onSuggestion === null || onSuggestion === void 0 ? void 0 : onSuggestion({
|
|
28
32
|
completeAddress: suggestion.value.name,
|
|
29
33
|
postalCode: suggestion.value.zipcode[0],
|
|
30
34
|
city: suggestion.value.city,
|
|
35
|
+
country: suggestion.value.country,
|
|
36
|
+
countryCode: suggestion.value.countrycode,
|
|
31
37
|
});
|
|
32
38
|
}, [onSuggestion]);
|
|
33
39
|
return (_jsx(AutocompleteSearchInput, { inputRef: inputRef, value: value, onValueChange: onValueChange, disabled: disabled, id: id, placeholder: placeholder, error: error, emptyResultText: emptyResultText, shouldDisplaySuggestions: shouldDisplaySuggestions, loadSuggestions: placekit != null ? loadSuggestions : undefined, onSuggestion: onSuggestionSelected }));
|