Skip to content

Commit

Permalink
Fix flaky Cypress test
Browse files Browse the repository at this point in the history
- caused by race condition with ref not updating `HandleInteractiveChildren`
  • Loading branch information
cee-chen committed Jan 5, 2024
1 parent d6c70f6 commit 1d9e46f
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ exports[`EuiDataGridBodyCustomRender treats \`renderCustomGridBody\` as a render
<button
aria-describedby="euiDataGridCellHeader_generated-id_sorting euiDataGridCellHeader_generated-id_actions"
class="euiDataGridHeaderCell__button"
data-euigrid-tab-managed="true"
data-test-subj="dataGridHeaderCellActionButton-columnA"
tabindex="-1"
>
<div
class="euiDataGridHeaderCell__content"
Expand Down Expand Up @@ -79,7 +81,9 @@ exports[`EuiDataGridBodyCustomRender treats \`renderCustomGridBody\` as a render
<button
aria-describedby="euiDataGridCellHeader_generated-id_sorting euiDataGridCellHeader_generated-id_actions"
class="euiDataGridHeaderCell__button"
data-euigrid-tab-managed="true"
data-test-subj="dataGridHeaderCellActionButton-columnB"
tabindex="-1"
>
<div
class="euiDataGridHeaderCell__content"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ exports[`EuiDataGridBodyVirtualized renders 1`] = `
<button
aria-describedby="euiDataGridCellHeader_generated-id_sorting euiDataGridCellHeader_generated-id_actions"
class="euiDataGridHeaderCell__button"
data-euigrid-tab-managed="true"
data-test-subj="dataGridHeaderCellActionButton-columnA"
tabindex="-1"
>
<div
class="euiDataGridHeaderCell__content"
Expand Down Expand Up @@ -83,7 +85,9 @@ exports[`EuiDataGridBodyVirtualized renders 1`] = `
<button
aria-describedby="euiDataGridCellHeader_generated-id_sorting euiDataGridCellHeader_generated-id_actions"
class="euiDataGridHeaderCell__button"
data-euigrid-tab-managed="true"
data-test-subj="dataGridHeaderCellActionButton-columnB"
tabindex="-1"
>
<div
class="euiDataGridHeaderCell__content"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ exports[`EuiDataGridHeaderCell renders 1`] = `
<button
aria-describedby="euiDataGridCellHeader_generated-id_sorting euiDataGridCellHeader_generated-id_actions"
class="euiDataGridHeaderCell__button"
data-euigrid-tab-managed="true"
data-test-subj="dataGridHeaderCellActionButton-someColumn"
tabindex="-1"
>
<div
class="euiDataGridHeaderCell__content"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import React, {
FocusEventHandler,
useContext,
useEffect,
useRef,
useState,
useCallback,
} from 'react';
Expand Down Expand Up @@ -41,7 +40,8 @@ export const EuiDataGridHeaderCellWrapper: FunctionComponent<
}) => {
const classes = classnames('euiDataGridHeaderCell', className);

const headerRef = useRef<HTMLDivElement>(null);
// Must be a state and not a ref to trigger a HandleInteractiveChildren rerender
const [headerEl, setHeaderEl] = useState<HTMLDivElement | null>(null);

const { setFocusedCell, onFocusUpdate } = useContext(DataGridFocusContext);
const updateCellFocusContext = useCallback(() => {
Expand All @@ -56,30 +56,29 @@ export const EuiDataGridHeaderCellWrapper: FunctionComponent<
}, [index, onFocusUpdate]);

useEffect(() => {
if (isFocused) {
const cell = headerRef.current!;
if (isFocused && headerEl) {
// Only focus the cell if not already focused on something in the cell
if (!cell.contains(document.activeElement)) {
cell.focus();
if (!headerEl.contains(document.activeElement)) {
headerEl.focus();
}
}
}, [isFocused]);
}, [isFocused, 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)
const onFocus: FocusEventHandler = useCallback(
(e) => {
if (hasActionsPopover && e.target === headerRef.current) {
if (hasActionsPopover && e.target === headerEl) {
focusActionsButton?.();
}
},
[hasActionsPopover, focusActionsButton]
[hasActionsPopover, focusActionsButton, headerEl]
);

return (
<div
role="columnheader"
ref={headerRef}
ref={setHeaderEl}
tabIndex={isFocused && !isActionsButtonFocused ? 0 : -1}
onFocus={onFocus}
className={classes}
Expand All @@ -92,7 +91,7 @@ export const EuiDataGridHeaderCellWrapper: FunctionComponent<
{...rest}
>
<HandleInteractiveChildren
cellEl={headerRef.current}
cellEl={headerEl}
updateCellFocusContext={updateCellFocusContext}
renderFocusTrap={!hasActionsPopover}
>
Expand Down

0 comments on commit 1d9e46f

Please sign in to comment.