-
-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #156 from baoagency/tabs
Add Tabs component
- Loading branch information
Showing
15 changed files
with
212 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,10 @@ | ||
<li class="Polaris-Tabs__TabContainer" role="presentation"> | ||
<%= render(Polaris::BaseComponent.new(**system_arguments)) do %> | ||
<span class="Polaris-Tabs__Title"> | ||
<%= @title %> | ||
<% if badge.present? %> | ||
<%= badge %> | ||
<% end %> | ||
</span> | ||
<% end %> | ||
</li> |
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,34 @@ | ||
class Polaris::Tabs::TabComponent < Polaris::NewComponent | ||
renders_one :badge, Polaris::BadgeComponent | ||
|
||
def initialize( | ||
title:, | ||
url: nil, | ||
active: false, | ||
**system_arguments | ||
) | ||
@title = title | ||
@url = url | ||
@active = active | ||
@system_arguments = system_arguments | ||
end | ||
|
||
def system_arguments | ||
@system_arguments.tap do |opts| | ||
opts[:rol] = "tab" | ||
opts[:tabindex] = "0" | ||
if @url.present? | ||
opts[:tag] = "a" | ||
opts[:href] = @url | ||
else | ||
opts[:tag] = "button" | ||
opts[:type] = "button" | ||
end | ||
opts[:classes] = class_names( | ||
@system_arguments[:classes], | ||
"Polaris-Tabs__Tab", | ||
"Polaris-Tabs__Tab--selected": @active | ||
) | ||
end | ||
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,7 @@ | ||
<%= render(Polaris::BaseComponent.new(**wrapper_arguments)) do %> | ||
<%= render(Polaris::BaseComponent.new(**system_arguments)) do %> | ||
<% tabs.each do |tab| %> | ||
<%= tab %> | ||
<% end %> | ||
<% 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,37 @@ | ||
module Polaris | ||
class TabsComponent < Polaris::NewComponent | ||
renders_many :tabs, Polaris::Tabs::TabComponent | ||
|
||
def initialize(fitted: false, wrapper_arguments: {}, **system_arguments) | ||
@fitted = fitted | ||
@wrapper_arguments = wrapper_arguments | ||
@system_arguments = system_arguments | ||
end | ||
|
||
def wrapper_arguments | ||
@wrapper_arguments.tap do |opts| | ||
opts[:tag] = "div" | ||
opts[:classes] = class_names( | ||
@wrapper_arguments[:classes], | ||
"Polaris-Tabs__Wrapper" | ||
) | ||
end | ||
end | ||
|
||
def system_arguments | ||
@system_arguments.tap do |opts| | ||
opts[:tag] = "ul" | ||
opts[:role] = "tablist" | ||
opts[:classes] = class_names( | ||
@system_arguments[:classes], | ||
"Polaris-Tabs", | ||
"Polaris-Tabs--fitted": @fitted | ||
) | ||
end | ||
end | ||
|
||
def renders? | ||
tabs.present? | ||
end | ||
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
10 changes: 10 additions & 0 deletions
10
demo/test/components/previews/navigation/tabs_component_preview.rb
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,10 @@ | ||
class Navigation::TabsComponentPreview < ViewComponent::Preview | ||
def default | ||
end | ||
|
||
def fitted | ||
end | ||
|
||
def with_badge | ||
end | ||
end |
12 changes: 12 additions & 0 deletions
12
demo/test/components/previews/navigation/tabs_component_preview/default.html.erb
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,12 @@ | ||
<%= polaris_card do |card| %> | ||
<% card.tabs do |tabs| %> | ||
<% tabs.tab(title: "All", active: true) %> | ||
<% tabs.tab(title: "Accepts marketing") %> | ||
<% tabs.tab(title: "Repeat customer") %> | ||
<% tabs.tab(title: "Prospects", url: "#") %> | ||
<% end %> | ||
<% card.section(title: "All") do %> | ||
Tab "All" selected. | ||
<% end %> | ||
<% end %> |
12 changes: 12 additions & 0 deletions
12
demo/test/components/previews/navigation/tabs_component_preview/fitted.html.erb
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,12 @@ | ||
<%= polaris_card do |card| %> | ||
<% card.tabs(fitted: true) do |tabs| %> | ||
<% tabs.tab(title: "All", active: true) %> | ||
<% tabs.tab(title: "Accepts marketing") %> | ||
<% tabs.tab(title: "Repeat customer") %> | ||
<% tabs.tab(title: "Prospects", url: "#") %> | ||
<% end %> | ||
<% card.section(title: "All") do %> | ||
Tab "All" selected. | ||
<% end %> | ||
<% end %> |
14 changes: 14 additions & 0 deletions
14
demo/test/components/previews/navigation/tabs_component_preview/with_badge.html.erb
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,14 @@ | ||
<%= polaris_card do |card| %> | ||
<% card.tabs(fitted: true) do |tabs| %> | ||
<% tabs.tab(title: "All", active: true) do |tab| %> | ||
<% tab.badge { "10+" } %> | ||
<% end %> | ||
<% tabs.tab(title: "Accepts marketing") do |tab| %> | ||
<% tab.badge { "4" } %> | ||
<% end %> | ||
<% end %> | ||
<% card.section(title: "All") do %> | ||
Tab "All" selected. | ||
<% 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
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,45 @@ | ||
require "test_helper" | ||
|
||
class TabsComponentTest < Minitest::Test | ||
include Polaris::ComponentTestHelpers | ||
|
||
def test_default_tabs | ||
render_inline(Polaris::TabsComponent.new) do |tabs| | ||
tabs.tab(title: "Active", active: true) | ||
tabs.tab(title: "With URL", url: "https://shopify.dev") | ||
end | ||
|
||
assert_selector ".Polaris-Tabs__Wrapper > ul.Polaris-Tabs" do | ||
assert_selector "li.Polaris-Tabs__TabContainer", count: 2 | ||
assert_selector "li:nth-of-type(1)" do | ||
assert_selector "button.Polaris-Tabs__Tab.Polaris-Tabs__Tab--selected" do | ||
assert_selector "span.Polaris-Tabs__Title", text: "Active" | ||
end | ||
end | ||
assert_selector "li:nth-of-type(2)" do | ||
assert_selector %(a.Polaris-Tabs__Tab[href="https://shopify.dev"]), text: "With URL" | ||
end | ||
end | ||
end | ||
|
||
def test_fitted_tabs | ||
render_inline(Polaris::TabsComponent.new(fitted: true)) do |tabs| | ||
tabs.tab(title: "Default") | ||
end | ||
|
||
assert_selector "ul.Polaris-Tabs.Polaris-Tabs--fitted" | ||
end | ||
|
||
def test_tabs_with_badge | ||
render_inline(Polaris::TabsComponent.new(fitted: true)) do |tabs| | ||
tabs.tab(title: "Default") do |tab| | ||
tab.badge { "100" } | ||
end | ||
end | ||
|
||
assert_selector ".Polaris-Tabs__Tab > span.Polaris-Tabs__Title" do | ||
assert_text "Default" | ||
assert_selector "span.Polaris-Badge", text: 100 | ||
end | ||
end | ||
end |