@@ -48,6 +48,12 @@ type NestedFilterGroupProps = {
4848 isLive ?: boolean ;
4949} ;
5050
51+ type ChildState = {
52+ expanded : boolean ;
53+ showDistributions ?: boolean ;
54+ hadSelections : boolean ;
55+ } ;
56+
5157const VIRTUAL_ITEM_HEIGHT_ESTIMATE = 26 ;
5258const MAX_VIRTUAL_LIST_HEIGHT = 350 ;
5359
@@ -111,25 +117,21 @@ export const NestedFilterGroup = ({
111117 const entry = selectedValues [ key ] ;
112118 return entry != null && entry . included . size + entry . excluded . size > 0 ;
113119 } ;
114- // Rows unmount when scrolled out of view, so their expanded state lives here.
115- // Like regular filter groups, a key expands when it gains a filter (including
116- // one added outside the sidebar) and keeps its state when it loses one.
117- const [ expandedChildren , setExpandedChildren ] = useState <
118- Record < string , { expanded : boolean ; hadSelections : boolean } >
119- > ( { } ) ;
120- const changedKeys = Object . keys ( {
121- ...selectedValues ,
122- ...expandedChildren ,
123- } ) . filter (
124- key =>
125- ( expandedChildren [ key ] ?. hadSelections ?? false ) !== keyHasSelections ( key ) ,
120+ // Rows unmount when scrolled out of view, so their state lives here. Like
121+ // regular filter groups, a key expands when it gains a filter (including one
122+ // added outside the sidebar) and keeps its state when it loses one.
123+ const [ childState , setChildState ] = useState < Record < string , ChildState > > ( { } ) ;
124+ const updateChildState = ( key : string , patch : Partial < ChildState > ) =>
125+ setChildState ( prev => ( { ...prev , [ key ] : { ...prev [ key ] , ...patch } } ) ) ;
126+ const changedKeys = Object . keys ( { ...selectedValues , ...childState } ) . filter (
127+ key => ( childState [ key ] ?. hadSelections ?? false ) !== keyHasSelections ( key ) ,
126128 ) ;
127129 if ( changedKeys . length > 0 ) {
128- setExpandedChildren ( prev => {
130+ setChildState ( prev => {
129131 const next = { ...prev } ;
130132 for ( const key of changedKeys ) {
131133 next [ key ] = keyHasSelections ( key )
132- ? { expanded : true , hadSelections : true }
134+ ? { ... prev [ key ] , expanded : true , hadSelections : true }
133135 : { ...prev [ key ] , hadSelections : false } ;
134136 }
135137 return next ;
@@ -301,17 +303,20 @@ export const NestedFilterGroup = ({
301303 hasLoadedMore = { hasLoadedMore [ child . key ] || false }
302304 showFilterCounts = { showFilterCounts }
303305 expanded = {
304- expandedChildren [ child . key ] ?. expanded ??
306+ childState [ child . key ] ?. expanded ??
305307 childHasSelections
306308 }
307309 onExpandedChange = { expanded =>
308- setExpandedChildren ( prev => ( {
309- ...prev ,
310- [ child . key ] : {
311- expanded,
312- hadSelections : childHasSelections ,
313- } ,
314- } ) )
310+ updateChildState ( child . key , {
311+ expanded,
312+ hadSelections : childHasSelections ,
313+ } )
314+ }
315+ showDistributions = {
316+ childState [ child . key ] ?. showDistributions ?? false
317+ }
318+ onShowDistributionsChange = { showDistributions =>
319+ updateChildState ( child . key , { showDistributions } )
315320 }
316321 chartConfig = { chartConfig }
317322 isLive = { isLive }
0 commit comments