Skip to content

Commit

Permalink
restructure cell focus update calls to avoid render side effect
Browse files Browse the repository at this point in the history
  • Loading branch information
chandlerprall committed Nov 6, 2020
1 parent 639f3bb commit 721cc29
Showing 1 changed file with 29 additions and 20 deletions.
49 changes: 29 additions & 20 deletions src/components/datagrid/data_grid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ import {
DataGridFocusContextShape,
DataGridSortingContext,
} from './data_grid_context';
import { enqueueStateChange } from '../../services/react';

// Used to short-circuit some async browser behaviour that is difficult to account for in tests
const IS_JEST_ENVIRONMENT = global.hasOwnProperty('_isJest');
Expand Down Expand Up @@ -545,27 +546,35 @@ const useFocus = (
EuiDataGridFocusedCell | undefined
>(undefined);

const setFocusedCell = useCallback(
(focusedCell: EuiDataGridFocusedCell) => {
_setFocusedCell((previousCell) => {
// verify that the cell has changed
if (
previousCell != null &&
previousCell[0] === focusedCell[0] &&
previousCell[1] === focusedCell[1]
) {
return previousCell;
}
const setFocusedCell = useCallback((focusedCell: EuiDataGridFocusedCell) => {
_setFocusedCell((previousCell) => {
// verify that the cell has changed
if (
previousCell != null &&
previousCell[0] === focusedCell[0] &&
previousCell[1] === focusedCell[1]
) {
return previousCell;
}
return focusedCell;
});
}, []);

if (previousCell) {
notifyCellOfFocusState(cellsUpdateFocus.current, previousCell, false);
}
notifyCellOfFocusState(cellsUpdateFocus.current, focusedCell, true);
return focusedCell;
});
},
[cellsUpdateFocus]
);
const previousCell = useRef<EuiDataGridFocusedCell | undefined>(undefined);
useEffect(() => {
if (previousCell.current) {
notifyCellOfFocusState(
cellsUpdateFocus.current,
previousCell.current,
false
);
}
previousCell.current = focusedCell;

if (focusedCell) {
notifyCellOfFocusState(cellsUpdateFocus.current, focusedCell, true);
}
}, [cellsUpdateFocus, focusedCell]);

const hasHadFocus = useMemo(() => focusedCell != null, [focusedCell]);

Expand Down

0 comments on commit 721cc29

Please sign in to comment.