From 77ab8a9f0b455ffe0ebdaeef6886332d99db9fc2 Mon Sep 17 00:00:00 2001 From: Chris Beer Date: Thu, 13 Oct 2022 10:53:20 -0700 Subject: [PATCH] Restore the wrapping blacklight-icons span to componentized icons --- .../blacklight/icons/icon_component.rb | 16 ++++++++++++++-- app/helpers/blacklight/icon_helper_behavior.rb | 6 +++--- .../blacklight/icon_helper_behavior_spec.rb | 16 ++++++++++++++-- 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/app/components/blacklight/icons/icon_component.rb b/app/components/blacklight/icons/icon_component.rb index 63106d0e71..2d2f924a5c 100644 --- a/app/components/blacklight/icons/icon_component.rb +++ b/app/components/blacklight/icons/icon_component.rb @@ -6,15 +6,27 @@ module Icons # You can override the default svg by setting: # Blacklight::Icons::ListComponent.svg = 'your SVG here' class IconComponent < ::ViewComponent::Base - def initialize(svg: nil) + # rubocop:disable Metrics/ParameterLists + def initialize(svg: nil, tag: :span, name: nil, label: nil, aria_hidden: nil, classes: nil, **options) self.svg = svg if svg + @classes = Array(classes) + ['blacklight-icons', "blacklight-icons-#{name}"] + @name = name + @tag = tag + @options = options.merge(aria: options.fetch(:aria, {}).reverse_merge(label: label, hidden: aria_hidden)) end + # rubocop:enable Metrics/ParameterLists def call - svg&.html_safe # rubocop:disable Rails/OutputSafety + tag.public_send(@tag, svg&.html_safe, # rubocop:disable Rails/OutputSafety + class: @classes, + **@options) end class_attribute :svg + + def name + @name ||= self.class.name.demodulize.underscore.sub('_component', '') + end end end end diff --git a/app/helpers/blacklight/icon_helper_behavior.rb b/app/helpers/blacklight/icon_helper_behavior.rb index d2a87764c0..b83ba59dd6 100644 --- a/app/helpers/blacklight/icon_helper_behavior.rb +++ b/app/helpers/blacklight/icon_helper_behavior.rb @@ -9,13 +9,13 @@ module Blacklight::IconHelperBehavior # the svg everytime. # @param [String, Symbol] icon_name # @return [String] - def blacklight_icon(icon_name, _options = {}) - render "Blacklight::Icons::#{icon_name.to_s.camelize}Component".constantize.new + def blacklight_icon(icon_name, **kwargs) + render "Blacklight::Icons::#{icon_name.to_s.camelize}Component".constantize.new(**kwargs) rescue NameError Blacklight.deprecation.warn( "Falling back on the LegacyIconComponent with \"#{icon_name}\" is deprecated. Instead create the component `Blacklight::Icons::#{icon_name.to_s.camelize}Component` for this icon." ) - render Blacklight::Icons::LegacyIconComponent.new(name: icon_name) + render Blacklight::Icons::LegacyIconComponent.new(name: icon_name, **kwargs) end end diff --git a/spec/helpers/blacklight/icon_helper_behavior_spec.rb b/spec/helpers/blacklight/icon_helper_behavior_spec.rb index 57fd874807..7946b9f3c8 100644 --- a/spec/helpers/blacklight/icon_helper_behavior_spec.rb +++ b/spec/helpers/blacklight/icon_helper_behavior_spec.rb @@ -2,10 +2,22 @@ RSpec.describe Blacklight::IconHelperBehavior do describe '#blacklight_icon' do - subject(:icon) { helper.blacklight_icon(:search) } + subject(:icon) { helper.blacklight_icon(:search, classes: 'custom-class') } it 'returns the svg' do - expect(icon).to have_css 'svg' + expect(icon).to have_css '.blacklight-icons svg' + end + + it 'adds classes to the wrappering element' do + expect(icon).to have_css '.custom-class svg' + end + + context 'with backwards compatible arguments' do + subject(:icon) { helper.blacklight_icon(:search, aria_hidden: true, label: 'blah') } + + it 'adds aria attributes' do + expect(icon).to have_css '[aria-hidden="true"][aria-label="blah"]' + end end end end