0.2.0 • Published 3 years ago

react-native-google-autocomplete v0.2.0

Weekly downloads
1,220
License
MIT
Repository
-
Last release
3 years ago

React-Native-Google-Autocomplete

Using render props to make google autocomplete work nicely with any design.

Installation

yarn add react-native-google-autocomplete

Props

PropsDescriptions
apiKeyYour api key get from https://developers.google.com/places/documentation
debounceoptional - default 300
languageoptional - default en
queryTypesoptional - default address - https://developers.google.com/places/web-service/autocomplete#place_types
minLengthoptional - default 2 - this is the min length of the term search before start
componentsoptional - A grouping of places to which you would like to restrict your results.
radiusoptional - The distance (in meters) within which to return place results.
latoptional - The latitude. If provide lng is required
lngoptional - The longitue. If provide lat is required

Render Props

PropsDescriptions
inputValueA string you can put to your input for controlled input
handleTextChangemost important function this is the callback for the text change just put it inside your TextInput
locationResultsThe array result
fetchDetailsHttp call when you have the place_id, good when you want to get more info after click an item
isSearchingBoolean if search is on
clearSearchClear the search result, can be nice when you have a clear button next to your text input

*(clearSearch was previously clearSearchs)

Results

The locationResults give you this. The maximum result set by Google is 5 location by query.

export interface GoogleLocationResult {
  description: string;
  id: string;
  matched_substrings: Array<{
    length: number;
    offset: number;
  }>;
  place_id: string;
  reference: string;
  structured_formatting: {
    main_text: string;
    secondary_text: string;
    main_text_matched_substrings: Array<{
      length: number;
    }>;
  };
  terms: Array<{
    offset: number;
    value: string;
  }>;
  types: string[];
}

When call the fetchDetails this is what you got

export interface GoogleLocationDetailResult {
  adr_address: string;
  formatted_address: string;
  icon: string;
  id: string;
  name: string;
  place_id: string;
  scope: string;
  reference: string;
  url: string;
  utc_offset: number;
  vicinity: string;
  types: string[];
  geometry: {
    location: {
      lat: number;
      lng: number;
    };
    viewport: {
      [type: string]: {
        lat: number;
        lng: number;
      };
    };
  };
  address_components: Array<{
    long_name: string;
    short_name: string;
    types: string[];
  }>;
}

Examples

import { GoogleAutoComplete } from 'react-native-google-autocomplete';

function MyComponent() {
  return (
    <GoogleAutoComplete apiKey="YOUR API KEY" debounce={300}>
      {({ inputValue, handleTextChange, locationResults, fetchDetails }) => (
        <React.Fragment>
          <TextInput
            style={{
              height: 40,
              width: 300,
              borderWidth: 1,
              paddingHorizontal: 16,
            }}
            value={inputValue}
            onChangeText={handleTextChange}
            placeholder="Location..."
          />
          <ScrollView style={{ maxHeight: 100 }}>
            {locationResults.map((el, i) => (
              <LocationItem
                {...el}
                fetchDetails={fetchDetails}
                key={String(i)}
              />
            ))}
          </ScrollView>
        </React.Fragment>
      )}
    </GoogleAutoComplete>
  );
}

Typings

You can import both result typing if you need for flow or typescript.

import { GoogleLocationDetailResult, GoogleLocationResult } from 'react-native-google-autocomplete';

Restrict by country

If you want to restrict the search by country you can add this as a props components="country:ca". This here would example restrict it to Canada only.

0.2.0

3 years ago

0.1.9

4 years ago

0.1.8

4 years ago

0.1.7

5 years ago

0.1.6

5 years ago

0.1.5

6 years ago

0.1.4

6 years ago

0.1.3

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago