You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
## The package provides 3 tools for working with google places services:
9
9
10
-
This is a simple react component for working with google [autocomplete](https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete)
10
+
1.[ReactGoogleAutocomplete](#reactgoogleautocomplete) is a simple html input component that provides functionality of the [google places widgets](https://developers.google.com/maps/documentation/javascript/reference/places-widget#AutocompleteOptions).
11
+
2.[usePlacesWidget](#useplaceswidget) is a react hook that provides the same functionality as `ReactGoogleAutocomplete` does but it does not create any dom elements. Instead, it gives you back a react ref which you can set to any input you want.
12
+
3.[usePlacesAutocompleteService](#useplacesautocompleteservice) is a more complex tool. It uses [google places autocomplete service](https://developers.google.com/maps/documentation/javascript/reference/places-autocomplete-service) and it provides all the functionality to you as the return value. In addition to that, you can set a `debounce` prop which will prevent users to send many requests to Google.
11
13
12
14
## Install
13
15
@@ -26,6 +28,13 @@ As of version 1.2.4, you can now pass an `apiKey` prop to automatically load the
26
28
apiKey={YOUR_GOOGLE_MAPS_API_KEY}
27
29
onPlaceSelected={(place) =>console.log(place)}
28
30
/>
31
+
or
32
+
const { ref } =usePlacesWidget({
33
+
apiKey:YOUR_GOOGLE_MAPS_API_KEY,
34
+
onPlaceSelected: (place) =>console.log(place)
35
+
})
36
+
37
+
<AnyInput ref={ref} />
29
38
```
30
39
31
40
Alternatively if not passing the `apiKey` prop, you can include google autocomplete link api in your app. Somewhere in index.html or somewhere else. More info [here](https://developers.google.com/maps/documentation/places/web-service/autocomplete)
@@ -37,15 +46,28 @@ Alternatively if not passing the `apiKey` prop, you can include google autocompl
37
46
></script>
38
47
```
39
48
40
-
## Props
49
+
## ReactGoogleAutocomplete
50
+
51
+
This is a simple react component for working with google [autocomplete](https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete)
-`apiKey`: pass to automatically load the Google maps scripts. The api key can be found in your [google cloud console.](https://developers.google.com/maps/documentation/javascript/get-api-key)
43
67
44
68
-`ref`: [React ref](https://reactjs.org/docs/hooks-reference.html#useref) to be assigned the underlying text input ref.
45
69
46
-
-`autocompleteRef`: [React ref](https://reactjs.org/docs/hooks-reference.html#useref) to be assigned the [google autocomplete instance](https://developers.google.com/maps/documentation/javascript/reference/places-widget#Autocomplete).
47
-
48
-
-`onPlaceSelected: (place: `[PlaceResult](https://developers.google.com/maps/documentation/javascript/reference/places-service#PlaceResult)`, inputRef, autocompleteRef) => void`: The function gets invoked every time a user chooses location.
70
+
-`onPlaceSelected: (place: `[PlaceResult](https://developers.google.com/maps/documentation/javascript/reference/places-service#PlaceResult)`, inputRef, `[autocompleteRef](https://developers.google.com/maps/documentation/javascript/reference/places-widget#Autocomplete)`) => void`: The function gets invoked every time a user chooses location.
@@ -60,6 +82,86 @@ Alternatively if not passing the `apiKey` prop, you can include google autocompl
60
82
61
83
You can pass any prop specified for the hmtl [input tag](https://www.w3schools.com/tags/tag_input.asp). You can also set [options.fields](https://developers.google.com/maps/documentation/javascript/reference/places-service#PlaceResult) prop if you need extra information, now it defaults to basic data in order to control expenses.
62
84
85
+
## usePlacesWidget
86
+
87
+
Is a hook that has a single config argument. It has exactly the same interface as ReactGoogleAutocomplete props. This hook is actually used in the ReactGoogleAutocomplete component.
It has only one config argument which has propperties: `apiKey`, `ref`, `onPlaceSelected`, `options`, `inputAutocompleteValue`, `googleMapsScriptBaseUrl`. The same props described [here](#reactgoogleautocomplete)
This is an initial implementation of debounced google places autocomplete service. It gives you an option to reduce the amount of requests sent to google which reduce your costs. For the time being we decided to use `lodash.debounce` to save time and in the later versions we might write our own implementation of debounce with hooks. Because it uses lodash we also decided to not put it into the index library file so it lives in its own file and could be only imported by it.
-`apiKey`: Google api key, otherwise google api has to be loaded manually.
150
+
-`googleMapsScriptBaseUrl`: Provide custom google maps url. By default `https://maps.googleapis.com/maps/api/js`.
151
+
-`debounce`: Number of milliseconds to accumulate responses for.
152
+
-`options`: Default [options](https://developers.google.com/maps/documentation/javascript/reference/places-autocomplete-service#QueryAutocompletionRequest) which will be passed to every request.
153
+
154
+
### Returned value
155
+
156
+
The hook returns an object with properties:
157
+
158
+
-`placePredictions`: an array of [AutocompletePrediction](https://developers.google.com/maps/documentation/javascript/reference/places-autocomplete-service#AutocompleteResponse)
159
+
-`isPlacePredictionsLoading`: sets to true when a `getPlacePredictions` request is being sent and not yet resolved.
160
+
-`getPlacePredictions: (opt: `[Options](https://developers.google.com/maps/documentation/javascript/reference/places-autocomplete-service#AutocompletionRequest)`): void`: a function which you call whenever you want to request places predictions. Takes one [argument](https://developers.google.com/maps/documentation/javascript/reference/places-autocomplete-service#AutocompleteResponse).
161
+
-`queryPredictions`: an array of [QueryAutocompletePrediction](https://developers.google.com/maps/documentation/javascript/reference/places-autocomplete-service#QueryAutocompletePrediction)
162
+
-`isQueryPredictionsLoading`: sets to true when `getQueryPredictions` request is being sent and not yet resolved.
163
+
-`getQueryPredictions: (opt: `[Options](https://developers.google.com/maps/documentation/javascript/reference/places-autocomplete-service#QueryAutocompletionRequest)`): void`: a function which you call whenever you want to request query predictions. Takes one [argument](https://developers.google.com/maps/documentation/javascript/reference/places-autocomplete-service#QueryAutocompletionRequest).
164
+
63
165
## Examples
64
166
65
167
### Simple autocomplete with options
@@ -81,61 +183,65 @@ import Autocomplete from "react-google-autocomplete";
More examples(dynamic props, MaterialUI) how to use the lib could be found in `docs/examples.js`
236
+
We are planning on rewriting the library with TS/Flow in the later releases but you can already use it with TypeScript bacause we use [declaration files](https://www.typescriptlang.org/docs/handbook/declaration-files/dts-from-js.html).
133
237
134
238
### TODO
135
239
136
240
- Check that it fully works with SSR
241
+
- Add more UI libraries examples/supports
137
242
- Add eslint config(base-airbnb)
138
243
- Rewrite the lib to TS and add flow support
244
+
- Remove lodash and use own built-in solution for debouncing
0 commit comments