Skip to content

Commit de2397d

Browse files
Add a no results item to autocomplete (#3321)
1 parent 9b9397d commit de2397d

File tree

11 files changed

+762
-130
lines changed

11 files changed

+762
-130
lines changed

.changeset/thick-mayflies-do.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@primer/view-components': minor
3+
---
4+
5+
[Primer::Beta::Autocomplete] Added a new component to render inside the the Autocomplete dropdown: Primer::Beta::AutoComplete::NoResultItem. This new component can be used to display a message to indicate that there are no available results. This component is marked upas role='presentation' as the autocomplete component already uses aria-live to announce if there are no results.

app/components/primer/beta/auto_complete/item.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,13 @@ class Item < Primer::Component
4545
# @param description_variant [Hash] Changes the description style. Allowed values are :inline, :block
4646
# @param description [String] Display description text below label
4747
# @param system_arguments [Hash] <%= link_to_system_arguments_docs %>
48-
def initialize(value:, selected: false, disabled: false, description_variant: DEFAULT_DESCRIPTION_VARIANT, **system_arguments)
48+
def initialize(value:, selected: false, disabled: false, description_variant: DEFAULT_DESCRIPTION_VARIANT, role: :option, **system_arguments)
4949
@description_variant = fetch_or_fallback(
5050
DESCRIPTION_VARIANT_OPTIONS, description_variant, DEFAULT_DESCRIPTION_VARIANT
5151
)
52-
5352
@system_arguments = deny_tag_argument(**system_arguments)
5453
@system_arguments[:tag] = :div
55-
@system_arguments[:role] = :option
54+
@system_arguments[:role] = role
5655
@system_arguments[:"data-autocomplete-value"] = value
5756

5857
@system_arguments[:"aria-selected"] = true if selected
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module Primer
2+
module Beta
3+
class AutoComplete
4+
class NoResultItem < Item
5+
def initialize(**system_arguments)
6+
super(
7+
role: :presentation,
8+
aria: merge_aria(
9+
{ aria: { hidden: true } },
10+
system_arguments
11+
),
12+
value: "",
13+
disabled: true,
14+
'data-no-result-found': true,
15+
**system_arguments
16+
)
17+
end
18+
end
19+
end
20+
end
21+
end

demo/app/controllers/auto_complete_test_controller.rb

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
class AutoCompleteTestController < ApplicationController
55
layout false
66

7-
def index
8-
@fruit_list = [
7+
FRUIT = [
98
"Apples",
109
"Apricots",
1110
"Avocado",
@@ -32,10 +31,21 @@ def index
3231
"Grapes",
3332
"Grapefruit",
3433
"Guava"
35-
].select { |fruit| fruit.downcase.include?(params["q"].downcase) }
34+
]
35+
36+
def index
37+
@fruit_list = FRUIT.select { |fruit| fruit.downcase.include?(params["q"].downcase) }
3638
@visual_type = params[:visual]
3739
@version = params[:version]
3840

3941
render :index, formats: [:html, :html_fragment]
4042
end
43+
44+
def no_results
45+
@fruit_list = FRUIT.select { |fruit| fruit.downcase.include?(params["q"].downcase) }
46+
@visual_type = params[:visual]
47+
@version = params[:version]
48+
49+
render :no_results, formats: [:html, :html_fragment]
50+
end
4151
end
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<% if @fruit_list.length > 0 %>
2+
<% @fruit_list.each do |fruit| %>
3+
<%= render(Primer::Beta::AutoComplete::Item.new(value: fruit)) do |component| %>
4+
<% if @visual_type == "leading" %>
5+
<% component.with_leading_visual_icon(icon: :"mark-github") %>
6+
<% elsif @visual_type == "trailing" %>
7+
<% component.with_trailing_visual_icon(icon: :"mark-github") %>
8+
<% end %>
9+
<%= fruit %>
10+
<% end %>
11+
<% end %>
12+
<% else %>
13+
<%= render(Primer::Beta::AutoComplete::NoResultItem.new) do %>
14+
No results!
15+
<% end %>
16+
<% end %>

demo/config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
get "/healthz", to: "health#index"
1919

2020
get "/auto_complete", to: "auto_complete_test#index", as: :autocomplete_index
21+
get "/auto_complete_no_results", to: "auto_complete_test#no_results", as: :autocomplete_no_results
2122

2223
resources :toggle_switch, only: [:create]
2324
resources :nav_list_items, only: [:index]

0 commit comments

Comments
 (0)