Skip to content

Commit 7b94de4

Browse files
committed
Support dynamic labels for multiple SelectPanel
Closes primer#3824
1 parent 47d051d commit 7b94de4

File tree

5 files changed

+79
-11
lines changed

5 files changed

+79
-11
lines changed

app/components/primer/alpha/select_panel_element.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -913,6 +913,8 @@ export class SelectPanelElement extends HTMLElement {
913913
} else {
914914
this.#removeSelectedItem(item)
915915
}
916+
917+
this.#setDynamicLabel()
916918
}
917919

918920
this.#updateInput()
@@ -949,8 +951,14 @@ export class SelectPanelElement extends HTMLElement {
949951
const invokerLabel = this.invokerLabel
950952
if (!invokerLabel) return
951953
this.#originalLabel ||= invokerLabel.textContent || ''
952-
const itemLabel =
953-
this.querySelector(`[${this.ariaSelectionType}=true] .ActionListItem-label`)?.textContent || this.#originalLabel
954+
let itemLabel: string;
955+
if (this.selectVariant === 'single') {
956+
itemLabel = this.querySelector(`[${this.ariaSelectionType}=true] .ActionListItem-label`)?.textContent ?? ''
957+
} else if (this.selectVariant === 'multiple') {
958+
itemLabel = Array.from(this.querySelectorAll(`[${this.ariaSelectionType}=true] .ActionListItem-label`))
959+
.map(label => label.textContent.trim()).join(', ')
960+
}
961+
itemLabel ||= this.#originalLabel
954962
if (itemLabel) {
955963
const prefixSpan = document.createElement('span')
956964
prefixSpan.classList.add('color-fg-muted')

previews/primer/alpha/select_panel_preview.rb

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,16 +156,26 @@ def multiselect(open_on_load: false)
156156
#
157157
# @snapshot interactive
158158
# @param open_on_load toggle
159-
def with_dynamic_label(open_on_load: false)
160-
render_with_template(locals: { open_on_load: open_on_load })
159+
# @param select_variant [Symbol] select [single, multiple]
160+
def with_dynamic_label(open_on_load: false, select_variant: :single)
161+
render_with_template(locals: {
162+
open_on_load: open_on_load,
163+
# .to_sym workaround for https://github.com/lookbook-hq/lookbook/issues/640
164+
select_variant: select_variant.to_sym
165+
})
161166
end
162167

163168
# @label With dynamic label and aria prefix
164169
#
165170
# @snapshot interactive
166171
# @param open_on_load toggle
167-
def with_dynamic_label_and_aria_prefix(open_on_load: false)
168-
render_with_template(locals: { open_on_load: open_on_load })
172+
# @param select_variant [Symbol] select [single, multiple]
173+
def with_dynamic_label_and_aria_prefix(open_on_load: false, select_variant: :single)
174+
render_with_template(locals: {
175+
open_on_load: open_on_load,
176+
# .to_sym workaround for https://github.com/lookbook-hq/lookbook/issues/640
177+
select_variant: select_variant.to_sym
178+
})
169179
end
170180

171181
# @!endgroup

previews/primer/alpha/select_panel_preview/with_dynamic_label.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
data: { interaction_subject: subject_id },
55
id: "with_avatar_items",
66
title: "Select users",
7-
select_variant: :single,
7+
select_variant: select_variant,
88
fetch_strategy: :local,
99
dynamic_label: true,
1010
dynamic_label_prefix: "Item",

previews/primer/alpha/select_panel_preview/with_dynamic_label_and_aria_prefix.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
data: { interaction_subject: subject_id },
55
id: "with_avatar_items",
66
title: "Select users",
7-
select_variant: :single,
7+
select_variant: select_variant,
88
fetch_strategy: :local,
99
dynamic_label: true,
1010
dynamic_label_prefix: "Item",

test/system/alpha/select_panel_test.rb

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,56 @@ def test_banner_scheme_is_passed_to_banner_component
752752
assert_selector "[data-target='select-panel.bannerErrorElement'] .Banner--warning", text: "Sorry, something went wrong"
753753
end
754754

755+
def test_dynamic_label_for_single_select_variant
756+
visit_preview(:with_dynamic_label, select_variant: :single)
757+
758+
click_on_invoker_button
759+
760+
assert_selector "select-panel button[aria-controls]", exact_text: "Item: Choose item"
761+
762+
click_on_second_item
763+
764+
assert_selector "select-panel button[aria-controls]", exact_text: "Item: Item 2"
765+
end
766+
767+
def test_dynamic_label_for_multiple_select_variant
768+
visit_preview(:with_dynamic_label, select_variant: :multiple)
769+
770+
click_on_invoker_button
771+
772+
assert_selector "select-panel button[aria-controls]", exact_text: "Item: Choose item"
773+
774+
click_on_second_item
775+
click_on_third_item
776+
777+
assert_selector "select-panel button[aria-controls]", exact_text: "Item: Item 2, Item 3"
778+
end
779+
780+
def test_dynamic_label_and_aria_prefix_for_single_select_variant
781+
visit_preview(:with_dynamic_label_and_aria_prefix, select_variant: :single)
782+
783+
click_on_invoker_button
784+
785+
assert_selector "select-panel button[aria-controls][aria-label='Your item: Choose item']"
786+
787+
click_on_second_item
788+
789+
assert_selector "select-panel button[aria-controls][aria-label='Your item: Item 2']"
790+
end
791+
792+
def test_dynamic_label_and_aria_prefix_for_multiple_select_variant
793+
visit_preview(:with_dynamic_label_and_aria_prefix, select_variant: :multiple)
794+
795+
click_on_invoker_button
796+
797+
assert_selector "select-panel button[aria-controls][aria-label='Your item: Choose item']"
798+
799+
click_on_second_item
800+
click_on_third_item
801+
802+
assert_selector "select-panel button[aria-controls][aria-label='Your item: Item 2, Item 3']"
803+
end
804+
755805
########## JAVASCRIPT API TESTS ############
756806

757807
def test_disable_item_via_js_api
@@ -1370,7 +1420,7 @@ def test_prevents_events_from_bubbling
13701420
visit_preview(:default)
13711421

13721422
click_on_invoker_button
1373-
1423+
13741424
keyboard.type(:tab)
13751425

13761426
evaluate_multiline_script(<<~JS)
@@ -1389,9 +1439,9 @@ def test_prevents_events_from_bubbling
13891439

13901440
refute_selector "select-panel dialog[open]"
13911441

1392-
keyboard.type("a")
1442+
keyboard.type("a")
13931443

13941444
assert page.evaluate_script("window.bodyKeydownFired")
13951445
end
13961446
end
1397-
end
1447+
end

0 commit comments

Comments
 (0)