Skip to content

Commit

Permalink
[elastic#5476] Fix keyboard navigation after hiding a column via head…
Browse files Browse the repository at this point in the history
…er actions
  • Loading branch information
cee-chen committed Jan 11, 2022
1 parent d29d938 commit 8a035de
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/components/datagrid/body/header/column_actions.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ describe('getColumnActions', () => {
const focusFirstVisibleInteractiveCell = jest.fn();
const setIsPopoverOpen = jest.fn();
const switchColumnPos = jest.fn();
const setFocusedCell = jest.fn();

const testArgs = {
column: { id: 'B' },
Expand All @@ -33,6 +34,7 @@ describe('getColumnActions', () => {
setIsPopoverOpen,
sorting: undefined,
switchColumnPos,
setFocusedCell,
};

// DRY test helper
Expand Down Expand Up @@ -185,12 +187,14 @@ describe('getColumnActions', () => {
`);
});

it('calls switchColumnPos on click', () => {
it('calls switchColumnPos and updates the focused cell column index on click', () => {
callActionOnClick(moveLeft);
expect(switchColumnPos).toHaveBeenCalledWith('B', 'A');
expect(setFocusedCell).toHaveBeenLastCalledWith([0, -1]);

callActionOnClick(moveRight);
expect(switchColumnPos).toHaveBeenCalledWith('B', 'C');
expect(setFocusedCell).toHaveBeenLastCalledWith([2, -1]);
});
});

Expand Down
8 changes: 7 additions & 1 deletion src/components/datagrid/body/header/column_actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ interface GetColumnActions {
setIsPopoverOpen: (value: boolean) => void;
sorting: EuiDataGridSorting | undefined;
switchColumnPos: (colFromId: string, colToId: string) => void;
setFocusedCell: DataGridFocusContextShape['setFocusedCell'];
}

export const getColumnActions = ({
Expand All @@ -45,6 +46,7 @@ export const getColumnActions = ({
setIsPopoverOpen,
sorting,
switchColumnPos,
setFocusedCell,
}: GetColumnActions): EuiListGroupItemProps[] => {
if (column.actions === false) {
return [];
Expand All @@ -67,6 +69,7 @@ export const getColumnActions = ({
column,
columns,
switchColumnPos,
setFocusedCell,
}),
...(column.actions?.additional || []),
];
Expand Down Expand Up @@ -133,13 +136,14 @@ export const getHideColumnAction = ({
*/
type MoveColumnActions = Pick<
GetColumnActions,
'column' | 'columns' | 'switchColumnPos'
'column' | 'columns' | 'switchColumnPos' | 'setFocusedCell'
>;

const getMoveColumnActions = ({
column,
columns,
switchColumnPos,
setFocusedCell,
}: MoveColumnActions): EuiListGroupItemProps[] => {
const items = [];

Expand All @@ -150,6 +154,7 @@ const getMoveColumnActions = ({
const targetCol = columns[colIdx - 1];
if (targetCol) {
switchColumnPos(column.id, targetCol.id);
setFocusedCell([colIdx - 1, -1]);
}
};
const action = {
Expand All @@ -169,6 +174,7 @@ const getMoveColumnActions = ({
const targetCol = columns[colIdx + 1];
if (targetCol) {
switchColumnPos(column.id, targetCol.id);
setFocusedCell([colIdx + 1, -1]);
}
};
const action = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ export const EuiDataGridHeaderCell: FunctionComponent<EuiDataGridHeaderCellProps
} = {};
const screenReaderId = useGeneratedHtmlId();

const { focusFirstVisibleInteractiveCell } = useContext(DataGridFocusContext);
const { setFocusedCell, focusFirstVisibleInteractiveCell } = useContext(
DataGridFocusContext
);

const { sorting } = useContext(DataGridSortingContext);
let sortString;
Expand Down Expand Up @@ -99,6 +101,7 @@ export const EuiDataGridHeaderCell: FunctionComponent<EuiDataGridHeaderCellProps
setIsPopoverOpen,
sorting,
switchColumnPos,
setFocusedCell,
});

const showColumnActions = columnActions && columnActions.length > 0;
Expand Down

0 comments on commit 8a035de

Please sign in to comment.