@@ -66,29 +66,48 @@ const SelectedItemsGroup = ({
6666 [ onDeselectAll , disabled ] ,
6767 ) ;
6868
69+ const hasNoItems = selectedOptions . length === 0 ;
70+
71+ // Collapse the group when no items are selected
72+ useEffect ( ( ) => {
73+ if ( hasNoItems && expanded ) {
74+ onToggleExpand ( ) ;
75+ }
76+ } , [ hasNoItems , expanded , onToggleExpand ] ) ;
77+
6978 return (
7079 < div className = { styles . selectedItemsGroup } >
7180 { /* Header - Always visible */ }
7281 < button
7382 type = "button"
7483 className = { styles . selectedItemsHeader }
75- onClick = { onToggleExpand }
84+ onClick = { hasNoItems ? undefined : onToggleExpand }
7685 aria-expanded = { expanded }
7786 aria-label = { `Selected items group, ${ selectedOptions . length } items selected` }
87+ disabled = { hasNoItems }
88+ style = { { cursor : hasNoItems ? "default" : "pointer" } }
7889 >
7990 { /* Caret icon */ }
8091 { expanded ? (
81- < IconChevron className = { styles . selectedItemsCaret } aria-hidden = "true" />
92+ < IconChevron
93+ className = { styles . selectedItemsCaret }
94+ aria-hidden = "true"
95+ style = { { opacity : hasNoItems ? 0.3 : 1 } }
96+ />
8297 ) : (
83- < IconChevronDown className = { styles . selectedItemsCaret } aria-hidden = "true" />
98+ < IconChevronDown
99+ className = { styles . selectedItemsCaret }
100+ aria-hidden = "true"
101+ style = { { opacity : hasNoItems ? 0.3 : 1 } }
102+ />
84103 ) }
85104
86105 { /* Deselect all checkbox */ }
87106 < Checkbox
88107 tabIndex = { - 1 }
89- checked = { true }
108+ checked = { selectedOptions . length > 0 }
90109 readOnly
91- disabled = { disabled }
110+ disabled = { disabled || selectedOptions . length === 0 }
92111 onClick = { handleDeselectAllClick }
93112 aria-label = "Deselect all items"
94113 />
@@ -105,25 +124,29 @@ const SelectedItemsGroup = ({
105124 { /* Content - Conditionally rendered when expanded */ }
106125 { expanded && (
107126 < div className = { styles . selectedItemsContent } >
108- { selectedOptions . map ( ( option , index ) => {
109- const optionValue = option ?. value ?? option ;
110- const label = option ?. label ?? optionValue ;
111-
112- return (
113- < button
114- key = { `selected-${ optionValue } -${ index } ` }
115- type = "button"
116- className = { styles . selectedItem }
117- onClick = { ( ) => handleItemClick ( option ) }
118- tabIndex = { disabled ? - 1 : 0 }
119- aria-label = { `Deselect ${ label } ` }
120- disabled = { disabled }
121- >
122- < Checkbox tabIndex = { - 1 } checked = { true } readOnly disabled = { disabled } />
123- < div className = "w-full min-w-0 truncate" > { label } </ div >
124- </ button >
125- ) ;
126- } ) }
127+ { selectedOptions . length > 0 ? (
128+ < CommandGroup >
129+ { selectedOptions . map ( ( option , index ) => {
130+ const optionValue = option ?. value ?? option ;
131+ const label = option ?. label ?? optionValue ;
132+
133+ return (
134+ < Option
135+ key = { `selected-${ optionValue } -${ index } ` }
136+ value = { optionValue }
137+ label = { label }
138+ isOptionSelected = { true }
139+ disabled = { disabled }
140+ multiple = { true }
141+ onSelect = { ( ) => handleItemClick ( option ) }
142+ style = { { paddingLeft : "var(--spacing-wider)" } }
143+ />
144+ ) ;
145+ } ) }
146+ </ CommandGroup >
147+ ) : (
148+ < div className = "px-base py-tight text-neutral-content-subtler text-center" > No items selected</ div >
149+ ) }
127150 </ div >
128151 ) }
129152 </ div >
@@ -199,6 +222,7 @@ export const Select = forwardRef(
199222 onClose,
200223 onOpen,
201224 footer,
225+ alwaysShowSelectedGroup = false ,
202226 ...props
203227 } : SelectProps < T , A > ,
204228 _ref : ForwardedRef < HTMLSelectElement > ,
@@ -512,7 +536,7 @@ export const Select = forwardRef(
512536 } ) }
513537 >
514538 { /* Selected Items Group - Only for multiple + searchable + virtual lists */ }
515- { multiple && searchable && isVirtualList && selectedOptions . length > 0 && (
539+ { multiple && searchable && isVirtualList && ( selectedOptions . length > 0 || alwaysShowSelectedGroup ) && (
516540 < SelectedItemsGroup
517541 expanded = { selectedGroupExpanded }
518542 onToggleExpand = { ( ) => setSelectedGroupExpanded ( ! selectedGroupExpanded ) }
0 commit comments