diff --git a/app/components/filters/filter_form_component.html.erb b/app/components/filters/filter_form_component.html.erb index 2c5408a8285..10fc8108366 100644 --- a/app/components/filters/filter_form_component.html.erb +++ b/app/components/filters/filter_form_component.html.erb @@ -1,9 +1,11 @@ -<% if @wrap_with_controller %> - <%= tag.div(class: "op-filters-form -expanded", data: controller_data_attributes) do %> - <%= hidden_filters_input if @hidden_input_name %> - <%= render(form_list) %> - <% end %> -<% else %> +<%= + render( + Primer::ConditionalWrapper.new( + condition: @wrap_with_controller, + **@wrapper_arguments + ) + ) do +%> <%= hidden_filters_input if @hidden_input_name %> <%= render(form_list) %> <% end %> diff --git a/app/components/filters/filter_form_component.rb b/app/components/filters/filter_form_component.rb index 7d19a594cbd..f7ad8e58d1f 100644 --- a/app/components/filters/filter_form_component.rb +++ b/app/components/filters/filter_form_component.rb @@ -74,6 +74,9 @@ # in a Primer dialog or another container that clips overflow, so the dropdown # portal renders outside that container instead of being clipped. class Filters::FilterFormComponent < ApplicationComponent + include Primer::AttributesHelper + include Primer::FetchOrFallbackHelper + OUTPUT_FORMATS = %i[params json].freeze def initialize(builder:, @@ -82,15 +85,31 @@ class Filters::FilterFormComponent < ApplicationComponent wrap_with_controller: false, hidden_input_name: nil, output_format: nil, - autocomplete_append_to: nil) + autocomplete_append_to: nil, + **wrapper_arguments) super() @builder = builder @query = query @allowed_filters = allowed_filters || query.available_advanced_filters @wrap_with_controller = wrap_with_controller @hidden_input_name = hidden_input_name - @output_format = validate_output_format(output_format) + @output_format = fetch_or_fallback(OUTPUT_FORMATS, output_format.to_sym) if output_format @autocomplete_append_to = autocomplete_append_to + @wrapper_arguments = wrapper_arguments + @wrapper_arguments[:tag] ||= :div + @wrapper_arguments[:classes] = class_names( + "op-filters-form -expanded", + @wrapper_arguments[:classes] + ) + @wrapper_arguments[:data] = merge_data( + @wrapper_arguments, + { + data: { + controller: "filter--filters-form", + filter__filters_form_output_format_value: @output_format&.to_s + } + } + ) end private @@ -101,28 +120,11 @@ class Filters::FilterFormComponent < ApplicationComponent Primer::Forms::FormList.new(*sub_forms) end - def controller_data_attributes - attrs = { controller: "filter--filters-form" } - attrs["filter--filters-form-output-format-value"] = @output_format.to_s if @output_format - attrs - end - - def validate_output_format(format) - return nil if format.nil? - - sym = format.to_sym - unless OUTPUT_FORMATS.include?(sym) - raise ArgumentError, - "Unknown output_format #{format.inspect}; expected one of #{OUTPUT_FORMATS.inspect}" - end - sym - end - def hidden_filters_input hidden_field_tag( @hidden_input_name, "", - data: { "filter--filters-form-target": "filtersInput" } + data: { filter__filters_form_target: "filtersInput" } ) end diff --git a/lookbook/docs/patterns/11-filter-forms.md.erb b/lookbook/docs/patterns/11-filter-forms.md.erb index fdbf97fe6a1..8d730bc2970 100644 --- a/lookbook/docs/patterns/11-filter-forms.md.erb +++ b/lookbook/docs/patterns/11-filter-forms.md.erb @@ -98,9 +98,10 @@ The host server receives the canonical string in ### Combining with non-filter inputs -`FilterFormComponent` composes with other forms via -`Primer::Forms::FormList`. All children share the same builder and -therefore submit through the same `