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
3 changes: 3 additions & 0 deletions app/javascript/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ import "controllers"
import bootstrap from "bootstrap"
import githubAutoCompleteElement from "@github/auto-complete-element"
import Blacklight from "blacklight"
import initializeAutocompleteToggle from "autocomplete_toggle"

import "arclight"

Blacklight.onLoad(() => {
// Initialize Bootstrap tooltips
const tooltipTriggerList = document.querySelectorAll('[data-bs-toggle="tooltip"]')
const tooltipList = [...tooltipTriggerList].map(tooltipTriggerEl => new bootstrap.Tooltip(tooltipTriggerEl))

initializeAutocompleteToggle();
})
import BlacklightRangeLimit from "blacklight-range-limit";
BlacklightRangeLimit.init({onLoadHandler: Blacklight.onLoad });
21 changes: 21 additions & 0 deletions app/javascript/autocomplete_toggle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export default function initializeAutocompleteToggle() {
const searchField = document.querySelector('#search_field');
Copy link
Contributor

@jcoyne jcoyne Apr 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason you aren't using Stimulus for this? All of the other JS for this app uses Stimulus.

Copy link
Contributor Author

@marlo-longley marlo-longley Apr 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wrote this in Stimulus first. I needed to copy down the Blacklight template for the search bar in order to insert the Stimulus targets, controller, etc. into the markup. I got the feedback that this seemed messier than writing vanilla JS. With vanilla JS, there was no need to copy down the Blacklight component. So I rewrote it this way.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By making that choice, you are binding to selectors that are not guaranteed to be stable between versions of blacklight/arclight. This seems more dangerous to me, even though it might be less code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes sense, thanks for the feedback. The team decided put this to draft for now as we learn more about what the stakeholders actually want from this feature.

const autocomplete = document.querySelector('.search-autocomplete-wrapper');
const autocompleteSrc = autocomplete?.src;

// Update the autocomplete display based on the selected search field value
function updateAutocompleteDisplay() {
const selectedSearchField = searchField.value;

if (selectedSearchField === "keyword") {
autocomplete.setAttribute("src", autocompleteSrc)
} else {
// Remove the src attribute to hide the autocomplete
autocomplete.removeAttribute("src");
}
}

if (searchField && autocomplete) {
searchField.addEventListener('change', updateAutocompleteDisplay);
}
}
1 change: 1 addition & 0 deletions config/importmap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
pin '@hotwired/stimulus', to: 'stimulus.min.js', preload: true
pin '@hotwired/stimulus-loading', to: 'stimulus-loading.js', preload: true
pin_all_from 'app/javascript/controllers', under: 'controllers'
pin 'autocomplete_toggle', to: 'autocomplete_toggle.js'
pin '@github/auto-complete-element', to: 'https://cdn.skypack.dev/@github/auto-complete-element'
pin '@popperjs/core', to: 'https://ga.jspm.io/npm:@popperjs/core@2.11.8/dist/umd/popper.min.js'
pin 'bootstrap', to: 'https://ga.jspm.io/npm:bootstrap@5.3.3/dist/js/bootstrap.js'
Expand Down
35 changes: 35 additions & 0 deletions spec/features/search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,39 @@
expect(page.current_url).to include('group=true')
end
end

context 'with keyword search' do
it 'displays the autocomplete dropdown suggetsions' do
visit search_catalog_path
select('Keyword', from: 'search_field')
fill_in 'q', with: 'france'

# use xpath because <auto-complete> is a custom web component that capybara can't otherwise find
src_value = find(:xpath, './/auto-complete')[:src]
expect(src_value).to eq('/catalog/suggest')

expect(page).to have_css('#autocomplete-popup', visible: :visible)
end
end

context 'with a non-keyword search' do
it 'can toggle the autocomplete functionality from off to on' do
visit search_catalog_path
select('Place', from: 'search_field')
fill_in 'q', with: 'france'

# use xpath because <auto-complete> is a custom web component that capybara can't otherwise find
src_value = find(:xpath, './/auto-complete')[:src]
expect(src_value).to be_empty
expect(page).to have_css('#autocomplete-popup', visible: :hidden)

# Autocomplete should work again
select('Keyword', from: 'search_field')
fill_in 'q', with: 'france'

src_value = find(:xpath, './/auto-complete')[:src]
expect(src_value).to eq('/catalog/suggest')
expect(page).to have_css('#autocomplete-popup', visible: :visible)
end
end
end