-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add Livewire component for address autofill
- Loading branch information
1 parent
2d13492
commit 39c5fc8
Showing
1 changed file
with
37 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<div> | ||
<input type="text" id="address-input" wire:model="address" placeholder="Enter your address" autocomplete="off"> | ||
<div id="address-suggestions" style="display: none;"> | ||
<ul> | ||
<template x-for="address in addresses" :key="address"> | ||
<li @click="selectAddress(address)"><x-text x-text="address"></li> | ||
</template> | ||
</ul> | ||
</div> | ||
</div> | ||
|
||
<script> | ||
document.addEventListener('livewire:load', function () { | ||
const input = document.getElementById('address-input'); | ||
let autocomplete; | ||
|
||
function initAutocomplete() { | ||
autocomplete = new google.maps.places.Autocomplete(input, {types: ['geocode']}); | ||
autocomplete.addListener('place_changed', fillInAddress); | ||
} | ||
|
||
function fillInAddress() { | ||
const place = autocomplete.getPlace(); | ||
Livewire.emit('setAddress', place.formatted_address); | ||
} | ||
|
||
initAutocomplete(); | ||
|
||
Livewire.on('setAddress', address => { | ||
input.value = address; | ||
document.getElementById('address-suggestions').style.display = 'none'; | ||
}); | ||
}); | ||
</script> | ||
|
||
@livewireStyles | ||
@livewireScripts |