Skip to content

Commit

Permalink
[SelectPanel] Disallow passing role: argument (#3054)
Browse files Browse the repository at this point in the history
  • Loading branch information
camertron authored Sep 9, 2024
1 parent 96b42db commit 4434ec0
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/chilly-berries-retire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/view-components': patch
---

[SelectPanel] Disallow passing `role:` argument
35 changes: 31 additions & 4 deletions app/components/primer/alpha/select_panel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -250,10 +250,28 @@ module Alpha
# )
# ```
class SelectPanel < Primer::Component
# @private
module Utils
def raise_if_role_given!(**system_arguments)
return if shouldnt_raise_error?
return unless system_arguments.include?(:role)

raise(
"Please avoid passing the `role:` argument to `SelectPanel` and its subcomponents. "\
"The component will automatically apply the correct roles where necessary."
)
end
end

include Utils

# The component that should be used to render the list of items in the body of a SelectPanel.
class ItemList < Primer::Alpha::ActionList
include Utils

# @param system_arguments [Hash] The arguments accepted by <%= link_to_component(Primer::Alpha::ActionList) %>.
def initialize(**system_arguments)
raise_if_role_given!(**system_arguments)
select_variant = system_arguments[:select_variant] || Primer::Alpha::ActionList::DEFAULT_SELECT_VARIANT

super(
Expand All @@ -263,6 +281,16 @@ def initialize(**system_arguments)
**system_arguments
)
end

def with_item(**system_arguments)
raise_if_role_given!(**system_arguments)
super
end

def with_avatar_item(**system_arguments)
raise_if_role_given!(**system_arguments)
super
end
end

status :alpha
Expand Down Expand Up @@ -362,6 +390,8 @@ def initialize(
anchor_side: Primer::Alpha::Overlay::DEFAULT_ANCHOR_SIDE,
**system_arguments
)
raise_if_role_given!(**system_arguments)

if src.present?
url = URI(src)
query = url.query || ""
Expand Down Expand Up @@ -419,12 +449,9 @@ def initialize(
form_arguments: form_arguments,
id: "#{@panel_id}-list",
select_variant: @select_variant,
role: "listbox",
aria_selection_variant: @select_variant == :multiple ? :checked : :selected,
aria: {
label: "#{title} options"
},
p: 2
}
)
end

Expand Down
4 changes: 4 additions & 0 deletions app/components/primer/component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ def should_raise_error?
!Rails.env.production? && raise_on_invalid_options? && !ENV["PRIMER_WARNINGS_DISABLED"]
end

def shouldnt_raise_error?
!should_raise_error?
end

def should_raise_aria_error?
!Rails.env.production? && raise_on_invalid_aria? && !ENV["PRIMER_WARNINGS_DISABLED"]
end
Expand Down
50 changes: 50 additions & 0 deletions test/components/alpha/select_panel_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,56 @@ def test_renders_close_button
panel_id = page.find_css("select-panel").first.attributes["id"].value
assert_selector "select-panel button[data-close-dialog-id='#{panel_id}-dialog']"
end

def test_raises_if_role_given
with_raise_on_invalid_options(true) do
error = assert_raises do
render_inline(Primer::Alpha::SelectPanel.new(role: :listbox))
end

assert_includes error.message, "Please avoid passing the `role:` argument"
end
end

def test_raises_if_role_given_to_item_slot
with_raise_on_invalid_options(true) do
error = assert_raises do
render_inline(Primer::Alpha::SelectPanel.new) do |panel|
panel.with_item(role: :option)
end
end

assert_includes error.message, "Please avoid passing the `role:` argument"
end
end

def test_does_not_raise_if_no_role_given_to_item_slot
render_inline(Primer::Alpha::SelectPanel.new) do |panel|
panel.with_item(label: "Foo")
end

assert_selector "select-panel"
end

def test_raises_if_role_given_to_avatar_item_slot
with_raise_on_invalid_options(true) do
error = assert_raises do
render_inline(Primer::Alpha::SelectPanel.new) do |panel|
panel.with_avatar_item(src: "camertron.jpg", username: "camertron", role: :option)
end
end

assert_includes error.message, "Please avoid passing the `role:` argument"
end
end

def test_does_not_raise_if_role_not_given_to_avatar_item_slot
render_inline(Primer::Alpha::SelectPanel.new) do |panel|
panel.with_avatar_item(src: "camertron.jpg", username: "camertron")
end

assert_selector(".avatar-small")
end
end
end
end

0 comments on commit 4434ec0

Please sign in to comment.