This repository has been archived by the owner on Feb 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 234
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(controlled-search): expose search method in places.js (#659)
* feat(controlled-search): expose search method in places.js and update example to use it * chore(whitespace): fix whitespace error from rebase * test(controlled-search): add test for .search method
- Loading branch information
1 parent
2b0e06c
commit 4ddd681
Showing
7 changed files
with
131 additions
and
50 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
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
30 changes: 30 additions & 0 deletions
30
docs/source/partials/examples/reverse-city-search/_code_places.html.erb
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,30 @@ | ||
<div> | ||
<strong>Searching around: </strong> | ||
<input type="search" id="reverse-city" placeholder="What's your favorite city?"></div> | ||
</div> | ||
|
||
<%= javascript_include_tag config[:places_lib_url] %> | ||
<script> | ||
var placesAutocomplete = places({ | ||
appId: '<%= app_id %>', | ||
apiKey: '<%= api_key %>', | ||
container: document.querySelector('#reverse-city') | ||
}).configure({ | ||
type: 'city', | ||
hitsPerPage: 1, | ||
aroundLatLngViaIP: true | ||
}); | ||
|
||
placesAutocomplete.search().then(function(suggestions) { | ||
if (!suggestions[0]) { | ||
return; | ||
} | ||
|
||
var name = suggestions[0].name; | ||
var country = suggestions[0].country; | ||
var formattedCity = locale_names[0] + ', ' + country; | ||
|
||
var infoElt = document.querySelector("#reverse-city"); | ||
infoElt.value = formattedCity; | ||
}); | ||
</script> |
46 changes: 0 additions & 46 deletions
46
docs/source/partials/examples/reverse-city-search/_demo.html.erb
This file was deleted.
Oops, something went wrong.
54 changes: 54 additions & 0 deletions
54
docs/source/partials/examples/reverse-city-search/_demo_places.html.erb
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,54 @@ | ||
<div> | ||
<p>When constructing a search UI using InstantSearch, you may want to filter your dataset to only display some of your products based on the distance to the end user. This is usually done using the aroundLatLngViaIP filter, or aroundLatLng filter if you have access to precise geolocation information. However geographical filters are hard to interpret when displayed in raw format, as noone really knows where the coordinates <span id="coords"><code>48.8566, 2.34287</code></span> are.</p> | ||
|
||
<p>Using Places, you can do a query to find the city in which your user is located and display that city name instead of a geolocation.</p> | ||
</div> | ||
|
||
<p><strong>Example:</strong></p> | ||
|
||
<div> | ||
<strong>Searching around: </strong> | ||
<input type="search" id="reverse-city" placeholder="Your favorite city" /> | ||
</div> | ||
|
||
<%= javascript_include_tag config[:places_lib_url] %> | ||
<script> | ||
var placesAutocomplete = places({ | ||
appId: '<%= app_id %>', | ||
apiKey: '<%= api_key %>', | ||
container: document.querySelector('#reverse-city') | ||
}).configure({ | ||
type: 'city', | ||
hitsPerPage: 5, | ||
aroundLatLngViaIP: true, | ||
debug: true | ||
}); | ||
|
||
placesAutocomplete.search().then(function(suggestions) { | ||
if (!suggestions[0]) { | ||
return; | ||
} | ||
|
||
var name = suggestions[0].name; | ||
var country = suggestions[0].country; | ||
var latlng = suggestions[0].latlng; | ||
var formattedCity = name + ', ' + country; | ||
|
||
var coordElt = document.querySelector("#coords"); | ||
var lat = latlng.lat; | ||
var lng = latlng.lng; | ||
coordElt.innerHTML = '<code>' + lat + ', ' + lng + '</code>'; | ||
|
||
var infoElt = document.querySelector("#reverse-city"); | ||
infoElt.value = formattedCity; | ||
}); | ||
|
||
placesAutocomplete.on('change', (response) => { | ||
var suggestion = response.suggestion; | ||
var latlng = suggestion.latlng; | ||
var coordElt = document.querySelector("#coords"); | ||
var lat = latlng.lat; | ||
var lng = latlng.lng; | ||
coordElt.innerHTML = '<code>' + lat + ', ' + lng + '</code>'; | ||
}); | ||
</script> |
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
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