Getting Started
Components
- Animated Number
- Auto Submit
- Carousel
- Character Counter
- Chartjs
- Checkbox Select All
- Clipboard
- Color Picker
- Content Loader
- Dialog
- Dropdown
- Glow
- Lightbox
- Notification
- Password Visibility
- Places Autocomplete
- Popover
- Prefetch
- Rails Nested Form
- Read More
- Remote Rails
- Reveal Controller
- Scroll Progress
- Scroll Reveal
- Scroll To
- Sortable
- Sound
- Textarea Autogrow
- Timeago
Installation
Install the package
$ yarn add stimulus-places-autocomplete
Register the controller in your application
app/javascript/controllers/index.jsimport { Application } from '@hotwired/stimulus' import PlacesAutocomplete from 'stimulus-places-autocomplete' const application = Application.start() application.register('places-autocomplete', 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-autocomplete"
data-action="google-maps-callback@window->places-autocomplete#initAutocomplete"
data-places-autocomplete-country-value='["fr"]'
>
<input
type="text"
data-action="keydown->places-autocomplete#preventSubmit"
data-places-autocomplete-target="address"
placeholder="Search a location"
/>
<input type="hidden" data-places-autocomplete-target="city" />
<input type="hidden" data-places-autocomplete-target="streetNumber" />
<input type="hidden" data-places-autocomplete-target="route" />
<input type="hidden" data-places-autocomplete-target="county" />
<input type="hidden" data-places-autocomplete-target="state" />
<input type="hidden" data-places-autocomplete-target="postalCode" />
<input type="hidden" data-places-autocomplete-target="country" />
<input type="hidden" data-places-autocomplete-target="longitude" />
<input type="hidden" data-places-autocomplete-target="latitude" />
</div>
Configuration
Attribute | Default | Description | Optional |
---|---|---|---|
data-places-autocomplete-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.