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

@coredna/magna-plugin-google-places-autocomplete

v1.1.1

Published

Magna is a tree based javascript router, plugin runner and ajax framework

Downloads

9

Readme

Magna Plugin: GooglePlacesAutocomplete

This plugin will add the functionality for a google places autocomplete to be added to your page.

You do not need to include the google places script in your page as this code will handle loading the api when only the API_KEY is specified

By default you provide it with the fields, their type and their matching selector in your page and it will fill in the details with what is returned from the google api request.

import Magna from '@coredna/magna'
import GooglePlacesAutocomplete from '@coredna/magna-plugin-google-places-autocomplete'

const app = new Magna([
  new GooglePlacesAutocomplete({
    selector: '#my-selector',
    fields: GooglePlacesAutocomplete.defaultFields,
    API_KEY: 'AIzaSyA....',
  })
]).start()

Install

Install through either npm or yarn

npm install @coredna/magna-plugin-google-places-autocomplete

or

yarn add @coredna/magna-plugin-google-places-autocomplete

Config

| property | type | default | required | Description | |--- |--- |--- |--- |--- | | API_KEY | string | null | true | Google places API KEY | country | string | 'US' | false | Country code, can specify multiple with comma separation 'US,AU' | | fields | object | {} | false | Object of fields containing key, selector and type see Fields | onPlaceChanged | Function | null | false | Run a callback when the address has been retrieved from google, cancel default actions by returning false Extend default fields |

Fields

You can set your own fields you can use any of:

| property | type | default | required | Description | |--- |--- |--- |--- |--- | | address | object | { selector: '[name="address1"]', type: 'long_name' } | false | Street address eg. '348 High Street' | | unit | object | { selector: '[name="address2"]', type: 'long_name' } | false | Unit number "address_line2" eg. 'Unit 1' | | city | object | { selector: '[name="city"]', type: 'long_name' } | false | Nearest city (suburb) eg. 'Windsor' | | postcode | object | { selector: '[name="postcode"]', type: 'long_name' } | false | Postal code (Zip code) eg. '3182' | | state | object | { selector: '[name="state"]', type: 'long_name' } | false | State eg. 'VIC' 'Victoria' | | country | object | { selector: '[name="countryid"]', type: 'long_name' } | false | Country eg. 'AU' 'Australia'|

| type | description | |--- |--- | | long_name | Long version of the value eg. 'Victoria' | | short_name | Short version of the value eg. 'VIC' |

const fields = {
  ['address|unit|postcode|city|state|country']: {
    selector: '[name="my_field_name"]',
    type: 'long_name|short_name'
  }
}

Default field objects

To help save time GooglePlacesAutocomplete has a static property defaultFields with useful defaults you should be able to use in your code immediately, but you cant still extend or modify

GooglePlacesAutocomplete.defaultFields = {
 country: {
   selector: '[name=countryid]',
   type: 'short_name'
 },
 unit: {
   selector: '[name=address2]',
   type: 'long_name'
 },
 address: {
   selector: '[name=address1]',
   type: 'long_name'
 },
 postcode: {
   selector: '[name=postcode]',
   type: 'short_name'
 },
 city: {
   selector: '[name=city]',
   type: 'long_name'
 },
 state: {
   selector: '[name=state]',
   type: 'short_name'
 }
}

If you need to update the fields to have a prefix you can do so using the static method GooglePlacesAutocomplet.prefixDefaultFields(prefix)

const userPrefixedFields = GooglePlacesAutocomplete.prefixDefaultFields('user_') // => { address: { selector: '[name=user_address1]'} ...}

Extend default fields

Using ES6 you can select specific entries, and extend or create your own

// extract individual default fields
const { address, country } = GooglePlacesAutocomplete.defaultFields
new GooglePlacesAutocomplete({
  selector: '#my-selector',
  API_KEY: 'AIzaSyA....',
  fields: {
    address,
    country: { ...country, selector: '#my-custom-selector' },
    city: { selector: '#my-custom-selector', type: 'long_name' },
  },
})

Events

You are able to hook into the google places api event onPlaceChange, and assigning the formatted values to your site using updateFields

const { address, country } = GooglePlacesAutocomplete.defaultFields
new GooglePlacesAutocomplete({
  onPlaceChange({ 
    place, 
    addressComponentObject, 
    address, 
    fields 
  }) {
    // do something with the place object
    return false // if you return false it will cancel the default behaviour
  },
  updateFields(fields) {
    console.log(fields)
  }
})