@@ -10,7 +10,7 @@ import MuiBox from '@mui/material/Box';
10
10
import MuiTooltip from '@mui/material/Tooltip' ;
11
11
import { SxProps } from '@mui/system' ;
12
12
import { defer } from 'lodash' ;
13
- import { useEffect , useMemo , useRef , useState } from 'react' ;
13
+ import { useEffect , useMemo , useState } from 'react' ;
14
14
import {
15
15
GroupedVirtuoso ,
16
16
GroupedVirtuosoHandle ,
@@ -19,16 +19,26 @@ import {
19
19
20
20
import { text } from '../../../shared/text' ;
21
21
import { OpossumColors } from '../../shared-styles' ;
22
+ import { useVirtuosoRefs } from '../../util/use-virtuoso-refs' ;
22
23
import { LoadingMask } from '../LoadingMask/LoadingMask' ;
23
24
import { NoResults } from '../NoResults/NoResults' ;
24
25
import { GroupContainer , StyledLinearProgress } from './GroupedList.style' ;
25
26
27
+ export interface GroupedListItemContentProps {
28
+ index : number ;
29
+ selected : boolean ;
30
+ focused : boolean ;
31
+ }
32
+
26
33
export interface GroupedListProps {
27
34
className ?: string ;
28
35
grouped : Record < string , ReadonlyArray < string > > | null ;
29
36
loading ?: boolean ;
30
37
renderGroupName ?: ( key : string ) => React . ReactNode ;
31
- renderItemContent : ( datum : string , index : number ) => React . ReactNode ;
38
+ renderItemContent : (
39
+ datum : string ,
40
+ props : GroupedListItemContentProps ,
41
+ ) => React . ReactNode ;
32
42
selected ?: string ;
33
43
sx ?: SxProps ;
34
44
testId ?: string ;
@@ -45,7 +55,6 @@ export function GroupedList({
45
55
testId,
46
56
...props
47
57
} : GroupedListProps & Omit < GroupedVirtuosoProps < string , unknown > , 'selected' > ) {
48
- const ref = useRef < GroupedVirtuosoHandle > ( null ) ;
49
58
const [ { startIndex, endIndex } , setRange ] = useState < {
50
59
startIndex : number ;
51
60
endIndex : number ;
@@ -66,6 +75,12 @@ export function GroupedList({
66
75
} ;
67
76
} , [ grouped , selected ] ) ;
68
77
78
+ const { ref, scrollerRef, focusedIndex } =
79
+ useVirtuosoRefs < GroupedVirtuosoHandle > ( {
80
+ data : groups ?. ids ,
81
+ selectedIndex : groups ?. selectedIndex ,
82
+ } ) ;
83
+
69
84
useEffect ( ( ) => {
70
85
if ( groups ?. selectedIndex !== undefined && groups . selectedIndex >= 0 ) {
71
86
defer ( ( ) =>
@@ -75,7 +90,7 @@ export function GroupedList({
75
90
} ) ,
76
91
) ;
77
92
}
78
- } , [ groups ?. selectedIndex ] ) ;
93
+ } , [ groups ?. selectedIndex , ref ] ) ;
79
94
80
95
return (
81
96
< LoadingMask
@@ -101,6 +116,7 @@ export function GroupedList({
101
116
EmptyPlaceholder :
102
117
loading || groups . ids . length ? undefined : ( ) => < NoResults /> ,
103
118
} }
119
+ scrollerRef = { scrollerRef }
104
120
rangeChanged = { setRange }
105
121
groupCounts = { groups ?. counts }
106
122
groupContent = { ( index ) => (
@@ -114,7 +130,13 @@ export function GroupedList({
114
130
) }
115
131
</ GroupContainer >
116
132
) }
117
- itemContent = { ( index ) => renderItemContent ( groups . ids [ index ] , index ) }
133
+ itemContent = { ( index ) =>
134
+ renderItemContent ( groups . ids [ index ] , {
135
+ index,
136
+ selected : index === groups . selectedIndex ,
137
+ focused : index === focusedIndex ,
138
+ } )
139
+ }
118
140
{ ...props }
119
141
/>
120
142
) ;
0 commit comments