-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbutton_group_component.rb
More file actions
67 lines (60 loc) · 2.14 KB
/
Copy pathbutton_group_component.rb
File metadata and controls
67 lines (60 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# frozen_string_literal: true
module Strata
module US
# ButtonGroupComponent renders a USWDS button group: a <ul class="usa-button-group">
# whose children are <li class="usa-button-group__item"> wrappers containing
# whatever button-styled element the caller chooses (a Strata::US::ButtonComponent,
# a link_to, an f.submit, etc.).
#
# See https://designsystem.digital.gov/components/button-group/.
#
# @example Default
# <%= render Strata::US::ButtonGroupComponent.new do |group| %>
# <% group.with_item do %>
# <%= render Strata::US::ButtonComponent.new do %>Save<% end %>
# <% end %>
# <% group.with_item do %>
# <%= link_to "Cancel", cancel_path,
# class: Strata::US::ButtonComponent.css_classes(variant: :outline) %>
# <% end %>
# <% end %>
#
# @example Segmented
# <%= render Strata::US::ButtonGroupComponent.new(segmented: true) do |group| %>
# <% group.with_item { ... } %>
# <% group.with_item { ... } %>
# <% end %>
class ButtonGroupComponent < ViewComponent::Base
renders_many :items, "Strata::US::ButtonGroupComponent::ItemComponent"
def initialize(segmented: false, classes: nil, **html_attributes)
@segmented = segmented
@classes = classes
@html_attributes = html_attributes
end
def wrapper_classes
class_names(
"usa-button-group",
{ "usa-button-group--segmented" => @segmented },
@classes
)
end
def list_attributes
attrs = @html_attributes.dup
attrs[:class] = wrapper_classes
attrs
end
# ItemComponent renders one entry as an <li class="usa-button-group__item">.
class ItemComponent < ViewComponent::Base
def initialize(classes: nil, **html_attributes)
@classes = classes
@html_attributes = html_attributes
end
def call
attrs = @html_attributes.dup
attrs[:class] = class_names("usa-button-group__item", @classes)
content_tag(:li, content, **attrs)
end
end
end
end
end