Skip to content

Commit

Permalink
Replace twitter-typeahead-rails with @github/auto-complete-element
Browse files Browse the repository at this point in the history
This is less code for us to manage and is a library that is currently receiving maintenance
  • Loading branch information
jcoyne committed Dec 6, 2021
1 parent b110de0 commit 76e8833
Show file tree
Hide file tree
Showing 16 changed files with 69 additions and 116 deletions.
1 change: 1 addition & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ RSpec/DescribeClass:
Exclude:
- 'spec/lib/tasks/blacklight_task_spec.rb'
- 'spec/features/**/*'
- 'spec/requests/**/*'
- 'spec/views/**/*'
RSpec/ExcessiveDocstringSpacing: # new in 2.5
Enabled: true
Expand Down
48 changes: 4 additions & 44 deletions app/assets/javascripts/blacklight/blacklight.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 1 addition & 5 deletions app/assets/javascripts/blacklight/blacklight.js.map

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions app/assets/stylesheets/blacklight/_autocomplete.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
auto-complete [aria-selected="true"],
auto-complete [role="option"]:hover {
background-color: $dropdown-link-hover-bg;
}
auto-complete {
position: relative;
flex-grow: 3;

ul {
list-style-type: none;
position: absolute;
background-color: $dropdown-bg;
padding-left: 0;
border: 1px solid $dropdown-border-color;
border-radius: $dropdown-border-radius;
}

[role="option"]{
padding: 0.25rem 1.5rem;
}
}
1 change: 1 addition & 0 deletions app/assets/stylesheets/blacklight/_blacklight_base.scss
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@
@import "modal";
@import "twitter_typeahead";
@import "icons";
@import "autocomplete";
15 changes: 14 additions & 1 deletion app/components/blacklight/search_bar_component.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,20 @@
<% end %>

<label for="<%= @prefix %><%= @query_param %>" class="sr-only visually-hidden"><%= scoped_t('search.label') %></label>
<%= text_field_tag @query_param, @q, placeholder: scoped_t('search.placeholder'), class: "search-q q form-control rounded-#{@search_fields.length > 1 ? '0' : 'left'}", id: "#{@prefix}q", autocomplete: autocomplete_path.present? ? "off" : "", autofocus: @autofocus, data: { autocomplete_enabled: autocomplete_path.present?, autocomplete_path: autocomplete_path } %>

<% if autocomplete_path.present? %>
<auto-complete src="<%= autocomplete_path %>" for="autocomplete-popup">
<%= text_field_tag @query_param, @q, placeholder: scoped_t('search.placeholder'),
class: "search-q q form-control rounded-#{@search_fields.length > 1 ? '0' : 'left'}",
id: "#{@prefix}q", autofocus: @autofocus %>

<ul id="autocomplete-popup"></ul>
</auto-complete>
<% else %>
<%= text_field_tag @query_param, @q, placeholder: scoped_t('search.placeholder'),
class: "search-q q form-control rounded-#{@search_fields.length > 1 ? '0' : 'left'}",
id: "#{@prefix}q", autofocus: @autofocus %>
<% end %>

<span class="input-group-append">
<%= append %>
Expand Down
8 changes: 3 additions & 5 deletions app/controllers/concerns/blacklight/catalog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,10 @@ def opensearch
end
end

# Returns the dropdown list for autocomplete
def suggest
respond_to do |format|
format.json do
render json: suggestions_service.suggestions
end
end
@suggestions = suggestions_service.suggestions
render 'suggest', layout: false
end

# @return [Array] first value is a Blacklight::Solr::Response and the second
Expand Down
40 changes: 0 additions & 40 deletions app/javascript/blacklight/autocomplete.js

This file was deleted.

2 changes: 0 additions & 2 deletions app/javascript/blacklight/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import Autocomplete from './autocomplete'
import BookmarkToggle from './bookmark_toggle'
import ButtonFocus from './button_focus'
import FacetLoad from './facet_load'
Expand All @@ -7,7 +6,6 @@ import SearchContext from './search_context'
import Core from './core'

export default {
Autocomplete,
BookmarkToggle,
ButtonFocus,
FacetLoad,
Expand Down
3 changes: 3 additions & 0 deletions app/views/catalog/suggest.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<% @suggestions.each do |suggestion| %>
<li role="option"><span><%= suggestion['term'] %></span></li>
<% end %>
3 changes: 3 additions & 0 deletions app/views/layouts/blacklight/base.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
<%= content_for(:head) %>
<script type="module">
import githubAutoCompleteElement from 'https://cdn.skypack.dev/@github/auto-complete-element';
</script>
</head>
<body class="<%= render_body_class %>">
<nav id="skip-link" role="navigation" aria-label="<%= t('blacklight.skip_links.label') %>">
Expand Down
2 changes: 1 addition & 1 deletion lib/blacklight/routes/searchable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def call(mapper, _options = {})
mapper.get ":id/raw", action: 'raw', as: 'raw', defaults: { format: 'json' }

mapper.get "opensearch"
mapper.get 'suggest', as: 'suggest_index', defaults: { format: 'json' }
mapper.get 'suggest', as: 'suggest_index'
mapper.get "facet/:id", action: 'facet', as: 'facet'
end
end
Expand Down
3 changes: 0 additions & 3 deletions lib/generators/blacklight/assets_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ class Assets < Rails::Generators::Base
# This could be skipped if you want to use webpacker
def add_javascript_dependencies
gem 'bootstrap', options[:'bootstrap-version']
gem 'twitter-typeahead-rails', '0.11.1.pre.corejavascript'
gem 'jquery-rails'
end

Expand Down Expand Up @@ -37,8 +36,6 @@ def assets
//
// Required by Blacklight
//= require popper
// Twitter Typeahead for autocomplete
//= require twitter/typeahead
//= require bootstrap
//= require blacklight/blacklight
CONTENT
Expand Down
14 changes: 0 additions & 14 deletions spec/controllers/catalog_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -467,20 +467,6 @@ def export_as_mock
end
end

describe 'GET suggest' do
it 'returns JSON' do
get :suggest, params: { format: 'json' }
expect(response.body).to eq [].to_json
end

it 'returns suggestions' do
get :suggest, params: { format: 'json', q: 'new' }
json = JSON.parse(response.body)
expect(json.count).to eq 5
expect(json.first['term']).to eq 'new jersey'
end
end

describe "email/sms" do
before do
mock_document.extend(Blacklight::Document::Sms)
Expand Down
2 changes: 1 addition & 1 deletion spec/features/autocomplete_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
RSpec.describe 'Autocomplete suggestions' do
it 'search form has suggest path' do
visit root_path
expect(page).to have_css 'input[data-autocomplete-path="/catalog/suggest"]'
expect(page).to have_css 'auto-complete[src="/catalog/suggest"]'
end
end
16 changes: 16 additions & 0 deletions spec/requests/load_suggestions_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'GET /catalog/suggest' do
it 'returns suggestions' do
get '/catalog/suggest?q=new'
expect(response.body).to eq <<-RESULT
<li role="option"><span>new jersey</span></li>
<li role="option"><span>new jersey bridgeton biography</span></li>
<li role="option"><span>new jersey bridgeton history</span></li>
<li role="option"><span>new york</span></li>
<li role="option"><span>nuwākshūṭ</span></li>
RESULT
end
end

0 comments on commit 76e8833

Please sign in to comment.