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

Add ability to clear combobox programmatically #193

Merged
merged 1 commit into from
Jul 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
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ Combobox.Filtering = Base => class extends Base {
}
}

clear(event) {
this._clearQuery()
this.chipDismisserTargets.forEach(el => el.click())
if (event && !event.defaultPrevented) event.target.focus()
}

_initializeFiltering() {
this._debouncedFilterAsync = debounce(this._debouncedFilterAsync.bind(this))
}
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 @@ -102,6 +102,10 @@ def form_object
@object = Form.new
end

def external_clear
@user = User.first || raise("No user found, load fixtures first.")
end

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

Expand Down
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 outlets = [ "hw-combobox" ]

clear(event) {
this.hwComboboxOutlets.forEach(outlet => outlet.clear(event))
}
}
16 changes: 16 additions & 0 deletions test/dummy/app/views/comboboxes/external_clear.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<%= tag.style nonce: content_security_policy_nonce do %>
#external_clear:focus {
outline: 1px solid blue;
}
<% end %>

<%= button_tag "clear", id: :external_clear, data: {
controller: "custom-clear",
action: "custom-clear#clear",
custom_clear_hw_combobox_outlet: "[data-clearable]" } %>

<%= combobox_tag "state", @state_options, value: "MI", id: "state-field", data: { clearable: "" } %>

<%= form_with model: @user do |form| %>
<%= form.combobox :visited_state_ids, State.all, label: "Visited states", multiselect_chip_src: state_chips_path, data: { clearable: "" } %>
<% end %>
1 change: 1 addition & 0 deletions test/dummy/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
get "grouped_options", to: "comboboxes#grouped_options"
get "morph", to: "comboboxes#morph"
get "form_object", to: "comboboxes#form_object"
get "external_clear", to: "comboboxes#external_clear"

resources :movies, only: %i[ index update ]
get "movies_html", to: "movies#index_html"
Expand Down
15 changes: 15 additions & 0 deletions test/system/hotwire_combobox_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1306,6 +1306,21 @@ class HotwireComboboxTest < ApplicationSystemTestCase
assert_combobox_display_and_value "#form_state_id", "Alabama", states(:alabama).id
end

test "clearing programmatically" do
visit external_clear_path

assert_combobox_display_and_value "#state-field", "Michigan", "MI"
assert_combobox_display_and_value "#user_visited_state_ids",
%w[ Florida Illinois ], states(:florida, :illinois).pluck(:id)

find("#external_clear").click

assert_combobox_display_and_value "#state-field", "", nil
assert_combobox_display_and_value "#user_visited_state_ids", [], nil

page.evaluate_script("document.activeElement.id") == "external_clear"
end

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