Skip to content

Commit

Permalink
Do not populate dynamic label with active item in multi-select mode (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
camertron authored Jan 31, 2025
1 parent a2af88f commit 7fbd8da
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/stale-fishes-begin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/view-components': patch
---

Do not populate dynamic label with active item in multi-select mode
10 changes: 10 additions & 0 deletions app/components/primer/alpha/action_menu/action_menu_element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,12 @@ export class ActionMenuElement extends HTMLElement {
() => Boolean(this.invokerElement),
() => this.#intersectionObserver.observe(this.invokerElement!),
)

// If there's no include fragment, then no async fetching will occur and we can
// mark the component as ready.
if (!this.includeFragment) {
this.setAttribute('data-ready', 'true')
}
}

disconnectedCallback() {
Expand Down Expand Up @@ -367,6 +373,9 @@ export class ActionMenuElement extends HTMLElement {
#handleIncludeFragmentReplaced() {
if (this.#firstItem) this.#firstItem.focus()
this.#softDisableItems()

// async items have loaded, so component is ready
this.setAttribute('data-ready', 'true')
}

// Close when focus leaves menu
Expand All @@ -387,6 +396,7 @@ export class ActionMenuElement extends HTMLElement {
}

#setDynamicLabel() {
if (this.selectVariant !== 'single') return
if (!this.dynamicLabel) return
const invokerLabel = this.invokerLabel
if (!invokerLabel) return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<% menu.with_show_button { "Strategy" } %>
<% menu.with_item(label: "Fast forward", data: { value: "fast_forward" }) %>
<% menu.with_item(label: "Recursive", data: { value: "recursive" }) %>
<% menu.with_item(label: "Ours", data: { value: "ours" }) %>
<% menu.with_item(label: "Ours", data: { value: "ours" }, active: true) %>
<% menu.with_item(label: "Resolve") %>
<% end %>
<hr>
Expand Down
13 changes: 11 additions & 2 deletions test/system/alpha/action_menu_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,9 @@ def test_multiple_select_form_submission

# for some reason the JSON response is wrapped in HTML, I have no idea why
response = JSON.parse(find("pre").text)
assert_equal %w[fast_forward recursive], response["value"]

# "ours" is pre-selected
assert_equal %w[fast_forward recursive ours], response["value"]
end

def test_multiple_select_form_uses_label_if_no_value_provided
Expand All @@ -453,7 +455,14 @@ def test_multiple_select_form_uses_label_if_no_value_provided

# for some reason the JSON response is wrapped in HTML, I have no idea why
response = JSON.parse(find("pre").text)
assert_equal %w[fast_forward Resolve], response["value"]

# "ours" is pre-selected
assert_equal %w[fast_forward ours Resolve], response["value"]
end

def test_multiple_select_does_not_set_dynamic_label_for_preselected_item
visit_preview(:multiple_select_form, route_format: :json)
assert_selector("action-menu[data-ready='true'] button[aria-controls]", exact_text: "Strategy")
end

def test_individual_items_can_submit_post_requests_via_forms
Expand Down

0 comments on commit 7fbd8da

Please sign in to comment.