From b5842fa74f73e349b3309c9f166deaa8d07d0486 Mon Sep 17 00:00:00 2001 From: Jose Farias Date: Sat, 30 Mar 2024 14:06:51 -0600 Subject: [PATCH 1/3] Allow preselecting a grouped option --- app/presenters/hotwire_combobox/listbox/group.rb | 4 +++- app/presenters/hotwire_combobox/listbox/item.rb | 2 +- .../hotwire_combobox/listbox/item/collection.rb | 14 ++++++++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 app/presenters/hotwire_combobox/listbox/item/collection.rb diff --git a/app/presenters/hotwire_combobox/listbox/group.rb b/app/presenters/hotwire_combobox/listbox/group.rb index bc4fbd30..a614f992 100644 --- a/app/presenters/hotwire_combobox/listbox/group.rb +++ b/app/presenters/hotwire_combobox/listbox/group.rb @@ -1,6 +1,8 @@ require "securerandom" class HotwireCombobox::Listbox::Group + attr_reader :options + def initialize(name, options:) @name = name @options = options @@ -17,7 +19,7 @@ def render_in(view) end private - attr_reader :name, :options + attr_reader :name def id @id ||= SecureRandom.uuid diff --git a/app/presenters/hotwire_combobox/listbox/item.rb b/app/presenters/hotwire_combobox/listbox/item.rb index 808ab01e..2cfe30c0 100644 --- a/app/presenters/hotwire_combobox/listbox/item.rb +++ b/app/presenters/hotwire_combobox/listbox/item.rb @@ -16,7 +16,7 @@ def initialize(view, options, render_in:, include_blank:, **custom_methods) def items items = groups_or_options items.unshift(blank_option) if include_blank.present? - items + Collection.new items end private diff --git a/app/presenters/hotwire_combobox/listbox/item/collection.rb b/app/presenters/hotwire_combobox/listbox/item/collection.rb new file mode 100644 index 00000000..396569d4 --- /dev/null +++ b/app/presenters/hotwire_combobox/listbox/item/collection.rb @@ -0,0 +1,14 @@ +class HotwireCombobox::Listbox::Item::Collection < Array + def find(&block) + if grouped? + flat_map { |item| item.options }.find(&block) + else + super(&block) + end + end + + private + def grouped? + first.is_a? HotwireCombobox::Listbox::Group + end +end From 870b8669af771adf1dfcde4e3b3bc8bcf7ac5c24 Mon Sep 17 00:00:00 2001 From: Jose Farias Date: Sat, 30 Mar 2024 23:07:00 -0600 Subject: [PATCH 2/3] Add test and refactor --- app/presenters/hotwire_combobox/component.rb | 2 +- .../hotwire_combobox/listbox/item.rb | 8 ++++++-- .../listbox/item/collection.rb | 6 +++--- lib/hotwire_combobox/helper.rb | 18 +++++++----------- .../views/comboboxes/grouped_options.html.erb | 1 + test/system/hotwire_combobox_test.rb | 4 ++++ 6 files changed, 22 insertions(+), 17 deletions(-) diff --git a/app/presenters/hotwire_combobox/component.rb b/app/presenters/hotwire_combobox/component.rb index e209445e..98a74dd5 100644 --- a/app/presenters/hotwire_combobox/component.rb +++ b/app/presenters/hotwire_combobox/component.rb @@ -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 diff --git a/app/presenters/hotwire_combobox/listbox/item.rb b/app/presenters/hotwire_combobox/listbox/item.rb index 2cfe30c0..bb7f803f 100644 --- a/app/presenters/hotwire_combobox/listbox/item.rb +++ b/app/presenters/hotwire_combobox/listbox/item.rb @@ -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 diff --git a/app/presenters/hotwire_combobox/listbox/item/collection.rb b/app/presenters/hotwire_combobox/listbox/item/collection.rb index 396569d4..9357e2a3 100644 --- a/app/presenters/hotwire_combobox/listbox/item/collection.rb +++ b/app/presenters/hotwire_combobox/listbox/item/collection.rb @@ -1,9 +1,9 @@ class HotwireCombobox::Listbox::Item::Collection < Array - def find(&block) + def find_by_value(value) if grouped? - flat_map { |item| item.options }.find(&block) + flat_map { |item| item.options }.find { |option| option.value == value } else - super(&block) + find { |option| option.value == value } end end diff --git a/lib/hotwire_combobox/helper.rb b/lib/hotwire_combobox/helper.rb index 70ed4979..d0400e42 100644 --- a/lib/hotwire_combobox/helper.rb +++ b/lib/hotwire_combobox/helper.rb @@ -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( @@ -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 diff --git a/test/dummy/app/views/comboboxes/grouped_options.html.erb b/test/dummy/app/views/comboboxes/grouped_options.html.erb index 143e6865..7e7b79de 100644 --- a/test/dummy/app/views/comboboxes/grouped_options.html.erb +++ b/test/dummy/app/views/comboboxes/grouped_options.html.erb @@ -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 %> diff --git a/test/system/hotwire_combobox_test.rb b/test/system/hotwire_combobox_test.rb index e8898a21..4e52301c 100644 --- a/test/system/hotwire_combobox_test.rb +++ b/test/system/hotwire_combobox_test.rb @@ -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 From 3a8846b79afd2e6e06d235ca3ee1618c40813491 Mon Sep 17 00:00:00 2001 From: Jose Farias Date: Sun, 31 Mar 2024 09:05:32 -0600 Subject: [PATCH 3/3] items -> collection --- app/presenters/hotwire_combobox/listbox/item.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/presenters/hotwire_combobox/listbox/item.rb b/app/presenters/hotwire_combobox/listbox/item.rb index bb7f803f..89a1cd4f 100644 --- a/app/presenters/hotwire_combobox/listbox/item.rb +++ b/app/presenters/hotwire_combobox/listbox/item.rb @@ -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 @@ -13,7 +13,7 @@ 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? Collection.new items @@ -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