-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
104 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# frozen_string_literal: true | ||
|
||
module TabBarHelper | ||
def tab_bar_item(item) | ||
content_tag( | ||
:li, | ||
safe_join([li_icon(item), li_content(item)]), | ||
class: li_class(item) | ||
) | ||
end | ||
|
||
private | ||
|
||
def li_content(item) | ||
if item[:link] | ||
link_to item[:title], item[:link] | ||
else | ||
item[:title] | ||
end | ||
end | ||
|
||
def li_class(item) | ||
'jam-tab-bar__selected' if item[:selected] | ||
end | ||
|
||
def li_icon(item) | ||
content_tag(:span, icon_svg.html_safe, class: 'icon') if item[:icon] # rubocop:disable Rails/OutputSafety | ||
end | ||
|
||
def icon_svg | ||
%( | ||
<svg width="0.75em" height="0.75em" viewBox="0 0 14 15" fill="none" xmlns="http://www.w3.org/2000/svg"> | ||
<path d="M14 8.5H8V14.5H6V8.5H0V6.5H6V0.5H8V6.5H14V8.5Z" fill="#F16B7C"/> | ||
</svg> | ||
) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<div class="jam-tab-bar"> | ||
<ul class="switcher"> | ||
<% if local_assigns[:items] %> | ||
<% items.each do |item| %> | ||
<%= tab_bar_item(item) %> | ||
<% end %> | ||
<% end %> | ||
</ul> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# frozen_string_literal: true | ||
|
||
class TabBarPreview < Lookbook::Preview | ||
def default | ||
render('shared/tab_bar', items: [ | ||
{ title: 'First', selected: true }, | ||
{ title: 'Second', link: 'https://example.com' }, | ||
{ title: 'Third', link: 'https://example.com', icon: true } | ||
]) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'test_helper' | ||
|
||
class TabBarHelperTest < ActionView::TestCase | ||
test 'tab_bar_item adds selected class if item is selected' do | ||
assert_dom_equal '<li class="jam-tab-bar__selected"></li>', tab_bar_item({ selected: true }) | ||
end | ||
|
||
test 'tab_bar_item does not add selected class if item is not selected' do | ||
assert_dom_equal '<li></li>', tab_bar_item({}) | ||
end | ||
|
||
test 'tab_bar_item uses provided title' do | ||
assert_dom_equal '<li>foo</li>', tab_bar_item({ title: 'foo' }) | ||
end | ||
|
||
test 'tab_bar_item links to provided title if link provided' do | ||
assert_dom_equal( | ||
'<li><a href="https://example.com">foo</a></li>', | ||
tab_bar_item({ title: 'foo', link: 'https://example.com' }) | ||
) | ||
end | ||
|
||
test 'tab_bar_item can include icon' do | ||
expected = %( | ||
<li> | ||
<span class="icon"> | ||
<svg width="0.75em" height="0.75em" viewBox="0 0 14 15" fill="none" xmlns="http://www.w3.org/2000/svg"> | ||
<path d="M14 8.5H8V14.5H6V8.5H0V6.5H6V0.5H8V6.5H14V8.5Z" fill="#F16B7C"/> | ||
</svg> | ||
</span> | ||
<a href="https://example.com">foo</a> | ||
</li> | ||
) | ||
|
||
assert_dom_equal( | ||
expected, | ||
tab_bar_item({ title: 'foo', link: 'https://example.com', icon: true }) | ||
) | ||
end | ||
end |