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

Use POST request for creating multiselect chips #162

Merged
merged 2 commits into from
May 4, 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
@@ -1,6 +1,6 @@
import Combobox from "hw_combobox/models/combobox/base"
import { cancel, nextRepaint } from "hw_combobox/helpers"
import { get } from "hw_combobox/vendor/requestjs"
import { post } from "hw_combobox/vendor/requestjs"

Combobox.Multiselect = Base => class extends Base {
navigateChip(event) {
Expand Down Expand Up @@ -79,7 +79,7 @@ Combobox.Multiselect = Base => class extends Base {
}

async _requestChips(values) {
await get(this.selectionChipSrcValue, {
await post(this.selectionChipSrcValue, {
responseKind: "turbo-stream",
query: {
for_id: this.element.dataset.asyncId,
Expand Down
3 changes: 2 additions & 1 deletion app/assets/javascripts/hw_combobox/models/combobox/toggle.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ Combobox.Toggle = Base => class extends Base {
this._isSync &&
!this._isSmallViewport &&
inputType != "hw:clickOutside" &&
inputType != "hw:focusOutside"
inputType != "hw:focusOutside" &&
inputType != "hw:asyncCloser"

this._lockInSelection()
this._clearInvalidQuery()
Expand Down
10 changes: 5 additions & 5 deletions test/dummy/app/controllers/state_chips_controller.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
class StateChipsController < ApplicationController
before_action :set_states, except: :new_possibly_new
before_action :set_states, except: :create_possibly_new

def new
def create
render turbo_stream: helpers.combobox_selection_chips_for(@states)
end

def new_html
def create_html
end

def new_dismissing
def create_dismissing
render turbo_stream: helpers.dismissing_combobox_selection_chips_for(@states)
end

def new_possibly_new
def create_possibly_new
@states = params[:combobox_values].split(",").map do |value|
State.find_by(id: value) || OpenStruct.new(to_combobox_display: value, id: value)
end
Expand Down
2 changes: 1 addition & 1 deletion test/dummy/app/views/comboboxes/morph.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@

<%= form_with model: @user do |f| %>
<%= f.combobox :home_state_id, State.all %>
<%= f.combobox :visited_state_ids, State.all, multiselect_chip_src: new_state_chip_path %>
<%= f.combobox :visited_state_ids, State.all, multiselect_chip_src: state_chips_path %>
<%= f.submit %>
<% end %>
2 changes: 1 addition & 1 deletion test/dummy/app/views/comboboxes/multiselect.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<%= combobox_tag "state", State.all,
id: "states-field",
label: "States",
multiselect_chip_src: new_state_chip_path,
multiselect_chip_src: state_chips_path,
placeholder: "Select states" %>
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@
<%= combobox_tag "state", states_path(page: 0),
id: "states-field",
label: "States",
multiselect_chip_src: new_html_state_chip_path,
multiselect_chip_src: html_state_chips_path,
value: State.limit(2).ids.join(","),
placeholder: "Select states" %>
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
<%= combobox_tag "states", State.all,
id: "states-field",
label: "States",
multiselect_chip_src: new_state_chip_path,
multiselect_chip_src: state_chips_path,
placeholder: "Select states",
data: { action: "hw-combobox:preselection->combobox--events#showPreselection hw-combobox:selection->combobox--events#showSelection hw-combobox:removal->combobox--events#showRemoval" } %>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
<%= combobox_tag "state", State.all,
id: "states-field",
label: "States",
multiselect_chip_src: new_dismissing_state_chip_path,
multiselect_chip_src: dismissing_state_chips_path,
placeholder: "Select states" %>

<%= combobox_tag "async_state", states_path,
id: "async-states-field",
label: "Async States",
multiselect_chip_src: dismissing_state_chips_path,
placeholder: "Select states" %>
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
State.all,
id: "states-field",
label: "Visited states (new allowed)",
multiselect_chip_src: new_possibly_new_state_chip_path,
multiselect_chip_src: possibly_new_state_chips_path,
name_when_new: "user[visited_states]" %>
<%= form.submit style: "padding: 0.5rem;" %>
<% end %>
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
<% end %>

<%= form_with model: @user, html: { style: "display: flex; flex-direction: column; gap: 1rem;" } do |form| %>
<%= form.combobox :visited_state_ids, State.all, label: "Visited states", multiselect_chip_src: new_state_chip_path %>
<%= form.combobox :visited_state_ids, State.all, label: "Visited states", multiselect_chip_src: state_chips_path %>
<%= form.submit style: "padding: 0.5rem;" %>
<% end %>
8 changes: 4 additions & 4 deletions test/dummy/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@
post "new_options_form", to: "new_options_forms#create"

resources :states, only: :index
resources :state_chips, only: :new, param: :combobox_value
get "new_html_state_chip", to: "state_chips#new_html"
get "new_dismissing_state_chip", to: "state_chips#new_dismissing"
get "new_possibly_new_state_chip", to: "state_chips#new_possibly_new"
resources :state_chips, only: :create, param: :combobox_value
post "html_state_chips", to: "state_chips#create_html"
post "dismissing_state_chips", to: "state_chips#create_dismissing"
post "possibly_new_state_chips", to: "state_chips#create_possibly_new"

resources :users, only: :update do
resources :visits, only: :create
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 @@ -933,6 +933,7 @@ class HotwireComboboxTest < ApplicationSystemTestCase
open_combobox "#user_visited_state_ids"
type_in_combobox "#user_visited_state_ids", "Lou"
click_on_option "Louisiana"
assert_open_combobox
assert_text "Alabama" # combobox is reset and still open

assert_combobox_display_and_value \
Expand Down Expand Up @@ -966,6 +967,24 @@ class HotwireComboboxTest < ApplicationSystemTestCase
states(:illinois, :louisiana).pluck(:id)
end

test "multiselect with dismissing streams" do
visit multiselect_dismissing_path

assert_closed_combobox

open_combobox "#states-field"
type_in_combobox "#states-field", "Lou"
click_on_option "Louisiana"
sleep 1
assert_closed_combobox

open_combobox "#async-states-field"
type_in_combobox "#async-states-field", "Lou"
click_on_option "Louisiana"
sleep 1
assert_closed_combobox
end

test "multiselect custom events" do
visit multiselect_custom_events_path

Expand Down