Skip to content

Commit

Permalink
Merge pull request #118 from forsbergplustwo/tag_component
Browse files Browse the repository at this point in the history
  • Loading branch information
Kirill Platonov authored Sep 6, 2021
2 parents 6b76761 + 9c6a4f2 commit 604b6ea
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ GEM
nio4r (2.5.8)
nokogiri (1.12.1-arm64-darwin)
racc (~> 1.4)
nokogiri (1.12.1-x86_64-darwin)
racc (~> 1.4)
nokogiri (1.12.1-x86_64-linux)
racc (~> 1.4)
pry (0.14.1)
Expand Down Expand Up @@ -160,6 +162,7 @@ GEM

PLATFORMS
arm64-darwin-20
x86_64-darwin-20
x86_64-linux

DEPENDENCIES
Expand Down
6 changes: 6 additions & 0 deletions app/components/polaris/tag_component.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<%= render Polaris::BaseComponent.new(**system_arguments) do %>
<span class="Polaris-TagText">
<%= content %>
</span>
<%= remove_button %>
<% end %>
35 changes: 35 additions & 0 deletions app/components/polaris/tag_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

module Polaris
class TagComponent < Polaris::NewComponent
renders_one :remove_button, lambda { |**system_arguments|
render Polaris::BaseButton.new(
classes: "Polaris-Tag__Button",
disabled: @disabled,
**system_arguments
) do |button|
polaris_icon(name: "CancelSmallMinor")
end
}

def initialize(clickable: false, disabled: false, **system_arguments)
@clickable = clickable
@disabled = disabled

@system_arguments = system_arguments
end

def system_arguments
@system_arguments.tap do |opts|
opts[:tag] = @clickable ? "button" : "span"
opts[:classes] = class_names(
@system_arguments[:classes],
"Polaris-Tag",
"Polaris-Tag--clickable" => @clickable,
"Polaris-Tag--disabled" => @disabled,
"Polaris-Tag--removable" => remove_button.present?
)
end
end
end
end
1 change: 1 addition & 0 deletions app/helpers/polaris/view_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ module ViewHelper
subheading: "Polaris::SubheadingComponent",
spinner: "Polaris::SpinnerComponent",
skeleton_body_text: "Polaris::SkeletonBodyTextComponent",
tag: "Polaris::TagComponent",
text_container: "Polaris::TextContainerComponent",
text_field: "Polaris::TextFieldComponent",
text_style: "Polaris::TextStyleComponent",
Expand Down
13 changes: 13 additions & 0 deletions demo/test/components/previews/forms/tag_component_preview.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Forms::TagComponentPreview < ViewComponent::Preview
def default
end

def disabled
end

def with_remove_button
end

def clickable
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= polaris_tag(clickable: true) { "Rustic" } %>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= polaris_tag { "Rustic" } %>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= polaris_tag(disabled: true) { "Rustic" } %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<%= polaris_tag do |tag| %>
<% tag.remove_button(data: {}) %>
Rustic
<% end %>
39 changes: 39 additions & 0 deletions test/components/polaris/tag_component_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require "test_helper"

class ButtonComponentTest < Minitest::Test
include Polaris::ComponentTestHelpers

def test_default_tag
render_inline(Polaris::TagComponent.new) { "Content" }

assert_selector "span.Polaris-Tag" do
assert_selector "span.Polaris-TagText", text: "Content"
end
end

def test_disabled_tag
render_inline(Polaris::TagComponent.new(disabled: true)) { "Content" }
assert_selector "button.Polaris-Tag.Polaris-Tag--clickable"
end

def test_disabled_tag
render_inline(Polaris::TagComponent.new(disabled: true)) { "Content" }

assert_selector "span.Polaris-Tag.Polaris-Tag--disabled"
end

def test_with_removable_button
render_inline Polaris::TagComponent.new do |tag|
tag.remove_button(data: {})
"Content"
end

assert_selector "span.Polaris-Tag.Polaris-Tag--removable" do
assert_selector "button.Polaris-Tag__Button" do
assert_selector "span.Polaris-Icon"
end
assert_selector "span.Polaris-TagText", text: "Content"
end
end

end

0 comments on commit 604b6ea

Please sign in to comment.