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

Make :for_id optional in async options #64

Merged
merged 2 commits into from
Mar 5, 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 @@ -21,7 +21,12 @@ Combobox.Filtering = Base => class extends Base {
}

async _filterAsync(event) {
const query = { q: this._fullQuery, input_type: event.inputType }
const query = {
q: this._fullQuery,
input_type: event.inputType,
for_id: this.element.dataset.asyncId
}

await get(this.asyncSrcValue, { responseKind: "turbo-stream", query })
}

Expand Down
6 changes: 5 additions & 1 deletion app/presenters/hotwire_combobox/component.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class HotwireCombobox::Component
attr_reader :async_src, :options, :dialog_label
attr_reader :options, :dialog_label

def initialize \
view, name,
Expand Down Expand Up @@ -181,6 +181,10 @@ def association_exists?
form&.object&.class&.reflect_on_association(association_name).present?
end

def async_src
view.hw_uri_with_params @async_src, for_id: canonical_id, format: :turbo_stream
end


def canonical_id
id || form&.field_id(name)
Expand Down
2 changes: 1 addition & 1 deletion app/views/hotwire_combobox/_next_page.turbo_stream.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

<%= turbo_stream.remove hw_pagination_frame_wrapper_id(for_id) %>
<%= turbo_stream.append hw_listbox_id(for_id) do %>
<%= render "hotwire_combobox/pagination", for_id: for_id, src: hw_combobox_next_page_uri(src, next_page) %>
<%= render "hotwire_combobox/pagination", for_id: for_id, src: hw_combobox_next_page_uri(src, next_page, for_id) %>
<% end %>
26 changes: 13 additions & 13 deletions lib/hotwire_combobox/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def hw_combobox_options(options, render_in: {}, display: :to_combobox_display, *
end
hw_alias :hw_combobox_options

def hw_paginated_combobox_options(options, for_id:, src: request.path, next_page: nil, render_in: {}, **methods)
def hw_paginated_combobox_options(options, for_id: params[:for_id], src: request.path, next_page: nil, render_in: {}, **methods)
this_page = render("hotwire_combobox/paginated_options", for_id: for_id, options: hw_combobox_options(options, render_in: render_in, **methods))
next_page = render("hotwire_combobox/next_page", for_id: for_id, src: src, next_page: next_page)

Expand All @@ -44,7 +44,7 @@ def hw_paginated_combobox_options(options, for_id:, src: request.path, next_page
alias_method :hw_async_combobox_options, :hw_paginated_combobox_options
hw_alias :hw_async_combobox_options

protected # library use only
# private library use only
def hw_listbox_id(id)
"#{id}-hw-listbox"
end
Expand All @@ -57,25 +57,16 @@ def hw_pagination_frame_id(id)
"#{id}__hw_combobox_pagination"
end

def hw_combobox_next_page_uri(uri, next_page)
def hw_combobox_next_page_uri(uri, next_page, for_id)
if next_page
hw_uri_with_params uri, page: next_page, q: params[:q], format: :turbo_stream
hw_uri_with_params uri, page: next_page, q: params[:q], for_id: for_id, format: :turbo_stream
end
end

def hw_combobox_page_stream_action
params[:page] ? :append : :update
end

private
def hw_extract_options_and_src(options_or_src, render_in)
if options_or_src.is_a? String
[ [], hw_uri_with_params(options_or_src, format: :turbo_stream) ]
else
[ hw_combobox_options(options_or_src, render_in: render_in), nil ]
end
end

def hw_uri_with_params(url_or_path, **params)
URI.parse(url_or_path).tap do |url_or_path|
query = URI.decode_www_form(url_or_path.query || "").to_h.merge(params)
Expand All @@ -85,6 +76,15 @@ def hw_uri_with_params(url_or_path, **params)
url_or_path
end

private
def hw_extract_options_and_src(options_or_src, render_in)
if options_or_src.is_a? String
[ [], options_or_src ]
else
[ hw_combobox_options(options_or_src, render_in: render_in), nil ]
end
end

def hw_parse_combobox_options(options, render_in: nil, **methods)
options.map do |option|
HotwireCombobox::Listbox::Option.new **hw_option_attrs_for(option, render_in: render_in, **methods)
Expand Down
1 change: 0 additions & 1 deletion test/dummy/app/views/movies/index.turbo_stream.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
<%= hw_async_combobox_options @page.records,
for_id: "movie-field",
next_page: @page.last? ? nil : @page.next_param %>
1 change: 0 additions & 1 deletion test/dummy/app/views/movies/index_html.turbo_stream.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<%= hw_async_combobox_options @page.records,
render_in: { partial: "movies/movie" },
for_id: "movie-field",
next_page: @page.last? ? nil : @page.next_param %>
Loading