Skip to content

Commit f853b94

Browse files
committed
Limit autocomplete to keyword searches
Closes #864
1 parent 8649538 commit f853b94

File tree

4 files changed

+60
-0
lines changed

4 files changed

+60
-0
lines changed

app/javascript/application.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@ import "controllers"
44
import bootstrap from "bootstrap"
55
import githubAutoCompleteElement from "@github/auto-complete-element"
66
import Blacklight from "blacklight"
7+
import initializeAutocompleteToggle from "autocomplete_toggle"
78

89
import "arclight"
910

1011
Blacklight.onLoad(() => {
1112
// Initialize Bootstrap tooltips
1213
const tooltipTriggerList = document.querySelectorAll('[data-bs-toggle="tooltip"]')
1314
const tooltipList = [...tooltipTriggerList].map(tooltipTriggerEl => new bootstrap.Tooltip(tooltipTriggerEl))
15+
16+
initializeAutocompleteToggle();
1417
})
1518
import BlacklightRangeLimit from "blacklight-range-limit";
1619
BlacklightRangeLimit.init({onLoadHandler: Blacklight.onLoad });
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
export default function initializeAutocompleteToggle() {
2+
const searchField = document.querySelector('#search_field');
3+
const autocomplete = document.querySelector('.search-autocomplete-wrapper');
4+
const autocompleteSrc = autocomplete?.src;
5+
6+
// Update the autocomplete display based on the selected search field value
7+
function updateAutocompleteDisplay() {
8+
const selectedSearchField = searchField.value;
9+
10+
if (selectedSearchField === "keyword") {
11+
autocomplete.setAttribute("src", autocompleteSrc)
12+
} else {
13+
// Remove the src attribute to hide the autocomplete
14+
autocomplete.removeAttribute("src");
15+
}
16+
}
17+
18+
if (searchField && autocomplete) {
19+
searchField.addEventListener('change', updateAutocompleteDisplay);
20+
}
21+
}

config/importmap.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
pin '@hotwired/stimulus', to: 'stimulus.min.js', preload: true
88
pin '@hotwired/stimulus-loading', to: 'stimulus-loading.js', preload: true
99
pin_all_from 'app/javascript/controllers', under: 'controllers'
10+
pin 'autocomplete_toggle', to: 'autocomplete_toggle.js'
1011
pin '@github/auto-complete-element', to: 'https://cdn.skypack.dev/@github/auto-complete-element'
1112
pin '@popperjs/core', to: 'https://ga.jspm.io/npm:@popperjs/core@2.11.8/dist/umd/popper.min.js'
1213
pin 'bootstrap', to: 'https://ga.jspm.io/npm:bootstrap@5.3.3/dist/js/bootstrap.js'

spec/features/search_spec.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,39 @@
4747
expect(page.current_url).to include('group=true')
4848
end
4949
end
50+
51+
context 'with keyword search' do
52+
it 'displays the autocomplete dropdown suggetsions' do
53+
visit search_catalog_path
54+
select('Keyword', from: 'search_field')
55+
fill_in 'q', with: 'france'
56+
57+
# use xpath because <auto-complete> is a custom web component that capybara can't otherwise find
58+
src_value = find(:xpath, './/auto-complete')[:src]
59+
expect(src_value).to eq('/catalog/suggest')
60+
61+
expect(page).to have_css('#autocomplete-popup', visible: :visible)
62+
end
63+
end
64+
65+
context 'with a non-keyword search' do
66+
it 'can toggle the autocomplete functionality from off to on' do
67+
visit search_catalog_path
68+
select('Place', from: 'search_field')
69+
fill_in 'q', with: 'france'
70+
71+
# use xpath because <auto-complete> is a custom web component that capybara can't otherwise find
72+
src_value = find(:xpath, './/auto-complete')[:src]
73+
expect(src_value).to be_empty
74+
expect(page).to have_css('#autocomplete-popup', visible: :hidden)
75+
76+
# Autocomplete should work again
77+
select('Keyword', from: 'search_field')
78+
fill_in 'q', with: 'france'
79+
80+
src_value = find(:xpath, './/auto-complete')[:src]
81+
expect(src_value).to eq('/catalog/suggest')
82+
expect(page).to have_css('#autocomplete-popup', visible: :visible)
83+
end
84+
end
5085
end

0 commit comments

Comments
 (0)