1.0.5 • Published 1 year ago

geo-autocomplete-form v1.0.5

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

GeoAutocompleteForm

GeoAutocompleteForm is a React component that allows users to autofill geolocation information based on the input of a city and country. It utilizes the Google Maps API (Places Library) and Ant Design for UI elements.

Features

  • Autofill suggestions for cities and countries.
  • Uses Google Maps API (Places Library) to fetch geolocation data.
  • Displays city and country input fields with autocompletion and suggestions.
  • Returns an address object containing city, country, latitude, and longitude upon submission.
  • Customizable styles and placeholders for each field.

Installation

  1. Install Ant Design and React Google Maps API.
yarn add antd @react-google-maps/api
  1. Get a Google Maps API key with access to the "Places" library.

Props

AutoCompleteComponentProps

Prop NameTypeDefault ValueDescription
API_KEYstringundefinedYour Google Maps API Key. Required to load the Places Library.
onFinish(address: Address) => voidundefinedCallback function triggered on form submission, returning the selected address information.
formRowGutterGutter or [Gutter, Gutter][10, 10]Gutter size for the layout grid of the form.
countyColSpannumber | string24Column span for the country input field.
cityColSpannumber | string24Column span for the city input field.
submitColSpannumber | string24Column span for the submit button.
cityClassNamestringundefinedCSS class for the city input field.
countryClassNamestringundefinedCSS class for the country input field.
submitClassNamestringundefinedCSS class for the submit button.
cityStyleReact.CSSPropertiesundefinedInline style for the city input field.
countryStyleReact.CSSPropertiesundefinedInline style for the country input field.
submitStyleReact.CSSPropertiesundefinedInline style for the submit button.
cityPlaceholderstring'Select city'Placeholder text for the city input field.
countryPlaceholderstring'Select country'Placeholder text for the country input field.
submitTextstring'Submit'Text to display on the submit button.
loadingbooleanundefinedShows a loading state if set to true.
rowWrapperClassNamestringundefinedCSS class for the wrapper row element.
rowWrapperStyleReact.CSSPropertiesundefinedInline style for the wrapper row element.
cityColClassNamestringundefinedCSS class for the column containing the city input field.
cityColStyleReact.CSSPropertiesundefinedInline style for the column containing the city input field.
countryColClassNamestringundefinedCSS class for the column containing the country input field.
countryColStyleReact.CSSPropertiesundefinedInline style for the column containing the country input field.
submitColClassNamestringundefinedCSS class for the column containing the submit button.
submitColStyleReact.CSSPropertiesundefinedInline style for the column containing the submit button.
citySkeletonClassNamestringundefinedCSS class for the skeleton loader of the city input field.
citySkeletonStyleReact.CSSPropertiesundefinedInline style for the skeleton loader of the city input field.
countrySkeletonClassNamestringundefinedCSS class for the skeleton loader of the country input field.
countrySkeletonStyleReact.CSSPropertiesundefinedInline style for the skeleton loader of the country input field.
submitSkeletonClassNamestringundefinedCSS class for the skeleton loader of the submit button.
submitSkeletonStyleReact.CSSPropertiesundefinedInline style for the skeleton loader of the submit button.

Address Object

The Address object returned in the onFinish callback contains the following properties:

PropertyTypeDescription
namestringFull formatted address.
citystringName of the selected city.
countrystringName of the selected country.
latitudenumberLatitude coordinate of the location.
longitudenumberLongitude coordinate of the location.

Example Usage

import React from 'react';
import GeoAutocompleteForm from './GeoAutocompleteForm';

const MyComponent = () => {
  const handleFinish = (address) => {
    console.log('Selected Address:', address);
  };

  return (
    <GeoAutocompleteForm 
      API_KEY="YOUR_GOOGLE_MAPS_API_KEY" 
      onFinish={handleFinish} 
    />
  );
};

export default MyComponent;

Error Handling

  • If the API_KEY is missing, the component will display a message asking the user to provide a valid Google Maps API key.
  • If there is a failure to load the Google Maps API, it will display an error message.

Skeleton States

Skeletons are displayed while the Google Maps API is loading or if the loading prop is set to true. You can customize the skeleton styles with the citySkeletonClassName, countrySkeletonClassName, and submitSkeletonClassName props.

License

This project is licensed under the MIT License.

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

0.0.0

1 year ago