Render the advanced search form without JavaScript

We were using the form and then showing it with JavaScript when advanced
search terms were present. Now we hide it with JavaScript when no
advanced search are present. This means users without JavaScript
(including users with JavaScript enabled but bad internet connections
preventing the JavaScript to load) can now access the form.

The other main difference between the two versions is the way the form
flashes while JavaScript is loading.

Previously, the form would always be hidden when no terms had been
introduced. However, when these terms were present, after submitting the
form it would briefly be hidden and then shown again.

Now the opposite happens. When advanced search terms are present, the
form is shown at all times. However, when they aren't, the form is
briefly shown before it disappears.

Here the previous behavior is arguably better because most of the time
these terms will not be present.

So basically we're significantly improving the experience of some users
at the cost of slightly worsen the experience of other users.

We're also hiding the button to show the form when JavaScript is
disabled, since in this scenario it's useless. We're using the `hidden`
attribute so hidden buttons can be detected in CSS.
This commit is contained in:
Javi Martín
2021-06-28 23:24:13 +02:00
parent f6979d1a79
commit 6a44ca350d
3 changed files with 25 additions and 6 deletions

View File

@@ -14,11 +14,16 @@
} }
}, },
initialize: function() { initialize: function() {
var toggle_button = $("#js-advanced-search-title");
toggle_button.removeAttr("hidden");
if (App.AdvancedSearch.advanced_search_terms()) { if (App.AdvancedSearch.advanced_search_terms()) {
$("#js-advanced-search").show();
App.AdvancedSearch.toggle_date_options(); App.AdvancedSearch.toggle_date_options();
} else {
$("#js-advanced-search").hide();
} }
$("#js-advanced-search-title").on({ toggle_button.on({
click: function() { click: function() {
$(this).attr("aria-expanded", !JSON.parse($(this).attr("aria-expanded"))); $(this).attr("aria-expanded", !JSON.parse($(this).attr("aria-expanded")));
$("#js-advanced-search").slideToggle(); $("#js-advanced-search").slideToggle();

View File

@@ -1,11 +1,11 @@
<div class="relative"> <div class="relative">
<button type="button" aria-expanded="<%= advanced_search.present? %>" id="js-advanced-search-title" class="advanced-search small"> <button type="button" aria-expanded="<%= advanced_search.present? %>" id="js-advanced-search-title" class="advanced-search small" hidden>
<%= t("shared.advanced_search.title") %> <%= t("shared.advanced_search.title") %>
</button> </button>
</div> </div>
<%= form_tag request.path, id: "advanced_search_form", class: "advanced-search-form", method: :get do %> <%= form_tag request.path, id: "advanced_search_form", class: "advanced-search-form", method: :get do %>
<div id="js-advanced-search" data-advanced-search-terms="<%= advanced_search.present? %>" style="display: none"> <div id="js-advanced-search" data-advanced-search-terms="<%= advanced_search.present? %>">
<div class="general-search"> <div class="general-search">
<label for="search"> <label for="search">
<%= t("shared.advanced_search.general") %> <%= t("shared.advanced_search.general") %>

View File

@@ -1,9 +1,23 @@
require "rails_helper" require "rails_helper"
describe Shared::AdvancedSearchComponent, type: :component do describe Shared::AdvancedSearchComponent, type: :component do
describe "SDG filter" do let(:component) { Shared::AdvancedSearchComponent.new }
let(:component) { Shared::AdvancedSearchComponent.new }
context "JavaScript disabled" do
it "renders the form" do
render_inline component
expect(page).to have_button "Filter"
end
it "hides the button to show the form" do
render_inline component
expect(page).to have_button "Advanced search", visible: :hidden
end
end
describe "SDG filter" do
before do before do
Setting["feature.sdg"] = true Setting["feature.sdg"] = true
Setting["sdg.process.proposals"] = true Setting["sdg.process.proposals"] = true