Skip to content

Commit

Permalink
Use provided id for combobox, not hidden field (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
josefarias authored Feb 7, 2024
1 parent eb04a03 commit 7ca2551
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 85 deletions.
3 changes: 3 additions & 0 deletions .github/release.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
changelog:
categories:
- title: Breaking Changes 🛠
labels:
- breaking
- title: 📦 Features
labels:
- '*'
Expand Down
21 changes: 13 additions & 8 deletions app/presenters/hotwire_combobox/component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def paginated?
end

def pagination_attrs
{ for_id: hidden_field_id, src: async_src }
{ for_id: canonical_id, src: async_src }
end

private
Expand All @@ -153,7 +153,7 @@ def infer_association_name

def fieldset_data
data.reverse_merge \
async_id: hidden_field_id,
async_id: canonical_id,
controller: view.token_list("hw-combobox", data[:controller]),
hw_combobox_expanded_value: open,
hw_combobox_name_when_new_value: name_when_new,
Expand Down Expand Up @@ -186,10 +186,15 @@ def association_exists?
end


def hidden_field_id
def canonical_id
id || form&.field_id(name)
end


def hidden_field_id
"#{canonical_id}-hw-hidden-field"
end

def hidden_field_name
form&.field_name(name) || name
end
Expand All @@ -204,7 +209,7 @@ def hidden_field_value


def input_id
"#{hidden_field_id}-hw-combobox"
canonical_id
end

def input_type
Expand All @@ -220,7 +225,7 @@ def input_data
click@window->hw-combobox#closeOnClickOutside
focusin@window->hw-combobox#closeOnFocusOutside".squish,
hw_combobox_target: "combobox",
async_id: hidden_field_id
async_id: canonical_id
end

def input_aria
Expand All @@ -241,7 +246,7 @@ def handle_data


def listbox_id
"#{hidden_field_id}-hw-listbox"
"#{canonical_id}-hw-listbox"
end

def listbox_data
Expand All @@ -262,7 +267,7 @@ def dialog_data
end

def dialog_input_id
"#{hidden_field_id}-hw-dialog-combobox"
"#{canonical_id}-hw-dialog-combobox"
end

def dialog_input_data
Expand All @@ -284,7 +289,7 @@ def dialog_input_aria
end

def dialog_listbox_id
"#{hidden_field_id}-hw-dialog-listbox"
"#{canonical_id}-hw-dialog-listbox"
end

def dialog_listbox_data
Expand Down
4 changes: 2 additions & 2 deletions test/dummy/app/views/comboboxes/new_options.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
<% end %>
<%= form_with model: User.new, url: new_options_form_path, method: :post do |form| %>
<%= form.label "Favorite State", for: "allow-new-hw-combobox" %>
<%= form.label "Favorite State", for: "allow-new" %>
<%= form.combobox :favorite_state_id, State.all, id: "allow-new", name_when_new: "user[favorite_state_attributes][name]" %>
<%= form.label "Home State", for: "disallow-new-hw-combobox" %>
<%= form.label "Home State", for: "disallow-new" %>
<%= form.combobox :home_state_id, State.all, id: "disallow-new" %>
<%= form.submit %>
Expand Down
3 changes: 2 additions & 1 deletion test/helpers/hotwire_combobox/helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ class HotwireCombobox::HelperTest < ApplicationViewTestCase
form = ActionView::Helpers::FormBuilder.new :foo, nil, self, {}
tag = combobox_tag :bar, form: form

assert_attrs tag, name: "foo[bar]", id: "foo_bar" # name is not "bar"
assert_attrs tag, type: "hidden", name: "foo[bar]" # name is not "bar"
assert_attrs tag, id: "foo_bar", role: "combobox" # id is determined by the form builder
end

test "passing a form builder object overrides value" do
Expand Down
Loading

0 comments on commit 7ca2551

Please sign in to comment.