Skip to content

Commit 3b48367

Browse files
Allow navigating down from header cells, as well as support cmd/ctrl + arrow keys (#10777)
* keyboard * add changeset * format * changes * s * changes * changes * format * add changeset --------- Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
1 parent 3232cdd commit 3b48367

File tree

4 files changed

+32
-12
lines changed

4 files changed

+32
-12
lines changed

.changeset/sharp-teams-start.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@gradio/dataframe": minor
3+
"gradio": minor
4+
---
5+
6+
feat:Allow navigating down from header cells, as well as support cmd/ctrl + arrow keys

js/dataframe/shared/context/keyboard_context.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export type KeyboardContext = {
3737
end: CellCoordinate
3838
) => CellCoordinate[];
3939
move_cursor: (
40-
key: "ArrowRight" | "ArrowLeft" | "ArrowDown" | "ArrowUp",
40+
event: KeyboardEvent,
4141
current_coords: CellCoordinate,
4242
data: CellData[][]
4343
) => false | CellCoordinate;

js/dataframe/shared/selection_utils.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,19 +151,39 @@ export function get_next_cell_coordinates(
151151
}
152152

153153
export function move_cursor(
154-
key: "ArrowRight" | "ArrowLeft" | "ArrowDown" | "ArrowUp",
154+
event: KeyboardEvent,
155155
current_coords: CellCoordinate,
156156
data: CellData[][]
157157
): CellCoordinate | false {
158+
const key = event.key as "ArrowRight" | "ArrowLeft" | "ArrowDown" | "ArrowUp";
158159
const dir = {
159160
ArrowRight: [0, 1],
160161
ArrowLeft: [0, -1],
161162
ArrowDown: [1, 0],
162163
ArrowUp: [-1, 0]
163164
}[key];
164165

165-
const i = current_coords[0] + dir[0];
166-
const j = current_coords[1] + dir[1];
166+
let i, j;
167+
if (event.metaKey || event.ctrlKey) {
168+
if (key === "ArrowRight") {
169+
i = current_coords[0];
170+
j = data[0].length - 1;
171+
} else if (key === "ArrowLeft") {
172+
i = current_coords[0];
173+
j = 0;
174+
} else if (key === "ArrowDown") {
175+
i = data.length - 1;
176+
j = current_coords[1];
177+
} else if (key === "ArrowUp") {
178+
i = 0;
179+
j = current_coords[1];
180+
} else {
181+
return false;
182+
}
183+
} else {
184+
i = current_coords[0] + dir[0];
185+
j = current_coords[1] + dir[1];
186+
}
167187

168188
if (i < 0 && j <= 0) {
169189
return false;

js/dataframe/shared/utils/keyboard_utils.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@ function handle_header_navigation(
99
ctx: KeyboardContext
1010
): boolean {
1111
if (ctx.selected_header === false || ctx.header_edit !== false) return false;
12-
1312
switch (event.key) {
1413
case "ArrowDown":
14+
ctx.df_actions.set_selected_header(false);
1515
ctx.df_actions.set_selected([0, ctx.selected_header]);
1616
ctx.df_actions.set_selected_cells([[0, ctx.selected_header]]);
17-
ctx.df_actions.set_selected_header(false);
1817
return true;
1918
case "ArrowLeft":
2019
ctx.df_actions.set_selected_header(
@@ -88,11 +87,7 @@ function handle_arrow_keys(
8887
if (ctx.editing) return false;
8988
event.preventDefault();
9089

91-
const next_coords = ctx.move_cursor(
92-
event.key as "ArrowRight" | "ArrowLeft" | "ArrowDown" | "ArrowUp",
93-
[i, j],
94-
ctx.data
95-
);
90+
const next_coords = ctx.move_cursor(event, [i, j], ctx.data);
9691
if (next_coords) {
9792
if (event.shiftKey) {
9893
ctx.df_actions.set_selected_cells(
@@ -195,7 +190,6 @@ async function handle_cell_navigation(
195190
ctx: KeyboardContext
196191
): Promise<boolean> {
197192
if (!ctx.selected) return false;
198-
199193
if (event.key === "c" && (event.metaKey || event.ctrlKey)) {
200194
event.preventDefault();
201195
if (ctx.selected_cells.length > 0) {

0 commit comments

Comments
 (0)