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 #185 from primer/btn-group-component
ButtonGroup component
- Loading branch information
Showing
12 changed files
with
137 additions
and
2 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 |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
**/*.stories.json | ||
**/node_modules/ | ||
*.log | ||
*.orig | ||
demo/public/packs/ | ||
demo/tmp/ | ||
demo/config/master.key | ||
|
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,5 @@ | ||
<%= render Primer::BaseComponent.new(**@system_arguments) do %> | ||
<% buttons.each do |button| %> | ||
<%= button %> | ||
<% 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,34 @@ | ||
# frozen_string_literal: true | ||
|
||
module Primer | ||
# Use ButtonGroupComponent to render a series of buttons. | ||
class ButtonGroupComponent < Primer::Component | ||
include ViewComponent::SlotableV2 | ||
|
||
renders_many :buttons, ->(**kwargs) { Primer::ButtonComponent.new(group_item: true, **kwargs) } | ||
|
||
# @example 50|Default | ||
# <%= render(Primer::ButtonGroupComponent.new) do |component| | ||
# component.button { "Default" } | ||
# component.button(button_type: :primary) { "Primary" } | ||
# component.button(button_type: :danger) { "Danger" } | ||
# component.button(button_type: :outline) { "Outline" } | ||
# component.button(classes: "my-class") { "Custom class" } | ||
# end %> | ||
# | ||
# @param system_arguments [Hash] <%= link_to_system_arguments_docs %> | ||
def initialize(**system_arguments) | ||
@system_arguments = system_arguments | ||
@system_arguments[:tag] ||= :div | ||
|
||
@system_arguments[:classes] = class_names( | ||
"BtnGroup", | ||
system_arguments[:classes] | ||
) | ||
end | ||
|
||
def render? | ||
buttons.any? | ||
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
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,31 @@ | ||
--- | ||
title: ButtonGroup | ||
status: Experimental | ||
source: https://github.com/primer/view_components/tree/main/app/components/primer/button_group_component.rb | ||
--- | ||
|
||
<!-- Warning: AUTO-GENERATED file, do not edit. Add code comments to your Ruby instead <3 --> | ||
|
||
Use ButtonGroupComponent to render a series of buttons. | ||
|
||
## Examples | ||
|
||
### Default | ||
|
||
<iframe style="width: 100%; border: 0px; height: 50px;" srcdoc="<html><head><link href='https://unpkg.com/@primer/css/dist/primer.css' rel='stylesheet'></head><body><div class='BtnGroup '> <button type='button' class='btn BtnGroup-item '>Default</button> <button type='button' class='btn BtnGroup-item btn-primary '>Primary</button> <button type='button' class='btn BtnGroup-item btn-danger '>Danger</button> <button type='button' class='btn BtnGroup-item btn-outline '>Outline</button> <button type='button' class='btn BtnGroup-item my-class '>Custom class</button></div></body></html>"></iframe> | ||
|
||
```erb | ||
<%= render(Primer::ButtonGroupComponent.new) do |component| | ||
component.button { "Default" } | ||
component.button(button_type: :primary) { "Primary" } | ||
component.button(button_type: :danger) { "Danger" } | ||
component.button(button_type: :outline) { "Outline" } | ||
component.button(classes: "my-class") { "Custom class" } | ||
end %> | ||
``` | ||
|
||
## Arguments | ||
|
||
| Name | Type | Default | Description | | ||
| :- | :- | :- | :- | | ||
| `system_arguments` | `Hash` | N/A | [System arguments](/system-arguments) | |
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,14 @@ | ||
# frozen_string_literal: true | ||
|
||
class Primer::ButtonGroupComponentStories < ViewComponent::Storybook::Stories | ||
layout "storybook_preview" | ||
|
||
story(:button_group) do | ||
content do |c| | ||
c.button { "Button" } | ||
c.button(button_type: :primary) { "Primary" } | ||
c.button(button_type: :danger) { "Danger" } | ||
c.button(button_type: :outline) { "Outline" } | ||
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,45 @@ | ||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
|
||
class PrimerButtonGroupComponentTest < Minitest::Test | ||
include Primer::ComponentTestHelpers | ||
|
||
def test_does_not_render_without_buttons | ||
render_inline(Primer::ButtonGroupComponent.new) | ||
|
||
refute_selector("div.BtnGroup") | ||
end | ||
|
||
def test_renders_button_items | ||
render_inline(Primer::ButtonGroupComponent.new) { |c| c.button { "Button" } } | ||
|
||
assert_selector("div.BtnGroup") do | ||
assert_selector("button.btn.BtnGroup-item", text: "Button") | ||
end | ||
end | ||
|
||
def test_renders_button_with_props | ||
render_inline(Primer::ButtonGroupComponent.new) do |c| | ||
c.button { "Button" } | ||
c.button(button_type: :primary) { "Primary" } | ||
c.button(button_type: :danger) { "Danger" } | ||
c.button(button_type: :outline) { "Outline" } | ||
c.button(classes: "my-class") { "Custom class" } | ||
end | ||
|
||
assert_selector("div.BtnGroup") do | ||
assert_selector("button.btn.BtnGroup-item", text: "Button") | ||
assert_selector("button.btn.BtnGroup-item.btn-primary", text: "Primary") | ||
assert_selector("button.btn.BtnGroup-item.btn-danger", text: "Danger") | ||
assert_selector("button.btn.BtnGroup-item.btn-outline", text: "Outline") | ||
assert_selector("button.btn.BtnGroup-item.my-class", text: "Custom class") | ||
end | ||
end | ||
|
||
def test_does_not_render_content | ||
render_inline(Primer::ButtonGroupComponent.new) { "content" } | ||
|
||
refute_text("content") | ||
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
d464d1d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: