diff --git a/app/components/blacklight/facet_component.rb b/app/components/blacklight/facet_component.rb index 3d7926e7b..f90c33c5a 100644 --- a/app/components/blacklight/facet_component.rb +++ b/app/components/blacklight/facet_component.rb @@ -11,8 +11,8 @@ class FacetComponent < ViewComponent::Base # @param [Blacklight::Solr::Response::Facets::FacetField] display_facet # @param [Blacklight::Configuration] blacklight_config # @param [Boolean] layout - # rubocop:disable Metrics/CyclomaticComplexity - def initialize(display_facet_or_field_config: nil, display_facet: nil, field_config: nil, response: nil, blacklight_config: nil, **component_args) + # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/ParameterLists + def initialize(display_facet_or_field_config: nil, display_facet: nil, field_config: nil, response: nil, blacklight_config: nil, component: nil, **component_args) if display_facet_or_field_config.is_a? Blacklight::FacetFieldPresenter @facet_field_presenter = display_facet_or_field_config @field_config = @facet_field_presenter.facet_field @@ -30,9 +30,10 @@ def initialize(display_facet_or_field_config: nil, display_facet: nil, field_con raise ArgumentError, 'You must provide one of display_facet or field_config' unless @field_config end + @component = component || (@field_config.component == true ? Blacklight::FacetFieldListComponent : @field_config.component) @component_args = component_args end - # rubocop:enable Metrics/CyclomaticComplexity + # rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/ParameterLists def render? helpers.should_render_field?(@field_config, @display_facet) @@ -41,10 +42,8 @@ def render? def call return render_partial if @field_config.partial - component = @field_config.component == true ? Blacklight::FacetFieldListComponent : @field_config.component - render( - component.new( + @component.new( facet_field: @facet_field_presenter || helpers.facet_field_presenter(@field_config, @display_facet), **@component_args ) diff --git a/spec/components/blacklight/facet_component_spec.rb b/spec/components/blacklight/facet_component_spec.rb index 3aa85330e..0ec546bf5 100644 --- a/spec/components/blacklight/facet_component_spec.rb +++ b/spec/components/blacklight/facet_component_spec.rb @@ -26,6 +26,25 @@ expect(rendered).to have_selector 'ul.facet-values' end + context 'with a provided component' do + let(:component_kwargs) { { field_config: facet_config, display_facet: display_facet, component: component_class } } + let(:component_class) do + Class.new(Blacklight::FacetFieldListComponent) do + def self.name + 'CustomFacetComponent' + end + + def call + 'Custom facet rendering' + end + end + end + + it 'renders the provided component' do + expect(rendered).to have_content 'Custom facet rendering' + end + end + context 'with a facet configured to use a partial' do let(:facet_config) do Blacklight::Configuration::FacetField.new(key: 'field', partial: 'catalog/facet_partial').normalize!