Skip to content

Commit

Permalink
Add #sl_button_to
Browse files Browse the repository at this point in the history
  • Loading branch information
yuki24 committed Jan 5, 2023
1 parent 81d4b5c commit e1bdedb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
23 changes: 8 additions & 15 deletions app/helpers/shoelace/form_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -260,21 +260,14 @@ def sl_button_tag(**attrs, &block)
content_tag("sl-button", **attrs, &block)
end

# Not providing this helper for now due to potentially untraceable HTML. E.g.
#
# <a href="...">
# <sl-button>...</sl-button>
# </a>
#
# may be trackable and traceable by search bots and scrapers, but:
#
# <sl-button href="...">...</sl-button>
#
# may not be. In the mean time, it is advisable to wrap a <sl-button> tag with an <a> tag.
#
# def sl_button_to(href, **attrs, &block)
# sl_button_tag(href: href, **attrs, &block)
# end
# Creates an <sl-button> tag with the href value as the caption.
def sl_button_to(body, href = nil, **attrs, &block)
if block_given? && href.nil?
sl_button_tag(href: body, **attrs, &block)
else
sl_button_tag(href: href) { body }
end
end

# Creates a submit button with the text value as the caption, with the +submit+ attribute.
def sl_submit_tag(value = 'Save changes', **options)
Expand Down
14 changes: 14 additions & 0 deletions test/helpers/form_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,20 @@ class FormHelperTest < ActionView::TestCase
HTML
end

test "#sl_button_to" do
assert_dom_equal <<~HTML, sl_button_to("Next", "/next")
<sl-button href="/next">Next</sl-button>
HTML

assert_dom_equal(<<~HTML, sl_button_to("/next") { "Next" })
<sl-button href="/next">Next</sl-button>
HTML

assert_dom_equal <<~HTML, sl_button_to("Next")
<sl-button>Next</sl-button>
HTML
end

test "#sl_submit_tag" do
assert_dom_equal <<~HTML, sl_submit_tag("Save")
<sl-button type="submit" variant="primary" data-disable-with="Save">Save</sl-button>
Expand Down

0 comments on commit e1bdedb

Please sign in to comment.