Skip to content

Commit

Permalink
[PR feedback] Simplify fns/useEffects
Browse files Browse the repository at this point in the history
  • Loading branch information
cee-chen committed Apr 25, 2024
1 parent 5a979b0 commit 156a98d
Showing 1 changed file with 7 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,26 +49,17 @@ export const EuiDataGridHeaderCellWrapper: FunctionComponent<
}, [index, setFocusedCell]);

const [isFocused, setIsFocused] = useState(false);

const updateFocus = (isFocused: Boolean, headerEl: HTMLDivElement | null) => {
if (isFocused && headerEl) {
// Only focus the cell if not already focused on something in the cell
if (!headerEl.contains(document.activeElement)) {
headerEl.focus();
}
}
}

useEffect(() => {
onFocusUpdate([index, -1], (isFocused: boolean) => {
setIsFocused(isFocused);
updateFocus(isFocused, headerEl);
if (isFocused && headerEl) {
// Only focus the cell if not already focused on something in the cell
if (!headerEl.contains(document.activeElement)) {
headerEl.focus();
}
}
});
}, [index, onFocusUpdate]);

useEffect(() => {
updateFocus(isFocused, headerEl);
}, [headerEl]);
}, [index, onFocusUpdate, headerEl]);

// For cell headers with actions, auto-focus into the button instead of the cell wrapper div
// The button text is significantly more useful to screen readers (e.g. contains sort order & hints)
Expand Down

0 comments on commit 156a98d

Please sign in to comment.