Skip to content

Commit f53f7e4

Browse files
authored
Merge pull request #495 from opf/feature/border-box-list-arguments
Teach BorderBox component a `list_arguments` param
2 parents 2c1af16 + 58b6dfe commit f53f7e4

6 files changed

Lines changed: 90 additions & 10 deletions

File tree

.changeset/rare-coats-hide.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@openproject/primer-view-components': patch
3+
---
4+
5+
Teach BorderBox component a list_arguments param

app/components/primer/beta/border_box.rb

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,12 @@ class BorderBox < Primer::Component
7171
}
7272

7373
# @param padding [Symbol] <%= one_of(Primer::Beta::BorderBox::PADDING_MAPPINGS.keys) %>
74+
# @param list_arguments [Hash] <%= link_to_system_arguments_docs %>
75+
# @param list_id [String] Deprecated. Use <code>list_arguments: { id: ... }</code> instead.
7476
# @param system_arguments [Hash] <%= link_to_system_arguments_docs %>
75-
def initialize(padding: DEFAULT_PADDING, **system_arguments)
77+
def initialize(padding: DEFAULT_PADDING, list_arguments: {}, **system_arguments)
7678
list_id = system_arguments.delete(:list_id)
79+
deprecation_warn("The `list_id:` param is deprecated. Use `list_arguments: { id: ... }` instead. It will be removed in a future version.") if list_id
7780

7881
@system_arguments = deny_tag_argument(**system_arguments)
7982
@system_arguments[:tag] = :div
@@ -84,9 +87,10 @@ def initialize(padding: DEFAULT_PADDING, **system_arguments)
8487
)
8588

8689
@system_arguments[:system_arguments_denylist] = { [:p, :pt, :pb, :pr, :pl] => PADDING_SUGGESTION }
87-
@list_arguments = { tag: :ul }
88-
@list_arguments[:id] = list_id if list_id
89-
@list_arguments[:classes] = "Box-list"
90+
@list_arguments = deny_tag_argument(**list_arguments)
91+
@list_arguments[:tag] = :ul
92+
@list_arguments[:id] ||= list_id if list_id
93+
@list_arguments[:classes] = class_names("Box-list", @list_arguments[:classes])
9094
end
9195

9296
def render?
@@ -98,9 +102,16 @@ def render?
98102
def before_render
99103
return unless header
100104

101-
@list_arguments[:aria] = {
102-
labelledby: header.id
103-
}
105+
@list_arguments[:aria] = merge_aria(
106+
@list_arguments,
107+
{ aria: { labelledby: header.id } }
108+
)
109+
end
110+
111+
def deprecation_warn(message)
112+
return if Rails.env.production? || silence_deprecations?
113+
114+
::Primer::ViewComponents.deprecation.warn(message)
104115
end
105116
end
106117
end

previews/primer/beta/border_box_preview.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ module Beta
66
class BorderBoxPreview < ViewComponent::Preview
77
# @label Playground
88
#
9+
# @param list_id text
910
# @param padding [Symbol] select [default, condensed, spacious]
1011
# @param scheme [Symbol] select [default, neutral, info, warning]
11-
# @param list_id [String] text
12-
def playground(padding: :default, scheme: :default, list_id: nil)
13-
render(Primer::Beta::BorderBox.new(padding: padding, list_id: list_id)) do |component|
12+
def playground(padding: :default, scheme: :default, list_id: "my-list")
13+
render(Primer::Beta::BorderBox.new(padding: padding, list_arguments: { id: list_id })) do |component|
1414
component.with_header { "Header" }
1515
component.with_body { "Body" }
1616
component.with_row(scheme: scheme) { "#{scheme.to_s.capitalize} row one" }

static/arguments.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4250,6 +4250,18 @@
42504250
"default": "`:default`",
42514251
"description": "One of `:condensed`, `:default`, or `:spacious`."
42524252
},
4253+
{
4254+
"name": "list_arguments",
4255+
"type": "Hash",
4256+
"default": "`{}`",
4257+
"description": "[System arguments](/system-arguments)"
4258+
},
4259+
{
4260+
"name": "list_id",
4261+
"type": "String",
4262+
"default": "N/A",
4263+
"description": "Deprecated. Use <code>list_arguments: { id: ... }</code> instead."
4264+
},
42534265
{
42544266
"name": "system_arguments",
42554267
"type": "Hash",

static/info_arch.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13509,6 +13509,18 @@
1350913509
"default": "`:default`",
1351013510
"description": "One of `:condensed`, `:default`, or `:spacious`."
1351113511
},
13512+
{
13513+
"name": "list_arguments",
13514+
"type": "Hash",
13515+
"default": "`{}`",
13516+
"description": "{{link_to_system_arguments_docs}}"
13517+
},
13518+
{
13519+
"name": "list_id",
13520+
"type": "String",
13521+
"default": "N/A",
13522+
"description": "Deprecated. Use <code>list_arguments: { id: ... }</code> instead."
13523+
},
1351213524
{
1351313525
"name": "system_arguments",
1351413526
"type": "Hash",

test/components/beta/border_box_test.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,28 @@ def test_labels_list_with_header
3333
assert_selector "ul[aria-labelledby='#{id}']"
3434
end
3535

36+
def test_renders_list_with_list_arguments
37+
render_inline(Primer::Beta::BorderBox.new(list_arguments: { id: "fake-tbody", role: "rowgroup" })) do |component|
38+
component.with_row { "Row" }
39+
end
40+
41+
assert_selector "ul#fake-tbody"
42+
assert_selector 'ul[role="rowgroup"]'
43+
end
44+
45+
def test_labels_list_with_header_and_additional_label
46+
render_inline(Primer::Beta::BorderBox.new(list_arguments: { aria: { labelledby: "my-footer" } })) do |component|
47+
component.with_header { "Header" }
48+
component.with_row { "Row" }
49+
component.with_footer(id: "my-footer") { "Footer" }
50+
end
51+
52+
header_id = page.find_css(".Box-header").first[:id]
53+
footer_id = page.find_css(".Box-footer").first[:id]
54+
assert_selector "ul[aria-labelledby*='#{header_id}']"
55+
assert_selector "ul[aria-labelledby*='#{footer_id}']"
56+
end
57+
3658
def test_renders_body
3759
render_inline(Primer::Beta::BorderBox.new) do |component|
3860
component.with_body { "Body" }
@@ -69,6 +91,24 @@ def test_renders_the_list_element_with_an_id_if_provided
6991
assert_selector("li.Box-row", count: 1)
7092
end
7193

94+
def test_warns_when_list_id_param_passed
95+
with_silence_deprecations(false) do
96+
::Primer::ViewComponents.deprecation.expects(:warn).with("The `list_id:` param is deprecated. Use `list_arguments: { id: ... }` instead. It will be removed in a future version.").once
97+
render_inline(Primer::Beta::BorderBox.new(list_id: "an-id")) do |component|
98+
component.with_row { "First" }
99+
end
100+
end
101+
end
102+
103+
def test_list_arguments_id_takes_precedence_over_deprecated_list_id
104+
render_inline(Primer::Beta::BorderBox.new(list_id: "old-id", list_arguments: { id: "new-id" })) do |component|
105+
component.with_row { "First" }
106+
end
107+
108+
assert_selector("ul#new-id", count: 1)
109+
assert_no_selector("ul#old-id")
110+
end
111+
72112
def test_renders_condensed
73113
render_inline(Primer::Beta::BorderBox.new(padding: :condensed)) do |component|
74114
component.with_body { "Body" }

0 commit comments

Comments
 (0)