Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SelectPanel] Disallow passing role: argument #3054

Merged
merged 7 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading