Skip to content

Commit

Permalink
Fix interactivity of listbox options when combobox is in a dialog (#197)
Browse files Browse the repository at this point in the history
Co-authored-by: Jose Farias <[email protected]>
  • Loading branch information
JoshAntBrown and josefarias authored Aug 31, 2024
1 parent b07617e commit 79302f8
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/presenters/hotwire_combobox/listbox/option.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def options
{
id: id,
role: :option,
tabindex: "-1",
class: [ "hw-combobox__option", { "hw-combobox__option--blank": blank? } ],
data: data,
aria: aria
Expand Down
3 changes: 3 additions & 0 deletions test/dummy/app/controllers/comboboxes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ def external_clear
@user = User.first || raise("No user found, load fixtures first.")
end

def dialog
end

private
delegate :combobox_options, :html_combobox_options, to: "ApplicationController.helpers", private: true

Expand Down
9 changes: 9 additions & 0 deletions test/dummy/app/javascript/controllers/dialog_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
static targets = [ "dialog" ]

open() {
this.dialogTarget.showModal()
}
}
16 changes: 16 additions & 0 deletions test/dummy/app/views/comboboxes/dialog.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<%= tag.style nonce: content_security_policy_nonce do %>
dialog {
height: 80vh;
width: 80vw;
}
<% end %>

<article data-controller="dialog">
<dialog data-dialog-target="dialog">
<%= form_with model: Movie.new do |form| %>
<%= form.combobox :rating, Movie.ratings %>
<% end %>
</dialog>

<button data-action="dialog#open">Show modal</button>
</article>
1 change: 1 addition & 0 deletions test/dummy/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
get "morph", to: "comboboxes#morph"
get "form_object", to: "comboboxes#form_object"
get "external_clear", to: "comboboxes#external_clear"
get "dialog", to: "comboboxes#dialog"

resources :movies, only: %i[ index update ]
get "movies_html", to: "movies#index_html"
Expand Down
10 changes: 10 additions & 0 deletions test/system/hotwire_combobox_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1332,6 +1332,16 @@ class HotwireComboboxTest < ApplicationSystemTestCase
page.evaluate_script("document.activeElement.id") == "external_clear"
end

test "selecting options within a dialog" do
visit dialog_path

click_on "Show modal"

open_combobox "#movie_rating"
click_on_option "R"
assert_combobox_display_and_value "#movie_rating", "R", Movie.ratings[:R]
end

private
def open_combobox(selector)
find(selector).click
Expand Down

0 comments on commit 79302f8

Please sign in to comment.