Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow preselecting a grouped option #133

Merged
merged 3 commits into from
Mar 31, 2024
Merged
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
2 changes: 1 addition & 1 deletion app/presenters/hotwire_combobox/component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ def prefilled_display
if async_src && associated_object
associated_object.to_combobox_display
elsif hidden_field_value
options.find { |option| option.value == hidden_field_value }&.autocompletable_as
options.find_by_value(hidden_field_value)&.autocompletable_as
end
end

Expand Down
4 changes: 3 additions & 1 deletion app/presenters/hotwire_combobox/listbox/group.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
require "securerandom"

class HotwireCombobox::Listbox::Group
attr_reader :options

def initialize(name, options:)
@name = name
@options = options
Expand All @@ -17,7 +19,7 @@ def render_in(view)
end

private
attr_reader :name, :options
attr_reader :name

def id
@id ||= SecureRandom.uuid
Expand Down
16 changes: 10 additions & 6 deletions app/presenters/hotwire_combobox/listbox/item.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class HotwireCombobox::Listbox::Item
class << self
def collection_for(view, options, render_in:, include_blank:, **custom_methods)
new(view, options, render_in: render_in, include_blank: include_blank, **custom_methods).items
new(view, options, render_in: render_in, include_blank: include_blank, **custom_methods).collection
end
end

Expand All @@ -13,10 +13,10 @@ def initialize(view, options, render_in:, include_blank:, **custom_methods)
@custom_methods = custom_methods
end

def items
def collection
items = groups_or_options
items.unshift(blank_option) if include_blank.present?
items
Collection.new items
end

private
Expand All @@ -31,7 +31,7 @@ def groups_or_options
end

def grouped?
key, value = options.to_a.first
_key, value = options.to_a.first
value.is_a? Array
end

Expand All @@ -43,8 +43,12 @@ def create_listbox_group(options)
end

def create_listbox_options(options)
options.map do |option|
HotwireCombobox::Listbox::Option.new **option_attrs(option)
if options.first.is_a? HotwireCombobox::Listbox::Option
options
else
options.map do |option|
HotwireCombobox::Listbox::Option.new **option_attrs(option)
end
end
end

Expand Down
14 changes: 14 additions & 0 deletions app/presenters/hotwire_combobox/listbox/item/collection.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class HotwireCombobox::Listbox::Item::Collection < Array
def find_by_value(value)
if grouped?
flat_map { |item| item.options }.find { |option| option.value == value }
else
find { |option| option.value == value }
end
end

private
def grouped?
first.is_a? HotwireCombobox::Listbox::Group
end
end
18 changes: 7 additions & 11 deletions lib/hotwire_combobox/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,12 @@ def hw_combobox_options(
include_blank: nil,
display: :to_combobox_display,
**custom_methods)
if options.first.is_a? HotwireCombobox::Listbox::Option
options
else
HotwireCombobox::Listbox::Item.collection_for \
self,
options,
render_in: render_in,
include_blank: include_blank,
**custom_methods.merge(display: display)
end
HotwireCombobox::Listbox::Item.collection_for \
self,
options,
render_in: render_in,
include_blank: include_blank,
**custom_methods.merge(display: display)
end

def hw_paginated_combobox_options(
Expand Down Expand Up @@ -184,7 +180,7 @@ def hw_call_method_or_proc(object, method_or_proc)
private
def hw_extract_options_and_src(options_or_src, render_in, include_blank)
if options_or_src.is_a? String
[ [], options_or_src ]
[ hw_combobox_options([]), options_or_src ]
else
[ hw_combobox_options(options_or_src, render_in: render_in, include_blank: include_blank), nil ]
end
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 @@ -14,3 +14,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" %>
<%= combobox_tag "preselected", State.all.group_by(&:location), id: "preselected", value: State.first.id %>
4 changes: 4 additions & 0 deletions test/system/hotwire_combobox_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1114,6 +1114,10 @@ class HotwireComboboxTest < ApplicationSystemTestCase
assert_selected_option_with text: "All"
type_in_combobox "#state-field-with-blank", :down
assert_selected_option_with text: "Alabama"

assert_combobox_display_and_value "#preselected", "Alabama", states(:alabama).id
open_combobox "#preselected"
assert_selected_option_with text: "Alabama"
end

test "preselected morph" do
Expand Down