Skip to content

Commit eee7e54

Browse files
committed
WIP Add toggle behaviour
1 parent e4601ae commit eee7e54

File tree

12 files changed

+180
-4
lines changed

12 files changed

+180
-4
lines changed
Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,18 @@
1-
<%= render(@border_box) %>
1+
<%=
2+
render(@border_box) do |box|
3+
box.with_header do
4+
render(Primer::BaseComponent.new(
5+
tag: "collapsible-border-box-trigger",
6+
data: {
7+
action: "click:collapsible-border-box#toggle"
8+
})) do
9+
render(Primer::OpenProject::FlexLayout.new) do |flex|
10+
flex.with_column(mr: 2) { @title }
11+
flex.with_column(mr: 2) { render(Primer::Beta::Counter.new(count: @count)) }
12+
flex.with_column { render(Primer::Beta::Octicon.new(icon: "chevron-up", classes: "up-icon")) }
13+
flex.with_column { render(Primer::Beta::Octicon.new(icon: "chevron-down", classes: "down-icon", display: :none)) }
14+
end
15+
end
16+
end
17+
end
18+
%>

app/components/primer/open_project/collapsible_border_box.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,14 @@ module OpenProject
55
class CollapsibleBorderBox < Primer::Component
66
status :open_project
77

8+
# renders_one :header, lambda { |title:, count: nil, **system_arguments|
9+
# Primer::OpenProject::CollapsibleBorderBox::Header.new(title: title, count: count, **system_arguments)
10+
# }
11+
812
# @param system_arguments [Hash] <%= link_to_system_arguments_docs %>
9-
def initialize(**system_arguments)
13+
def initialize(title:, count: nil, **system_arguments)
14+
@title = title
15+
@count = count
1016
@system_arguments = system_arguments
1117

1218
@border_box = Primer::Beta::BorderBox.new(**@system_arguments)
@@ -20,6 +26,8 @@ def initialize(**system_arguments)
2026
private
2127

2228
def before_render
29+
30+
2331
content
2432
end
2533
end
Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,63 @@
1-
import '@github/catalyst'
1+
import {controller, target} from '@github/catalyst'
2+
3+
@controller
4+
class CollapsibleBorderBoxTriggerElement extends HTMLElement {
5+
container = null
6+
collapsed = false // Needs to be retrieved from the component call, to be set-able externally
7+
8+
// eslint-disable-next-line custom-elements/no-constructor
9+
constructor() {
10+
super()
11+
12+
this.container = this.closest('.Box')
13+
14+
// Only to test as I couldn't get the action to trigger on click
15+
setTimeout(() => {
16+
this.toggle()
17+
}, 1000)
18+
}
19+
20+
public toggle() {
21+
if (this.collapsed) {
22+
this.collapsed = false
23+
this.expandAll()
24+
} else {
25+
this.collapsed = true
26+
this.hideAll()
27+
}
28+
}
29+
30+
private hideAll() {
31+
const rows = this.container.querySelectorAll('.Box-row, .Box-body, .Box-footer')
32+
33+
rows.forEach(row => {
34+
row.style.display = 'none';
35+
})
36+
37+
this.container.querySelector('.down-icon').style.display = 'block'
38+
this.container.querySelector('.up-icon').style.display = 'none'
39+
}
40+
41+
private expandAll() {
42+
const rows = this.container.querySelectorAll('.Box-row, .Box-body, .Box-footer')
43+
44+
rows.forEach(row => {
45+
row.style.display = 'block';
46+
})
47+
48+
this.container.querySelector('.down-icon').style.display = 'none'
49+
this.container.querySelector('.up-icon').style.display = 'block'
50+
51+
}
52+
}
53+
54+
declare global {
55+
interface Window {
56+
CollapsibleBorderBoxTriggerElement: typeof CollapsibleBorderBoxTriggerElement
57+
}
58+
}
59+
60+
if (!window.customElements.get('collapsible-border-box-trigger')) {
61+
window.CollapsibleBorderBoxTriggerElement = CollapsibleBorderBoxTriggerElement
62+
window.customElements.define('collapsible-border-box-trigger', CollapsibleBorderBoxTriggerElement)
63+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<%= render Primer::BaseComponent.new(**@system_arguments) do %>
2+
<%= @title %>
3+
<%= @count %>
4+
<% end %>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/* CSS for CollapsibleBorderBox::Header */
2+
3+
.CollapsibleBorderBox::Header {
4+
5+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# frozen_string_literal: true
2+
3+
module Primer
4+
module OpenProject
5+
# Add a general description of component here
6+
# Add additional usage considerations or best practices that may aid the user to use the component correctly.
7+
# @accessibility Add any accessibility considerations
8+
class CollapsibleBorderBox::Header < Primer::Component
9+
status :open_project
10+
11+
# @param system_arguments [Hash] <%= link_to_system_arguments_docs %>
12+
def initialize(title:, count: nil, **system_arguments)
13+
puts "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~123"
14+
@title = title
15+
@count = count
16+
@system_arguments = system_arguments
17+
@system_arguments[:tag] = "div"
18+
end
19+
20+
private
21+
22+
def before_render
23+
content
24+
end
25+
end
26+
end
27+
end

app/components/primer/primer.pcss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,4 @@
5050
@import "./open_project/sub_header.pcss";
5151
@import "./open_project/side_panel/section.pcss";
5252
@import "./open_project/collapsible_border_box.pcss";
53+
@import "./open_project/collapsible_border_box/header.pcss";
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# frozen_string_literal: true
2+
3+
# Setup Playground to use all available component props
4+
# Setup Features to use individual component props and combinations
5+
6+
module Primer
7+
module OpenProject
8+
# @label CollapsibleBorderBox::Header
9+
class CollapsibleBorderBox::HeaderPreview < ViewComponent::Preview
10+
# @label Playground
11+
# @param string_example text
12+
# @param boolean_example toggle
13+
# @param email_example email
14+
# @param number_example number
15+
# @param url_example url
16+
# @param tel_example tel
17+
# @param textarea_example textarea
18+
# @param select_example select [one, two, three]
19+
# @param select_custom_labels select [[One label, one], [Two label, two], [Three label, three]]
20+
# With empty option (`~` in YAML)
21+
# @param select_empty_option select [~, one, two, three]
22+
def playground(string_example: "Some value", boolean_example: false, select_example: :one)
23+
render(Primer::OpenProject::CollapsibleBorderBox::Header.new(string_example: string_example, boolean_example: boolean_example, select_example: select_example))
24+
end
25+
end
26+
end
27+
end

previews/primer/open_project/collapsible_border_box_preview.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class CollapsibleBorderBoxPreview < ViewComponent::Preview
1313
# end
1414

1515
def default
16-
render(Primer::OpenProject::CollapsibleBorderBox.new) do |component|
16+
render(Primer::OpenProject::CollapsibleBorderBox.new(title: "Test", count: 2)) do |component|
1717
component.with_body { "Body" }
1818
component.with_row { "Row 1" }
1919
component.with_row { "Row 2" }

test/components/component_test.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class PrimerComponentTest < Minitest::Test
88

99
# Components with any arguments necessary to make them render
1010
COMPONENTS_WITH_ARGS = [
11+
[Primer::OpenProject::CollapsibleBorderBox::Header, {}],
1112
[Primer::OpenProject::CollapsibleBorderBox, {}],
1213
[Primer::OpenProject::Heading, { tag: :h2 }],
1314
[Primer::OpenProject::DangerDialog, { title: "Danger action" }, proc { |component|
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# frozen_string_literal: true
2+
3+
require "components/test_helper"
4+
5+
class PrimerOpenProjectCollapsibleBorderBox::HeaderTest < Minitest::Test
6+
include Primer::ComponentTestHelpers
7+
8+
def test_renders
9+
render_inline(Primer::CollapsibleBorderBox::Header.new)
10+
11+
assert_text("Add a test here")
12+
end
13+
end
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# frozen_string_literal: true
2+
3+
require "system/test_case"
4+
5+
class IntegrationOpenProjectCollapsibleBorderBox::HeaderTest < System::TestCase
6+
def test_renders_component
7+
visit_preview(:default)
8+
9+
assert_selector(".collapsible-border-box/header")
10+
end
11+
end

0 commit comments

Comments
 (0)