Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion folium/plugins/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,14 @@ class Search(JSCSSMixin, MacroElement):
{% if this.options %}
e.layer.setStyle({{ this.options|tojavascript }});
{% endif %}
if(e.layer._popup)
if(e.layer._popup) {
e.layer.openPopup();
} else if ({{this.layer.get_name()}}.getPopup()) {
// GeoJsonPopup is bound to the group. Temporarily bind it
// to the found feature so its content uses that feature.
e.layer.bindPopup({{this.layer.get_name()}}.getPopup())
.openPopup().unbindPopup();
}
})
{{this.layer.get_name()}}searchControl.on('search:collapsed', function(e) {
{{this.layer.get_name()}}.setStyle(function(feature){
Expand Down
21 changes: 21 additions & 0 deletions tests/plugins/test_search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import folium
from folium.plugins import Search
from folium.utilities import normalize


def test_search_opens_popup_bound_to_geojson_group():
m = folium.Map([0, 0], tiles=None)
layer = folium.GeoJson(
{
"type": "Feature",
"properties": {"name": "Example"},
"geometry": {"type": "Point", "coordinates": [0, 0]},
},
popup=folium.GeoJsonPopup(fields=["name"]),
).add_to(m)
search = Search(layer, search_label="name").add_to(m)
rendered = normalize(search._template.module.script(search))

# GeoJsonPopup is bound to the group, not to each indexed feature.
assert normalize(f"else if ({layer.get_name()}.getPopup())") in rendered
assert normalize(f"e.layer.bindPopup({layer.get_name()}.getPopup()).openPopup().unbindPopup();") in rendered
Loading