Skip to content

Commit

Permalink
Support include_blank
Browse files Browse the repository at this point in the history
  • Loading branch information
josefarias committed Mar 21, 2024
1 parent 6d03a02 commit e68dc01
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
18 changes: 11 additions & 7 deletions app/presenters/hotwire_combobox/listbox/item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,22 @@ def initialize(view, options, render_in, include_blank:, **custom_methods)
end

def items
if grouped?
create_listbox_group options
else
items = create_listbox_options options
items.unshift(blank_option) if include_blank.present?
items
end
items = groups_or_options
items.unshift(blank_option) if include_blank.present?
items
end

private
attr_reader :view, :options, :render_in, :include_blank, :custom_methods

def groups_or_options
if grouped?
create_listbox_group options
else
create_listbox_options options
end
end

def grouped?
key, value = options.to_a.first
value.is_a? Array
Expand Down
1 change: 1 addition & 0 deletions test/dummy/app/views/comboboxes/grouped_options.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@

<%= combobox_tag "state", State.all.group_by(&:location), id: "state-field" %>
<%= combobox_tag "state_but_weird", weird_options, id: "weird-state-field" %>
<%= combobox_tag "state_with_blank", State.all.group_by(&:location), id: "state-field-with-blank", include_blank: "All" %>
10 changes: 10 additions & 0 deletions test/system/hotwire_combobox_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1085,6 +1085,16 @@ class HotwireComboboxTest < ApplicationSystemTestCase

type_in_combobox "#weird-state-field", :down
assert_selected_option_with text: "United States"

open_combobox "#state-field-with-blank"
assert_option_with class: ".hw-combobox__option--blank", text: "All"
assert_group_with text: "South"
assert_option_with text: "Alabama"

type_in_combobox "#state-field-with-blank", :down
assert_selected_option_with text: "All"
type_in_combobox "#state-field-with-blank", :down
assert_selected_option_with text: "Alabama"
end

private
Expand Down

0 comments on commit e68dc01

Please sign in to comment.