react-native-google-places-autocomplete 2.5.6 → 2.5.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/GooglePlacesAutocomplete.d.ts +11 -0
- package/GooglePlacesAutocomplete.js +97 -22
- package/package.json +3 -2
|
@@ -286,11 +286,15 @@ interface GooglePlaceData {
|
|
|
286
286
|
interface Point {
|
|
287
287
|
lat: number;
|
|
288
288
|
lng: number;
|
|
289
|
+
latitude: number;
|
|
290
|
+
longitude: number;
|
|
289
291
|
}
|
|
290
292
|
|
|
291
293
|
interface AddressComponent {
|
|
292
294
|
long_name: string;
|
|
293
295
|
short_name: string;
|
|
296
|
+
longText: string;
|
|
297
|
+
shortText: string;
|
|
294
298
|
types: PlaceType[];
|
|
295
299
|
}
|
|
296
300
|
|
|
@@ -323,6 +327,11 @@ interface GooglePlaceDetail {
|
|
|
323
327
|
url: string;
|
|
324
328
|
utc_offset: number;
|
|
325
329
|
vicinity: string;
|
|
330
|
+
// New Places API parameters
|
|
331
|
+
addressComponents: AddressComponent[];
|
|
332
|
+
adrFormatAddress: string;
|
|
333
|
+
formattedAddress: string;
|
|
334
|
+
location: Point;
|
|
326
335
|
}
|
|
327
336
|
|
|
328
337
|
/** @see https://developers.google.com/places/web-service/autocomplete */
|
|
@@ -431,6 +440,8 @@ interface GooglePlacesAutocompleteProps {
|
|
|
431
440
|
/** text input props */
|
|
432
441
|
textInputProps?: TextInputProps | Object;
|
|
433
442
|
timeout?: number;
|
|
443
|
+
isNewPlacesAPI?: boolean;
|
|
444
|
+
fields?: string;
|
|
434
445
|
}
|
|
435
446
|
|
|
436
447
|
export type GooglePlacesAutocompleteRef = {
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
import debounce from 'lodash.debounce';
|
|
3
3
|
import PropTypes from 'prop-types';
|
|
4
4
|
import Qs from 'qs';
|
|
5
|
+
import { v4 as uuidv4 } from 'uuid';
|
|
5
6
|
import React, {
|
|
6
7
|
forwardRef,
|
|
7
8
|
useMemo,
|
|
@@ -159,7 +160,7 @@ export const GooglePlacesAutocomplete = forwardRef((props, ref) => {
|
|
|
159
160
|
const [listLoaderDisplayed, setListLoaderDisplayed] = useState(false);
|
|
160
161
|
|
|
161
162
|
const inputRef = useRef();
|
|
162
|
-
|
|
163
|
+
const [sessionToken, setSessionToken] = useState(uuidv4());
|
|
163
164
|
useEffect(() => {
|
|
164
165
|
setUrl(getRequestUrl(props.requestUrl));
|
|
165
166
|
}, [getRequestUrl, props.requestUrl]);
|
|
@@ -281,10 +282,14 @@ export const GooglePlacesAutocomplete = forwardRef((props, ref) => {
|
|
|
281
282
|
|
|
282
283
|
if (request.status === 200) {
|
|
283
284
|
const responseJSON = JSON.parse(request.responseText);
|
|
284
|
-
|
|
285
|
-
|
|
285
|
+
if (
|
|
286
|
+
responseJSON.status === 'OK' ||
|
|
287
|
+
(props.isNewPlacesAPI && responseJSON.id)
|
|
288
|
+
) {
|
|
286
289
|
// if (_isMounted === true) {
|
|
287
|
-
const details =
|
|
290
|
+
const details = props.isNewPlacesAPI
|
|
291
|
+
? responseJSON
|
|
292
|
+
: responseJSON.result;
|
|
288
293
|
_disableRowLoaders();
|
|
289
294
|
_onBlur();
|
|
290
295
|
|
|
@@ -322,16 +327,29 @@ export const GooglePlacesAutocomplete = forwardRef((props, ref) => {
|
|
|
322
327
|
}
|
|
323
328
|
};
|
|
324
329
|
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
330
|
+
if (props.isNewPlacesAPI) {
|
|
331
|
+
request.open(
|
|
332
|
+
'GET',
|
|
333
|
+
`${url}/v1/places/${rowData.place_id}?` +
|
|
334
|
+
Qs.stringify({
|
|
335
|
+
key: props.query.key,
|
|
336
|
+
sessionToken,
|
|
337
|
+
fields: props.fields,
|
|
338
|
+
}),
|
|
339
|
+
);
|
|
340
|
+
setSessionToken(uuidv4());
|
|
341
|
+
} else {
|
|
342
|
+
request.open(
|
|
343
|
+
'GET',
|
|
344
|
+
`${url}/place/details/json?` +
|
|
345
|
+
Qs.stringify({
|
|
346
|
+
key: props.query.key,
|
|
347
|
+
placeid: rowData.place_id,
|
|
348
|
+
language: props.query.language,
|
|
349
|
+
...props.GooglePlacesDetailsQuery,
|
|
350
|
+
}),
|
|
351
|
+
);
|
|
352
|
+
}
|
|
335
353
|
|
|
336
354
|
request.withCredentials = requestShouldUseWithCredentials();
|
|
337
355
|
setRequestHeaders(request, getRequestHeaders(props.requestUrl));
|
|
@@ -419,6 +437,29 @@ export const GooglePlacesAutocomplete = forwardRef((props, ref) => {
|
|
|
419
437
|
return results;
|
|
420
438
|
};
|
|
421
439
|
|
|
440
|
+
const _filterResultsByPlacePredictions = (unfilteredResults) => {
|
|
441
|
+
const results = [];
|
|
442
|
+
for (let i = 0; i < unfilteredResults.length; i++) {
|
|
443
|
+
if (unfilteredResults[i].placePrediction) {
|
|
444
|
+
results.push({
|
|
445
|
+
description: unfilteredResults[i].placePrediction.text?.text,
|
|
446
|
+
place_id: unfilteredResults[i].placePrediction.placeId,
|
|
447
|
+
reference: unfilteredResults[i].placePrediction.placeId,
|
|
448
|
+
structured_formatting: {
|
|
449
|
+
main_text:
|
|
450
|
+
unfilteredResults[i].placePrediction.structuredFormat?.mainText
|
|
451
|
+
?.text,
|
|
452
|
+
secondary_text:
|
|
453
|
+
unfilteredResults[i].placePrediction.structuredFormat
|
|
454
|
+
?.secondaryText?.text,
|
|
455
|
+
},
|
|
456
|
+
types: unfilteredResults[i].placePrediction.types ?? [],
|
|
457
|
+
});
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
return results;
|
|
461
|
+
};
|
|
462
|
+
|
|
422
463
|
const _requestNearby = (latitude, longitude) => {
|
|
423
464
|
_abortRequests();
|
|
424
465
|
|
|
@@ -524,6 +565,7 @@ export const GooglePlacesAutocomplete = forwardRef((props, ref) => {
|
|
|
524
565
|
setListLoaderDisplayed(false);
|
|
525
566
|
if (request.status === 200) {
|
|
526
567
|
const responseJSON = JSON.parse(request.responseText);
|
|
568
|
+
|
|
527
569
|
if (typeof responseJSON.predictions !== 'undefined') {
|
|
528
570
|
// if (_isMounted === true) {
|
|
529
571
|
const results =
|
|
@@ -538,6 +580,14 @@ export const GooglePlacesAutocomplete = forwardRef((props, ref) => {
|
|
|
538
580
|
setDataSource(buildRowsFromResults(results, text));
|
|
539
581
|
// }
|
|
540
582
|
}
|
|
583
|
+
if (typeof responseJSON.suggestions !== 'undefined') {
|
|
584
|
+
const results = _filterResultsByPlacePredictions(
|
|
585
|
+
responseJSON.suggestions,
|
|
586
|
+
);
|
|
587
|
+
|
|
588
|
+
_results = results;
|
|
589
|
+
setDataSource(buildRowsFromResults(results, text));
|
|
590
|
+
}
|
|
541
591
|
if (typeof responseJSON.error_message !== 'undefined') {
|
|
542
592
|
if (!props.onFail)
|
|
543
593
|
console.warn(
|
|
@@ -556,18 +606,39 @@ export const GooglePlacesAutocomplete = forwardRef((props, ref) => {
|
|
|
556
606
|
setStateText(props.preProcess(text));
|
|
557
607
|
}
|
|
558
608
|
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
609
|
+
if (props.isNewPlacesAPI) {
|
|
610
|
+
const keyQueryParam = props.query.key
|
|
611
|
+
? '?' +
|
|
612
|
+
Qs.stringify({
|
|
613
|
+
key: props.query.key,
|
|
614
|
+
})
|
|
615
|
+
: '';
|
|
616
|
+
request.open('POST', `${url}/v1/places:autocomplete${keyQueryParam}`);
|
|
617
|
+
} else {
|
|
618
|
+
request.open(
|
|
619
|
+
'GET',
|
|
620
|
+
`${url}/place/autocomplete/json?input=` +
|
|
621
|
+
encodeURIComponent(text) +
|
|
622
|
+
'&' +
|
|
623
|
+
Qs.stringify(props.query),
|
|
624
|
+
);
|
|
625
|
+
}
|
|
566
626
|
|
|
567
627
|
request.withCredentials = requestShouldUseWithCredentials();
|
|
568
628
|
setRequestHeaders(request, getRequestHeaders(props.requestUrl));
|
|
569
629
|
|
|
570
|
-
|
|
630
|
+
if (props.isNewPlacesAPI) {
|
|
631
|
+
const { key, locationbias, types, ...rest } = props.query;
|
|
632
|
+
request.send(
|
|
633
|
+
JSON.stringify({
|
|
634
|
+
input: text,
|
|
635
|
+
sessionToken,
|
|
636
|
+
...rest,
|
|
637
|
+
}),
|
|
638
|
+
);
|
|
639
|
+
} else {
|
|
640
|
+
request.send();
|
|
641
|
+
}
|
|
571
642
|
} else {
|
|
572
643
|
_results = [];
|
|
573
644
|
setDataSource(buildRowsFromResults([]));
|
|
@@ -942,6 +1013,8 @@ GooglePlacesAutocomplete.propTypes = {
|
|
|
942
1013
|
textInputHide: PropTypes.bool,
|
|
943
1014
|
textInputProps: PropTypes.object,
|
|
944
1015
|
timeout: PropTypes.number,
|
|
1016
|
+
isNewPlacesAPI: PropTypes.bool,
|
|
1017
|
+
fields: PropTypes.string,
|
|
945
1018
|
};
|
|
946
1019
|
|
|
947
1020
|
GooglePlacesAutocomplete.defaultProps = {
|
|
@@ -986,6 +1059,8 @@ GooglePlacesAutocomplete.defaultProps = {
|
|
|
986
1059
|
textInputHide: false,
|
|
987
1060
|
textInputProps: {},
|
|
988
1061
|
timeout: 20000,
|
|
1062
|
+
isNewPlacesAPI: false,
|
|
1063
|
+
fields: '*',
|
|
989
1064
|
};
|
|
990
1065
|
|
|
991
1066
|
GooglePlacesAutocomplete.displayName = 'GooglePlacesAutocomplete';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-google-places-autocomplete",
|
|
3
|
-
"version": "2.5.
|
|
3
|
+
"version": "2.5.7",
|
|
4
4
|
"description": "Customizable Google Places autocomplete component for iOS and Android React-Native apps",
|
|
5
5
|
"main": "GooglePlacesAutocomplete.js",
|
|
6
6
|
"types": "GooglePlacesAutocomplete.d.ts",
|
|
@@ -31,7 +31,8 @@
|
|
|
31
31
|
"dependencies": {
|
|
32
32
|
"lodash.debounce": "^4.0.8",
|
|
33
33
|
"prop-types": "^15.7.2",
|
|
34
|
-
"qs": "~6.9.1"
|
|
34
|
+
"qs": "~6.9.1",
|
|
35
|
+
"uuid": "^10.0.0"
|
|
35
36
|
},
|
|
36
37
|
"devDependencies": {
|
|
37
38
|
"@react-native-community/eslint-config": "2.0.0",
|