Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Datagrid remaining keyboard shortcuts #2519

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
3c582e6
Implements some of the remaining key shortcuts for the data grid
ffknob Nov 12, 2019
9fc1ea5
Data grid
ffknob Nov 12, 2019
7b7d5bc
Page down/up now walks through the grid's pages
ffknob Nov 12, 2019
71808f5
Adds some more keybindings for the data grid component and includes a
ffknob Nov 12, 2019
0ee5510
Removes the conditional testing for alphanumeric key events
ffknob Nov 13, 2019
e77a176
Merge branch 'master' into datagrid-remaining-keyboard-shortcuts
ffknob Nov 13, 2019
c783cd3
Merge branch 'master' into datagrid-remaining-keyboard-shortcuts
ffknob Nov 14, 2019
406b2bf
Refactors the Page Up/Page Down as suggested by @chandlerprall
ffknob Nov 14, 2019
5c198e7
Keeps focus on same cell position (row/column) when changing pages
ffknob Nov 14, 2019
992d5d6
Merge branch 'master' into datagrid-remaining-keyboard-shortcuts
ffknob Nov 15, 2019
25830ce
Creates a context for the data grid and tries to use it to solve
ffknob Nov 15, 2019
3845a10
Fixes bug when paging down and the last page has less rows than the row
ffknob Nov 15, 2019
1d1c7e7
Creates a state to hold the updateFocus() matrix
ffknob Nov 17, 2019
fe3fc64
Calls updateFocus() inside a requestAnimationFrame()
ffknob Nov 18, 2019
0fd79d8
Merge branch 'master' into datagrid-remaining-keyboard-shortcuts
ffknob Nov 18, 2019
8c01784
EuiDataGrid keyboard navigation tweaks
chandlerprall Nov 19, 2019
05c7f10
Refactored cellsUpdateFocus usage
chandlerprall Nov 19, 2019
4cbb9fa
commented on an anti-pattern
chandlerprall Nov 20, 2019
93043d5
Merge pull request #1 from chandlerprall/drks
ffknob Nov 20, 2019
a3569c6
Merge branch 'master' into datagrid-remaining-keyboard-shortcuts
ffknob Nov 20, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

No public interface changes since `14.10.0`.
ffknob marked this conversation as resolved.
Show resolved Hide resolved

- Added new keyboard shortcuts for the data grid component: `Home` (same row, first column), `End` (same row, last column), `Ctrl+Home` (first row, first column), `Ctrl+End` (last row, last column), `Page Up` (next page) and `Page Down` (previous page)

**Bug fixes**

- Created `.euiTableCaption` with `position: relative` to avoid double border under header row ([#2484](https://github.com/elastic/eui/pull/2484))
Expand Down
100 changes: 97 additions & 3 deletions src/components/datagrid/data_grid.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1444,15 +1444,22 @@ Array [
const component = mount(
<EuiDataGrid
{...requiredProps}
columns={[{ id: 'A' }, { id: 'B' }]}
columns={[{ id: 'A' }, { id: 'B' }, { id: 'C' }]}
columnVisibility={{
visibleColumns: ['A', 'B'],
visibleColumns: ['A', 'B', 'C'],
setVisibleColumns: () => {},
}}
rowCount={3}
rowCount={9}
renderCellValue={({ rowIndex, columnId }) =>
`${rowIndex}, ${columnId}`
}
pagination={{
pageIndex: 0,
pageSize: 3,
pageSizeOptions: [3, 6, 10],
onChangePage: () => {},
onChangeItemsPerPage: () => {},
}}
/>
);

Expand Down Expand Up @@ -1503,6 +1510,93 @@ Array [
expect(
focusableCell.find('[data-test-subj="cell-content"]').text()
).toEqual('0, A');

focusableCell
.simulate('keydown', { keyCode: keyCodes.DOWN })
.simulate('keydown', { keyCode: keyCodes.END });

focusableCell = getFocusableCell(component);
expect(
focusableCell.find('[data-test-subj="cell-content"]').text()
).toEqual('1, C');

focusableCell
.simulate('keydown', { keyCode: keyCodes.UP })
.simulate('keydown', { keyCode: keyCodes.HOME });

focusableCell = getFocusableCell(component);
expect(
focusableCell.find('[data-test-subj="cell-content"]').text()
).toEqual('0, A');

focusableCell.simulate('keydown', {
ctrlKey: true,
keyCode: keyCodes.END,
});

focusableCell = getFocusableCell(component);
expect(
focusableCell.find('[data-test-subj="cell-content"]').text()
).toEqual('2, C');

focusableCell.simulate('keydown', {
ctrlKey: true,
keyCode: keyCodes.HOME,
});

focusableCell = getFocusableCell(component);
expect(
focusableCell.find('[data-test-subj="cell-content"]').text()
).toEqual('0, A');

focusableCell.simulate('keydown', {
keyCode: keyCodes.PAGE_DOWN,
});

focusableCell = getFocusableCell(component);
expect(
focusableCell.find('[data-test-subj="cell-content"]').text()
).toEqual('0, A'); // focus should not move when up against an edge

focusableCell.simulate('keydown', {
keyCode: keyCodes.PAGE_UP,
});

focusableCell = getFocusableCell(component);
expect(
focusableCell.find('[data-test-subj="cell-content"]').text()
).toEqual('3, A');

focusableCell
.simulate('keydown', { keyCode: keyCodes.RIGHT }) // 3, B
.simulate('keydown', {
keyCode: keyCodes.PAGE_UP,
});

focusableCell = getFocusableCell(component);
expect(
focusableCell.find('[data-test-subj="cell-content"]').text()
).toEqual('6, A'); // should move page up and focus on the cell at the first column/row, even if in the previous page wasn't focused there

focusableCell.simulate('keydown', {
keyCode: keyCodes.PAGE_DOWN,
});

focusableCell = getFocusableCell(component);
expect(
focusableCell.find('[data-test-subj="cell-content"]').text()
).toEqual('3, A'); // back one page

focusableCell
.simulate('keydown', { keyCode: keyCodes.RIGHT }) // 3, B
.simulate('keydown', {
keyCode: keyCodes.PAGE_DOWN,
});

focusableCell = getFocusableCell(component);
expect(
focusableCell.find('[data-test-subj="cell-content"]').text()
).toEqual('0, A'); // should be back in the first page
});
it('does not break arrow key focus control behavior when also using a mouse', () => {
const component = mount(
Expand Down
87 changes: 61 additions & 26 deletions src/components/datagrid/data_grid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -292,35 +292,70 @@ function createKeyDownHandler(
const colCount = visibleColumns.length - 1;
const [x, y] = focusedCell;
const rowCount = computeVisibleRows(props);
const { keyCode } = event;
const { keyCode, ctrlKey } = event;

switch (keyCode) {
case keyCodes.DOWN:
event.preventDefault();
if (y < rowCount - 1) {
setFocusedCell([x, y + 1]);
}
break;
case keyCodes.LEFT:
event.preventDefault();
if (x > 0) {
setFocusedCell([x - 1, y]);
}
break;
case keyCodes.UP:
event.preventDefault();
// TODO sort out when a user can arrow up into the column headers
const minimumIndex = headerIsInteractive ? -1 : 0;
if (y > minimumIndex) {
setFocusedCell([x, y - 1]);
if (keyCode === keyCodes.DOWN) {
event.preventDefault();
if (y < rowCount - 1) {
setFocusedCell([x, y + 1]);
}
} else if (keyCode === keyCodes.LEFT) {
event.preventDefault();
if (x > 0) {
setFocusedCell([x - 1, y]);
}
} else if (keyCode === keyCodes.UP) {
event.preventDefault();
// TODO sort out when a user can arrow up into the column headers
myasonik marked this conversation as resolved.
Show resolved Hide resolved
const minimumIndex = headerIsInteractive ? -1 : 0;
if (y > minimumIndex) {
setFocusedCell([x, y - 1]);
}
} else if (keyCode === keyCodes.RIGHT) {
event.preventDefault();
if (x < colCount) {
setFocusedCell([x + 1, y]);
}
} else if (keyCode === keyCodes.PAGE_UP) {
event.preventDefault();
if (props.pagination) {
const totalRowCount = props.rowCount;
const pageIndex = props.pagination.pageIndex;
const pageSize = props.pagination.pageSize;
const pageCount = Math.ceil(totalRowCount / pageSize);
if (pageIndex < pageCount) {
props.pagination!.pageIndex += 1;
setFocusedCell([0, 0]);
}
break;
case keyCodes.RIGHT:
event.preventDefault();
if (x < colCount) {
setFocusedCell([x + 1, y]);
}
} else if (keyCode === keyCodes.PAGE_DOWN) {
event.preventDefault();
if (props.pagination) {
const pageIndex = props.pagination.pageIndex;
if (pageIndex > 0) {
props.pagination!.pageIndex -= 1;
setFocusedCell([0, 0]);
}
break;
}
} else if (keyCode === (ctrlKey && keyCodes.END)) {
event.preventDefault();
setFocusedCell([colCount, rowCount - 1]);
} else if (keyCode === (ctrlKey && keyCodes.HOME)) {
event.preventDefault();
setFocusedCell([0, 0]);
} else if (keyCode === keyCodes.END) {
event.preventDefault();
setFocusedCell([colCount, y]);
} else if (keyCode === keyCodes.HOME) {
event.preventDefault();
setFocusedCell([0, y]);
} else if (
(keyCode >= 48 && keyCode <= 57) ||
(keyCode >= 65 && keyCode <= 90)
) {
event.preventDefault();
// Alphanumeric
// TODO: If a cell contains an input widget, places focus on the first one, and inserts key (#2482)
}
ffknob marked this conversation as resolved.
Show resolved Hide resolved
};
}
Expand Down
10 changes: 10 additions & 0 deletions src/services/key_codes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ export const UP = 38;
export const LEFT = 37;
export const RIGHT = 39;

export const PAGE_UP = 33;
export const PAGE_DOWN = 34;
export const END = 35;
export const HOME = 36;

export enum keyCodes {
ENTER = 13,
SPACE = 32,
Expand All @@ -23,4 +28,9 @@ export enum keyCodes {
UP = 38,
LEFT = 37,
RIGHT = 39,

PAGE_UP = 33,
PAGE_DOWN = 34,
END = 35,
HOME = 36,
}