diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index c662b364e589a1b05b0be3a0a41db2f49df51f9a..90218d42c377dc0497fd83e0aebef047c51e9dd3 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -498,14 +498,13 @@ def gitlab_ui_form_with(**args, &block) # link_button_to _('Foo'), some_path, variant: :danger, category: :secondary # # For accessibility, ensure that icon-only links have aria-label set. - def link_button_to(name = nil, options = nil, html_options = nil, &block) + def link_button_to(name = nil, href = nil, options = nil, &block) if block - html_options = options - options = name - name = block + options = href + href = name end - html_options ||= {} + options ||= {} # Ignore args that don't make sense for links, like disabled, loading, etc. options_for_button = %i[ @@ -519,15 +518,11 @@ def link_button_to(name = nil, options = nil, html_options = nil, &block) method ] - args = html_options.slice(*options_for_button) - html_options = html_options.except(*options_for_button) + args = options.slice(*options_for_button) + button_options = options.except(*options_for_button) - if block - render Pajamas::ButtonComponent.new(href: options, **args, button_options: html_options), &block - else - render Pajamas::ButtonComponent.new(href: options, **args, button_options: html_options) do - name - end + render Pajamas::ButtonComponent.new(href: href, **args, button_options: button_options) do + block.present? ? yield : name end end diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb index 7a35ebb1a7116ff473b9052e850155ce46c1ae38..f87971094014097f17cbb0e73e37b02454a62541 100644 --- a/spec/helpers/application_helper_spec.rb +++ b/spec/helpers/application_helper_spec.rb @@ -621,19 +621,19 @@ def stub_controller_method(method_name, value) describe '#link_button_to', feature_category: :design_system do let(:content) { 'Button content' } - let(:options) { '#' } - let(:html_options) { {} } + let(:href) { '#' } + let(:options) { {} } RSpec.shared_examples 'basic behavior' do it 'renders a basic link button' do expect(subject.name).to eq('a') expect(subject.classes).to include(*%w[gl-button btn btn-md btn-default]) - expect(subject.attr('href')).to eq(options) + expect(subject.attr('href')).to eq(href) expect(subject.content.strip).to eq(content) end describe 'variant option' do - let(:html_options) { { variant: :danger } } + let(:options) { { variant: :danger } } it 'renders the variant class' do expect(subject.classes).to include('btn-danger') @@ -641,7 +641,7 @@ def stub_controller_method(method_name, value) end describe 'category option' do - let(:html_options) { { category: :tertiary } } + let(:options) { { category: :tertiary } } it 'renders the category class' do expect(subject.classes).to include('btn-default-tertiary') @@ -649,7 +649,7 @@ def stub_controller_method(method_name, value) end describe 'size option' do - let(:html_options) { { size: :small } } + let(:options) { { size: :small } } it 'renders the small class' do expect(subject.classes).to include('btn-sm') @@ -657,7 +657,7 @@ def stub_controller_method(method_name, value) end describe 'block option' do - let(:html_options) { { block: true } } + let(:options) { { block: true } } it 'renders the block class' do expect(subject.classes).to include('btn-block') @@ -665,7 +665,7 @@ def stub_controller_method(method_name, value) end describe 'selected option' do - let(:html_options) { { selected: true } } + let(:options) { { selected: true } } it 'renders the selected class' do expect(subject.classes).to include('selected') @@ -673,7 +673,7 @@ def stub_controller_method(method_name, value) end describe 'target option' do - let(:html_options) { { target: '_blank' } } + let(:options) { { target: '_blank' } } it 'renders the target attribute' do expect(subject.attr('target')).to eq('_blank') @@ -681,7 +681,7 @@ def stub_controller_method(method_name, value) end describe 'method option' do - let(:html_options) { { method: :post } } + let(:options) { { method: :post } } it 'renders the data-method attribute' do expect(subject.attr('data-method')).to eq('post') @@ -689,7 +689,7 @@ def stub_controller_method(method_name, value) end describe 'icon option' do - let(:html_options) { { icon: 'remove' } } + let(:options) { { icon: 'remove' } } it 'renders the icon' do icon = subject.at_css('svg.gl-icon') @@ -699,7 +699,7 @@ def stub_controller_method(method_name, value) describe 'icon only' do let(:content) { nil } - let(:html_options) { { icon: 'remove' } } + let(:options) { { icon: 'remove' } } it 'renders the icon-only class' do expect(subject.classes).to include('btn-icon') @@ -708,7 +708,7 @@ def stub_controller_method(method_name, value) describe 'arbitrary html options' do let(:content) { nil } - let(:html_options) { { data: { foo: true }, aria: { labelledby: 'foo' } } } + let(:options) { { data: { foo: true }, aria: { labelledby: 'foo' } } } it 'renders the attributes' do expect(subject.attr('data-foo')).to eq('true') @@ -719,7 +719,7 @@ def stub_controller_method(method_name, value) describe 'without block' do subject do - tag = helper.link_button_to content, options, **html_options + tag = helper.link_button_to content, href, options Nokogiri::HTML.fragment(tag).first_element_child end @@ -728,7 +728,7 @@ def stub_controller_method(method_name, value) describe 'with block' do subject do - tag = helper.link_button_to options, **html_options do + tag = helper.link_button_to href, options do content end Nokogiri::HTML.fragment(tag).first_element_child