diff --git a/folium/plugins/search.py b/folium/plugins/search.py index 92e70c203d..e82876b875 100644 --- a/folium/plugins/search.py +++ b/folium/plugins/search.py @@ -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){ diff --git a/tests/plugins/test_search.py b/tests/plugins/test_search.py new file mode 100644 index 0000000000..4cfd7e13eb --- /dev/null +++ b/tests/plugins/test_search.py @@ -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