This component is available on GitHub.

Stimulus Places Autocomplete

A Stimulus controller for Google Places Autocomplete.

Installation

$ yarn add stimulus-places-autocomplete

And use it in your JS file:

// Probably in app/javascript/controllers/index.js

import { Application } from 'stimulus'
import PlacesAutocomplete from 'stimulus-places-autocomplete'

const application = Application.start()
application.register('places', PlacesAutocomplete)

Google Maps Callback

Load the Google Maps Api JavaScript in your <head>:

<head>
  <script
    src="https://maps.googleapis.com/maps/api/js?key={your_key_here}&libraries=places&callback=initAutocomplete"
    async=""
    defer=""
  ></script>
</head>

Define a callback to trigger the PlacesAutocomplete controller in your views:

window.initAutocomplete = function () {
  const event = new Event('google-maps-callback', {
    bubbles: true,
    cancelable: true,
  })
  window.dispatchEvent(event)
}

πŸ‘‰ Tip: You don't need this callback if you add the <div> asynchronously in your DOM.

Usage

<div
  data-controller="places"
  data-action="google-maps-callback@window->places#initAutocomplete"
  data-places-country-value='["fr"]'
>
  <input
    type="text"
    data-action="keydown->places#preventSubmit"
    data-places-target="address"
    placeholder="Search a location"
  />

  <input type="hidden" data-places-target="city" />
  <input type="hidden" data-places-target="streetNumber" />
  <input type="hidden" data-places-target="route" />
  <input type="hidden" data-places-target="county" />
  <input type="hidden" data-places-target="state" />
  <input type="hidden" data-places-target="postalCode" />
  <input type="hidden" data-places-target="country" />

  <input type="hidden" data-places-target="longitude" />
  <input type="hidden" data-places-target="latitude" />
</div>

Configuration

AttributeDefaultDescriptionOptional
data-places-country-value[]Array of countries the autocomplete is restricted to. Must be parseable by JSON.parse.βœ…

If a target does not exist, it will be ignored.

πŸŽ› Extending Controller

You can use inheritance to extend the functionality of any Stimulus component:

import PlacesAutocomplete from 'stimulus-places-autocomplete'

export default class extends PlacesAutocomplete {
  connect() {
    super.connect()
    console.log('Do what you want here.')

    // The google.maps.places.Autocomplete instance.
    this.autocomplete
  }

  // You can override the `initAutocomplete` method here.
  initAutocomplete() {
    super.initAutocomplete()
  }

  // You can override the `placeChanged` method here.
  placeChanged() {
    super.placeChanged()
  }

  // You can set the Autocomplete options in this getter.
  get autocompleteOptions() {
    return {
      fields: ['address_components', 'geometry'],
    }
  }
}

This controller will automatically have access to targets defined in the parent class.

If you override the connect, disconnect or any other methods from the parent, you'll want to call super.method() to make sure the parent functionality is executed.

β˜•οΈ Sponsors

Stimulus Component is an MIT licensed open source project and completely free to use. However, the amount of effort needed to maintain and develop new features for the project is not sustainable without proper financial backing. You can support Stimulus Components development on Github Sponsors. πŸ™

πŸ‘· Contributing

Do not hesitate to contribute to the project by adapting or adding features ! Bug reports or pull requests are welcome.

Don’t forget to drop a 🌟 on Github to support the project.

πŸ“ License

This project is released under the MIT license.