Skip to content

Commit

Permalink
Merge pull request #2788 from projectblacklight/facet-component-arg
Browse files Browse the repository at this point in the history
Add a component parameter to the FacetComponent, allowing callers to …
  • Loading branch information
jcoyne committed Aug 11, 2022
2 parents be96f65 + c23df54 commit 57db11b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
9 changes: 5 additions & 4 deletions app/components/blacklight/facet_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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, Metrics/PerceivedComplexity
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
Expand All @@ -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
@component_args = component_args
end
# rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
# rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/ParameterLists

def render?
helpers.should_render_field?(@field_config, @display_facet)
Expand All @@ -42,7 +43,7 @@ def call
return render_partial if @field_config.partial

render(
@field_config.component.new(
@component.new(
facet_field: @facet_field_presenter || helpers.facet_field_presenter(@field_config, @display_facet),
**@component_args
)
Expand Down
19 changes: 19 additions & 0 deletions spec/components/blacklight/facet_component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,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!
Expand Down

0 comments on commit 57db11b

Please sign in to comment.