Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Itacomp is a [View Component](https://viewcomponent.org/) and [Helper](https://a
* [ita_progress](app/helpers/itacomp/common_helper.rb)
* [ita_spinner](app/helpers/itacomp/common_helper.rb)
* [ita_visually_hidden](app/helpers/itacomp/common_helper.rb)
* [ita_badge](app/helpers/itacomp/common_helper.rb)
* [ita_size](app/helpers/itacomp/common_helper.rb)
* [ita_bg](app/helpers/itacomp/common_helper.rb)
* [ita_text](app/helpers/itacomp/common_helper.rb)
Expand Down
13 changes: 13 additions & 0 deletions app/helpers/itacomp/common_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,19 @@ def ita_progress(value = nil, type: nil)
tag.div tag.div(**opts), class: progress_class
end

# return a span.badge tag with an optional span.visually-hidden tag
#
# ==== Options
# * <tt>value</tt> [String], default <tt>nil</tt>, mandatory.badge content
# * <tt>msg</tt> [String], default <tt>nil</tt>, optional visually-hidden span message for accessibility
# * <tt>:class</tt> [String,Array] default <tt>nil</tt>, if present is added as tag class after <tt>badge</tt>
# * <tt>**opts</tt> [Hash] default <tt>{}</tt>, each other named params is delegated to span.badge tag.
def ita_badge(value, msg = nil, **opts)
opts[:class] = [ "badge", opts[:class] ]
hidden = msg.present? ? ita_visually_hidden(msg) : nil
safe_join([ tag.span(value, **opts), hidden ])
end

# return size class from bootstrap-italia size types.
#
# ==== Options
Expand Down
6 changes: 6 additions & 0 deletions test/helpers/common_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ class CommonHelperTest < ActionView::TestCase
assert_equal '<div class="progress progress-color"><div class="progress-bar bg-primary" role="progressbar" style="width: 50%" aria-valuemin="0" aria-valuemax="100" aria-valuenow="50"></div></div>', ita_progress(50, type: "primary")
end

test "ita_badge" do
assert_equal '<span class="badge">1</span>', ita_badge(1)
assert_equal '<span class="badge test" id="test">1</span>', ita_badge(1, class: "test", id: "test")
assert_equal '<span class="badge">1</span><span class="visually-hidden">test</span>', ita_badge(1, "test")
end

test "ita_size" do
assert_equal "size-md", ita_size
assert_equal "size-xs", ita_size(:xs)
Expand Down