Skip to content

Commit

Permalink
feat: create LabelComponent
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-brousse committed Oct 18, 2023
1 parent f1aacca commit 186405c
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
26 changes: 26 additions & 0 deletions app/components/label_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

class LabelComponent < ApplicationComponent
TYPES = %i[default primary success info warning danger]

erb_template <<~ERB
<span class="<%= css_classes %>">
<%= content %>
</span>
ERB

def initialize(text = nil, type: :default)
@text = text

@type = type.to_sym
raise ArgumentError, "'#{@type.inspect}' is not a valid type" unless TYPES.include?(@type)
end

def css_classes
"label label-#{@type}"
end

def content
super || @text
end
end
29 changes: 29 additions & 0 deletions spec/components/label_component_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe LabelComponent, type: :component do
context "with html content" do
let(:component) { described_class.new(text, **kwargs) }
let(:rendered_component) { render_inline(component, &block) }
let(:text) { "Text as argument" }
let(:kwargs) { {} }
let(:block) {}

context "with text" do
it { expect(rendered_component.to_html).to have_tag("span.label.label-default", text: /Text as argument/) }
end

context "with text as block" do
let(:block) { proc { "Text as block" } }

it { expect(rendered_component.to_html).to have_tag("span.label.label-default", text: /Text as block/) }
end

context "with type" do
let(:kwargs) { { type: :success } }

it { expect(rendered_component.to_html).to have_tag("span.label.label-success", text: /Text as argument/) }
end
end
end

0 comments on commit 186405c

Please sign in to comment.