Skip to content

Commit c2b8c48

Browse files
authored
No table reloading when searching element within same directory (#462)
Signed-off-by: LE SAULNIER Kevin <[email protected]>
1 parent f1b62e1 commit c2b8c48

File tree

3 files changed

+36
-8
lines changed

3 files changed

+36
-8
lines changed

src/components/directory-content.jsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,11 @@ const DirectoryContent = () => {
9595
const selectionForCopy = useSelector((state) => state.selectionForCopy);
9696
const activeDirectory = useSelector((state) => state.activeDirectory);
9797

98-
const [onGridReady, getRowStyle] = useHighlightSearchedElement();
98+
const gridRef = useRef();
99+
100+
const [onGridReady, getRowStyle] = useHighlightSearchedElement(
101+
gridRef?.current?.api
102+
);
99103

100104
const [languageLocal] = useParameterState(PARAM_LANGUAGE);
101105

@@ -152,7 +156,6 @@ const DirectoryContent = () => {
152156
useState(true);
153157

154158
const intl = useIntl();
155-
const gridRef = useRef();
156159
const [rows, childrenMetadata] = useDirectoryContent(
157160
setIsMissingDataAfterDirChange
158161
);

src/components/search/search-bar.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ export const SearchBar: FunctionComponent<SearchBarProps> = ({ inputRef }) => {
4646
const { snackError } = useSnackMessage();
4747
const treeData = useSelector((state: ReduxState) => state.treeData);
4848
const treeDataRef = useRef<ITreeData>();
49+
const selectedDirectory = useSelector(
50+
(state: ReduxState) => state.selectedDirectory
51+
);
4952
treeDataRef.current = treeData;
5053

5154
const { elementsFound, isLoading, searchTerm, updateSearchTerm } =
@@ -128,11 +131,15 @@ export const SearchBar: FunctionComponent<SearchBarProps> = ({ inputRef }) => {
128131
});
129132
}
130133
const lastElement = elementUuidPath.pop();
131-
handleDispatchDirectory(lastElement);
134+
132135
dispatch(setSearchedElement(data));
136+
if (lastElement !== selectedDirectory?.elementUuid) {
137+
handleDispatchDirectory(lastElement);
138+
}
133139
}
134140
},
135141
[
142+
selectedDirectory?.elementUuid,
136143
elementsFound,
137144
handleDispatchDirectory,
138145
updateMapData,

src/components/search/use-highlight-searched-element.tsx

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,28 @@
66
*/
77

88
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';
1116
import { useDispatch, useSelector } from 'react-redux';
1217
import { setSearchedElement } from '../../redux/actions';
1318
import { ReduxState } from '../../redux/reducer.type';
1419

1520
const SEARCH_HIGHLIGHT_DURATION_S = 4;
1621

17-
export const useHighlightSearchedElement = () => {
22+
export const useHighlightSearchedElement = (gridApi: GridApi | null) => {
1823
const searchedElement = useSelector(
1924
(state: ReduxState) => state.searchedElement
2025
);
2126
const dispatch = useDispatch();
2227
const timeout = useRef<ReturnType<typeof setTimeout>>();
2328

24-
const onGridReady = useCallback(
25-
({ api }: GridReadyEvent<ElementAttributes>) => {
29+
const highlightElement = useCallback(
30+
(api: GridApi<ElementAttributes>) => {
2631
// 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
2732
if (!searchedElement) {
2833
return;
@@ -42,6 +47,19 @@ export const useHighlightSearchedElement = () => {
4247
[searchedElement, dispatch]
4348
);
4449

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+
4563
const getRowStyle = useCallback(
4664
(cellData: RowClassParams<ElementAttributes, any>) => {
4765
const style: RowStyle = { fontSize: '1rem' };

0 commit comments

Comments
 (0)