Skip to content

Commit 4ef3027

Browse files
authored
Merge pull request #466 from opf/feature/select-panel-dynamic-label-multiple
Support dynamic labels for multiple variant `SelectPanel`
2 parents f8c760c + bc5fd8d commit 4ef3027

7 files changed

Lines changed: 82 additions & 8 deletions

File tree

.changeset/afraid-monkeys-unite.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': minor
3+
---
4+
5+
Support dynamic labels for SelectPanel multiple select variant
Loading

app/components/primer/alpha/select_panel_element.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -916,6 +916,8 @@ export class SelectPanelElement extends HTMLElement {
916916
} else {
917917
this.#removeSelectedItem(item)
918918
}
919+
920+
this.#setDynamicLabel()
919921
}
920922

921923
this.#updateInput()
@@ -952,8 +954,15 @@ export class SelectPanelElement extends HTMLElement {
952954
const invokerLabel = this.invokerLabel
953955
if (!invokerLabel) return
954956
this.#originalLabel ||= invokerLabel.textContent || ''
955-
const itemLabel =
956-
this.querySelector(`[${this.ariaSelectionType}=true] .ActionListItem-label`)?.textContent || this.#originalLabel
957+
let itemLabel: string | undefined
958+
if (this.selectVariant === 'single') {
959+
itemLabel = this.querySelector(`[${this.ariaSelectionType}=true] .ActionListItem-label`)?.textContent
960+
} else if (this.selectVariant === 'multiple') {
961+
itemLabel = Array.from(this.querySelectorAll(`[${this.ariaSelectionType}=true] .ActionListItem-label`))
962+
.map(label => label.textContent?.trim() ?? '')
963+
.join(', ')
964+
}
965+
itemLabel ||= this.#originalLabel
957966
if (itemLabel) {
958967
const prefixSpan = document.createElement('span')
959968
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
@@ -144,15 +144,25 @@ def multiselect(open_on_load: false)
144144
# @label With dynamic label
145145
#
146146
# @param open_on_load toggle
147-
def with_dynamic_label(open_on_load: false)
148-
render_with_template(locals: { open_on_load: open_on_load })
147+
# @param select_variant [Symbol] select [single, multiple]
148+
def with_dynamic_label(open_on_load: false, select_variant: :single)
149+
render_with_template(locals: {
150+
open_on_load: open_on_load,
151+
# .to_sym workaround for https://github.com/lookbook-hq/lookbook/issues/640
152+
select_variant: select_variant.to_sym
153+
})
149154
end
150155

151156
# @label With dynamic label and aria prefix
152157
#
153158
# @param open_on_load toggle
154-
def with_dynamic_label_and_aria_prefix(open_on_load: false)
155-
render_with_template(locals: { open_on_load: open_on_load })
159+
# @param select_variant [Symbol] select [single, multiple]
160+
def with_dynamic_label_and_aria_prefix(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+
})
156166
end
157167

158168
# @!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: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,56 @@ def test_banner_scheme_is_passed_to_banner_component
737737
assert_selector "[data-target='select-panel.bannerErrorElement'] .Banner--warning", text: "Sorry, something went wrong"
738738
end
739739

740+
def test_dynamic_label_for_single_select_variant
741+
visit_preview(:with_dynamic_label, select_variant: :single)
742+
743+
click_on_invoker_button
744+
745+
assert_selector "select-panel button[aria-controls]", exact_text: "Item: Choose item"
746+
747+
click_on_second_item
748+
749+
assert_selector "select-panel button[aria-controls]", exact_text: "Item: Item 2"
750+
end
751+
752+
def test_dynamic_label_for_multiple_select_variant
753+
visit_preview(:with_dynamic_label, select_variant: :multiple)
754+
755+
click_on_invoker_button
756+
757+
assert_selector "select-panel button[aria-controls]", exact_text: "Item: Choose item"
758+
759+
click_on_second_item
760+
click_on_third_item
761+
762+
assert_selector "select-panel button[aria-controls]", exact_text: "Item: Item 2, Item 3"
763+
end
764+
765+
def test_dynamic_label_and_aria_prefix_for_single_select_variant
766+
visit_preview(:with_dynamic_label_and_aria_prefix, select_variant: :single)
767+
768+
click_on_invoker_button
769+
770+
assert_selector "select-panel button[aria-controls][aria-label='Your item: Choose item']"
771+
772+
click_on_second_item
773+
774+
assert_selector "select-panel button[aria-controls][aria-label='Your item: Item 2']"
775+
end
776+
777+
def test_dynamic_label_and_aria_prefix_for_multiple_select_variant
778+
visit_preview(:with_dynamic_label_and_aria_prefix, select_variant: :multiple)
779+
780+
click_on_invoker_button
781+
782+
assert_selector "select-panel button[aria-controls][aria-label='Your item: Choose item']"
783+
784+
click_on_second_item
785+
click_on_third_item
786+
787+
assert_selector "select-panel button[aria-controls][aria-label='Your item: Item 2, Item 3']"
788+
end
789+
740790
########## JAVASCRIPT API TESTS ############
741791

742792
def test_disable_item_via_js_api

0 commit comments

Comments
 (0)