npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ftools-suit/autocomplete

v2.2.0

Published

Autocomplete and geocoding functions/hooks of google maps

Downloads

232

Readme

@ftools-suit/autocomplete

This library focuses on giving google predictions and autocompletions

Installation

Using npm

npm install @ftools-suit/autocomplete

Using yarn

yarn add @ftools-suit/autocomplete

How to imports?

import { useAutoComplete, geoMappedAddress, getGeocode, getLatLng, getMappedAddress, getZipCode } from '@ftools-suit/autocomplete'

const { useAutoComplete, geoMappedAddress, getGeocode, getLatLng, getMappedAddress, getZipCode } = require('@ftools-suit/autocomplete')

// OR
import useAutoComplete from '@ftools-suit/autocomplete/react'
const useAutoComplete = require('@ftools-suit/autocomplete/react')

Getting started

This package is based on the google places autocomplete service, so we would need to load the library and request an access to google to use its apis.

APIs

To use this package you need to generate a google maps api key

Load the library:

Use the script tag to load google maps library in your index.html or another entry point

<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places"></script>

Create component to use Autocomplete

You can use our hook and with the help of our geocoding functions you can create your own component with the UI of your choice.

import useAutocomplete from '@ftools-suit/autocomplete/react'
import { geoMappedAddress } from '@ftools-suit/autocomplete/geocode'

const AddressAutocomplete = ({ onSelectAddress }) => {
    const { place, setPlace, predictions, clearPredictions } = useAutocomplete({
        debounce: 200,
        requestOptions: { 
            componentRestrictions: { country: 'mx' }
         } 
    })

    // Example: San Francisco 223
    const handleOnChange = useCallBack((e) => {
        setPlace(e.target.value)
    }, [])

    const handleSelectPrediction = async (placeId) => {
       clearPredictions() // To clean predictions data
       const address = await geoMappedAddress(placeId) // Return object with a address
       setPlace(address.fullAddress, false) // To set place value and second parameter is so that it does not make a request returned to the api
       onSelectAddress(address)
    }

    const renderPredictions = () => {
        return predictions.data.map((prediction) => (
            <li key={prediction.place_id} onClick={() => handleSelectPrediction(prediction.place_id)} >{prediction.structured_formatting.main_text}</li>
        ))
    }

    return (
        <div>
            <input value={place} onChange={handleOnChange} />
            {!predictions.isLoading && predictions.status === 'OK' && <ul>{renderPredictions()}</ul>}
        </div>
    )
}

API Reference

To improve the autocomplete hook we can pass an object as a parameter to indicate options to it

const autocomplete = useAutocomplete({
    debounce: 200,
    requestOptions: {
        componentRestrictions: { country: ['mx'] } // To filter by country
    },
    customFilters: { state: 'cdmx' }, // To filter by state
    defaults: {
        place: 'San Diego 332'
    }
})

Parameter options (optional)

Options to pass to the hook

| Key | Type | Default | Description | | -- | -- | -- | -- | | debounce | number | 300 | Milisecons delay before making a request with Google places | requestOptions | object | {} | Docs | customFilters | object | {state: undefined} | Customizable filters to return predictions | defaults | object | { place: '', shouldPrediction: false, isLoading: false, status: '' } | Defaults value to setter on a hook

GeoCode

We offer geocoding utility functions for making complex things simple

To obtain results predictions by placeId:

import { getGeoCode } from '@ftools-suit/autocomplete/geocode'


(async () => {
    const results = await getGeoCode({ placeId: '123kjao02a123c' }) 
})

To obtain a final geocoding address and its mapped components:

import { geoMappedAddress } from '@ftools-suit/autocomplete/geocode'

const handleSelect = async (placeId) => {
    const { street, streetNumber, zipCode, neighborhood, state, lat, lng, country, plusCode, subpremise, parking, floor, intersection, fullAddress } = await geoMappedAddress(placeId)
    // Now you can do whatever you want with this data...
}

To obtain postal code of an adress:

import { getZipCode } from '@ftools-suit/autocomplete/geocode'

const results =  GeocoderResult // Object returned by getGeoCode function
getZipCode(results) // '0700'