@arkyn/components 1.3.97 → 1.3.98

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.
@@ -1,14 +1,49 @@
1
1
  import { GoogleSearchPlacesProps } from "@arkyn/types";
2
- import { Input } from "../Input";
3
2
  import { LoadScript, StandaloneSearchBox } from "@react-google-maps/api";
3
+ import { useState } from "react";
4
+
5
+ import { Input } from "../Input";
6
+
7
+ type AddressComponentsType = {
8
+ long_name: string;
9
+ short_name: string;
10
+ types: string[];
11
+ }[];
4
12
 
5
13
  function GoogleSearchPlaces({
6
14
  googleMapsApiKey,
7
- onLoad,
8
- onPlacesChanged,
15
+ onChange,
9
16
  options,
10
17
  ...rest
11
18
  }: GoogleSearchPlacesProps) {
19
+ const [searchBox, setSearchBox] = useState<any>(null);
20
+ const handleLoad = (ref: any) => setSearchBox(ref);
21
+
22
+ const handlePlacesChanged = () => {
23
+ const places = searchBox.getPlaces();
24
+ const place = places[0];
25
+
26
+ const address_components =
27
+ place?.address_components as AddressComponentsType;
28
+
29
+ function findData(key: string) {
30
+ const data = address_components.find((item) => item.types[0] === key);
31
+ if (data) return data.long_name;
32
+ return "";
33
+ }
34
+
35
+ if (place) {
36
+ const street = findData("route");
37
+ const district = findData("sublocality_level_1");
38
+ const city = findData("administrative_area_level_2");
39
+ const state = findData("administrative_area_level_1");
40
+ const cep = findData("postal_code");
41
+
42
+ const sendPlace = { street, city, state, district, cep };
43
+ onChange && onChange(sendPlace);
44
+ }
45
+ };
46
+
12
47
  return (
13
48
  <LoadScript
14
49
  libraries={["places"]}
@@ -16,8 +51,8 @@ function GoogleSearchPlaces({
16
51
  loadingElement={<Input type="text" {...rest} />}
17
52
  >
18
53
  <StandaloneSearchBox
19
- onLoad={onLoad}
20
- onPlacesChanged={onPlacesChanged}
54
+ onLoad={handleLoad}
55
+ onPlacesChanged={handlePlacesChanged}
21
56
  options={options}
22
57
  >
23
58
  <Input type="text" {...rest} />