Skip to content

Commit 9b7eec2

Browse files
authored
Merge pull request #111 from Heyian/GeocompleteMinLength
Added minimum characters for autocompletion for cost optimization
2 parents bb9e4db + 4e291d9 commit 9b7eec2

File tree

5 files changed

+42
-6
lines changed

5 files changed

+42
-6
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -650,11 +650,15 @@ provide your own closure for handling reverse geocode data, as described in the
650650
->debug() // output the results of reverse geocoding in the browser console, useful for figuring out symbol formats
651651
->updateLatLng() // update the lat/lng fields on your form when a Place is selected
652652
->maxLength(1024)
653+
->minChars(0) // minimum number of characters before autocomplete starts
653654
->prefix('Choose:')
654655
->placeholder('Start typing an address ...')
655656
->geolocate() // add a suffix button which requests and reverse geocodes the device location
656657
->geolocateIcon('heroicon-o-map'), // override the default icon for the geolocate button
657658
```
659+
This field is *cost optimized*, so it will only start searching for places after 300ms of inactivity, and will not
660+
search while you are typing. This is to prevent excessive API calls, as Google charges for each one. If you set the minChars
661+
to 0, it will start searching immediately. I suggest you set it to a minimum of 3.
658662

659663
The Geocomplete field also offers many of the same features as Filament's TextInput,
660664
like prefixes, suffixes, placeholders, etc.

dist/cheesegrits/filament-google-maps/filament-google-geocomplete.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

resources/js/filament-google-geocomplete.js

+21-5
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ export default function filamentGoogleGeocomplete({
1212
placeField,
1313
reverseGeocodeUsing,
1414
hasReverseGeocodeUsing = false,
15+
minChars,
1516
}) {
17+
const geocompleteEl = isLocation ? statePath + "-fgm-address" : statePath;
18+
const geoComplete = document.getElementById(geocompleteEl);
19+
1620
return {
1721
geocoder: null,
1822
mapEl: null,
@@ -68,8 +72,23 @@ export default function filamentGoogleGeocomplete({
6872
init: function (mapEl) {
6973
console.log("geocomplete init");
7074
this.mapEl = mapEl;
71-
this.loadGMaps();
72-
},
75+
76+
let typingTimer;
77+
const doneTypingInterval = 300; // milliseconds
78+
79+
geoComplete.addEventListener('input', () => {
80+
clearTimeout(typingTimer);
81+
82+
if (geoComplete.value.length >= minChars) {
83+
typingTimer = setTimeout(() => {
84+
console.log('minChars met, loading GMaps');
85+
this.loadGMaps();
86+
}, doneTypingInterval);
87+
} else {
88+
console.log('minChars not met');
89+
}
90+
});
91+
},
7392

7493
createAutocomplete: function () {
7594
window.filamentGoogleMapsAPILoaded = true;
@@ -91,9 +110,6 @@ export default function filamentGoogleGeocomplete({
91110
types: types,
92111
};
93112

94-
const geocompleteEl = isLocation ? statePath + "-fgm-address" : statePath;
95-
const geoComplete = document.getElementById(geocompleteEl);
96-
97113
if (geoComplete) {
98114
window.addEventListener(
99115
"keydown",

resources/views/fields/filament-google-geocomplete.blade.php

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ class="w-full"
5858
countries: @js($getCountries()),
5959
debug: @js($getDebug()),
6060
gmaps: @js($getMapsUrl()),
61+
minChars: @js($getMinChars()),
6162
})"
6263
wire:ignore
6364
>

src/Fields/Geocomplete.php

+15
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ class Geocomplete extends Field implements Contracts\CanBeLengthConstrained, Con
5252

5353
protected Closure|bool $debug = false;
5454

55+
protected int $minChars = 0;
56+
5557
/**
5658
* DO NOT USE! Only used by the Radius Filter, to set the state path for the filter form data.
5759
*
@@ -416,6 +418,7 @@ public function getGeocompleteConfig(): string
416418
'placeField' => $this->getPlaceField(),
417419
'debug' => $this->getDebug(),
418420
'gmaps' => $this->getMapsUrl(),
421+
'minChars' => $this->getMinChars(),
419422
]);
420423

421424
//ray($config);
@@ -456,4 +459,16 @@ public function getFormattedState(): string
456459

457460
return $state;
458461
}
462+
463+
public function minChars(int $minChars): static
464+
{
465+
$this->minChars = $minChars;
466+
467+
return $this;
468+
}
469+
470+
public function getMinChars(): int
471+
{
472+
return $this->evaluate($this->minChars);
473+
}
459474
}

0 commit comments

Comments
 (0)