6
6
*/
7
7
8
8
import { ElementAttributes } from '@gridsuite/commons-ui' ;
9
- import { GridReadyEvent , RowClassParams , RowStyle } from 'ag-grid-community' ;
10
- import { useCallback , useRef } from 'react' ;
9
+ import {
10
+ GridApi ,
11
+ GridReadyEvent ,
12
+ RowClassParams ,
13
+ RowStyle ,
14
+ } from 'ag-grid-community' ;
15
+ import { useCallback , useEffect , useRef } from 'react' ;
11
16
import { useDispatch , useSelector } from 'react-redux' ;
12
17
import { setSearchedElement } from '../../redux/actions' ;
13
18
import { ReduxState } from '../../redux/reducer.type' ;
14
19
15
20
const SEARCH_HIGHLIGHT_DURATION_S = 4 ;
16
21
17
- export const useHighlightSearchedElement = ( ) => {
22
+ export const useHighlightSearchedElement = ( gridApi : GridApi | null ) => {
18
23
const searchedElement = useSelector (
19
24
( state : ReduxState ) => state . searchedElement
20
25
) ;
21
26
const dispatch = useDispatch ( ) ;
22
27
const timeout = useRef < ReturnType < typeof setTimeout > > ( ) ;
23
28
24
- const onGridReady = useCallback (
25
- ( { api } : GridReadyEvent < ElementAttributes > ) => {
29
+ const highlightElement = useCallback (
30
+ ( api : GridApi < ElementAttributes > ) => {
26
31
// if there is a searched element, we scroll to it, style it for SEARCH_HIGHTLIGHT_DURATION, then remove it from searchedElement to go back to previous style
27
32
if ( ! searchedElement ) {
28
33
return ;
@@ -42,6 +47,19 @@ export const useHighlightSearchedElement = () => {
42
47
[ searchedElement , dispatch ]
43
48
) ;
44
49
50
+ const onGridReady = useCallback (
51
+ ( { api } : GridReadyEvent < ElementAttributes > ) => {
52
+ highlightElement ( api ) ;
53
+ } ,
54
+ [ highlightElement ]
55
+ ) ;
56
+
57
+ useEffect ( ( ) => {
58
+ if ( gridApi ) {
59
+ highlightElement ( gridApi ) ;
60
+ }
61
+ } , [ highlightElement , gridApi ] ) ;
62
+
45
63
const getRowStyle = useCallback (
46
64
( cellData : RowClassParams < ElementAttributes , any > ) => {
47
65
const style : RowStyle = { fontSize : '1rem' } ;
0 commit comments