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

Add a component parameter to the FacetComponent, allowing callers to … #2788

Merged
merged 1 commit into from
Aug 11, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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