From ab1510f81888ef1e83c2389800a4864806067d2a Mon Sep 17 00:00:00 2001 From: Jose Farias <31393016+josefarias@users.noreply.github.com> Date: Mon, 4 Mar 2024 18:03:51 -0600 Subject: [PATCH] Make `:for_id` optional in async options (#64) Co-authored-by: Chris Oliver --- .../hw_combobox/models/combobox/filtering.js | 7 ++++- app/presenters/hotwire_combobox/component.rb | 6 ++++- .../_next_page.turbo_stream.erb | 2 +- lib/hotwire_combobox/helper.rb | 26 +++++++++---------- .../app/views/movies/index.turbo_stream.erb | 1 - .../views/movies/index_html.turbo_stream.erb | 1 - 6 files changed, 25 insertions(+), 18 deletions(-) diff --git a/app/assets/javascripts/hw_combobox/models/combobox/filtering.js b/app/assets/javascripts/hw_combobox/models/combobox/filtering.js index a5779f9..14f6250 100644 --- a/app/assets/javascripts/hw_combobox/models/combobox/filtering.js +++ b/app/assets/javascripts/hw_combobox/models/combobox/filtering.js @@ -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 }) } diff --git a/app/presenters/hotwire_combobox/component.rb b/app/presenters/hotwire_combobox/component.rb index aff8882..b66f4ef 100644 --- a/app/presenters/hotwire_combobox/component.rb +++ b/app/presenters/hotwire_combobox/component.rb @@ -1,5 +1,5 @@ class HotwireCombobox::Component - attr_reader :async_src, :options, :dialog_label + attr_reader :options, :dialog_label def initialize \ view, name, @@ -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) diff --git a/app/views/hotwire_combobox/_next_page.turbo_stream.erb b/app/views/hotwire_combobox/_next_page.turbo_stream.erb index 1d0dd49..4a3bfc9 100644 --- a/app/views/hotwire_combobox/_next_page.turbo_stream.erb +++ b/app/views/hotwire_combobox/_next_page.turbo_stream.erb @@ -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 %> diff --git a/lib/hotwire_combobox/helper.rb b/lib/hotwire_combobox/helper.rb index a7d9930..bc49e9f 100644 --- a/lib/hotwire_combobox/helper.rb +++ b/lib/hotwire_combobox/helper.rb @@ -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) @@ -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 @@ -57,9 +57,9 @@ 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 @@ -67,15 +67,6 @@ 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) @@ -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) diff --git a/test/dummy/app/views/movies/index.turbo_stream.erb b/test/dummy/app/views/movies/index.turbo_stream.erb index cfca708..cbeecb3 100644 --- a/test/dummy/app/views/movies/index.turbo_stream.erb +++ b/test/dummy/app/views/movies/index.turbo_stream.erb @@ -1,3 +1,2 @@ <%= hw_async_combobox_options @page.records, - for_id: "movie-field", next_page: @page.last? ? nil : @page.next_param %> diff --git a/test/dummy/app/views/movies/index_html.turbo_stream.erb b/test/dummy/app/views/movies/index_html.turbo_stream.erb index 1804b76..677dc58 100644 --- a/test/dummy/app/views/movies/index_html.turbo_stream.erb +++ b/test/dummy/app/views/movies/index_html.turbo_stream.erb @@ -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 %>