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

Reconnect after element morph #132

Merged
merged 1 commit into from
Mar 30, 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
4 changes: 4 additions & 0 deletions app/assets/javascripts/controllers/hw_combobox_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ export default class HwComboboxController extends Concerns(...concerns) {
}

connect() {
this.idempotentConnect()
}

idempotentConnect() {
this._connectSelection()
this._connectListAutocomplete()
this._connectDialog()
Expand Down
3 changes: 2 additions & 1 deletion app/presenters/hotwire_combobox/component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,8 @@ def input_data
click@window->hw-combobox#closeOnClickOutside
focusin@window->hw-combobox#closeOnFocusOutside
turbo:before-stream-render@document->hw-combobox#rerouteListboxStreamToDialog
turbo:before-cache@document->hw-combobox#hideChipsForCache".squish,
turbo:before-cache@document->hw-combobox#hideChipsForCache
turbo:morph-element->hw-combobox#idempotentConnect".squish,
hw_combobox_target: "combobox",
async_id: canonical_id
end
Expand Down
4 changes: 4 additions & 0 deletions test/dummy/app/controllers/comboboxes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ def multiselect_new_values
def grouped_options
end

def morph
@user = User.where.not(home_state: nil).first || raise("No user found with home state, load fixtures first.")
end

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

Expand Down
2 changes: 1 addition & 1 deletion test/dummy/app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ def user_params
end

def visited_state_ids
params[:user][:visited_state_ids].split(",")
params[:user][:visited_state_ids]&.split(",") || []
end
end
12 changes: 12 additions & 0 deletions test/dummy/app/views/comboboxes/morph.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<%= content_for :head do %>
<meta name="turbo-refresh-method" content="morph">
<% end %>

<% if flash[:notice] %>
<p><%= flash[:notice] %></p>
<% end %>

<%= form_with model: @user do |f| %>
<%= f.combobox :home_state_id, State.all %>
<%= f.submit %>
<% end %>
2 changes: 2 additions & 0 deletions test/dummy/app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
<%= combobox_style_tag %>
<%= javascript_importmap_tags %>

<%= yield :head %>

<style>
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;700&display=swap');

Expand Down
1 change: 1 addition & 0 deletions test/dummy/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
get "multiselect_custom_events", to: "comboboxes#multiselect_custom_events"
get "multiselect_new_values", to: "comboboxes#multiselect_new_values"
get "grouped_options", to: "comboboxes#grouped_options"
get "morph", to: "comboboxes#morph"

resources :movies, only: %i[ index update ]
get "movies_html", to: "movies#index_html"
Expand Down
19 changes: 19 additions & 0 deletions test/system/hotwire_combobox_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1116,6 +1116,25 @@ class HotwireComboboxTest < ApplicationSystemTestCase
assert_selected_option_with text: "Alabama"
end

test "preselected morph" do
visit morph_path

user = User.where.not(home_state: nil).first
new_state = State.all.without(user.home_state).first

assert_combobox_display_and_value "#user_home_state_id", user.home_state.name, user.home_state.id

open_combobox "#user_home_state_id"
click_on_option new_state.name
assert_combobox_display_and_value "#user_home_state_id", new_state.name, new_state.id

find("input[type=submit]").click

assert_text "User updated"

assert_combobox_display_and_value "#user_home_state_id", new_state.name, new_state.id
end

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