Skip to content

Commit 68455c7

Browse files
authored
Fixes/run filtered keyboard nav (#1074)
* Ensure each switch condition returns a state * Fix for scrolling when filtered * Fix for up/down navigation when filtered
1 parent b0a2c42 commit 68455c7

File tree

2 files changed

+37
-31
lines changed

2 files changed

+37
-31
lines changed

apps/webapp/app/components/primitives/TreeView/TreeView.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ export function useTree<TData, TFilterValue>({
246246

247247
const scrollToNodeFn = useCallback(
248248
(id: string) => {
249-
const itemIndex = tree.findIndex((node) => node.id === id);
249+
const itemIndex = state.visibleNodeIds.findIndex((n) => n === id);
250250

251251
if (itemIndex !== -1) {
252252
virtualizer.scrollToIndex(itemIndex, { align: "auto" });

apps/webapp/app/components/primitives/TreeView/reducer.ts

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -231,16 +231,16 @@ export function reducer(state: TreeState, action: Action): TreeState {
231231
const currentlySelected = state.nodes[action.payload.id]?.selected ?? false;
232232
if (currentlySelected) {
233233
return reducer(state, { type: "DESELECT_NODE", payload: { id: action.payload.id } });
234-
} else {
235-
return reducer(state, {
236-
type: "SELECT_NODE",
237-
payload: {
238-
id: action.payload.id,
239-
scrollToNode: action.payload.scrollToNode,
240-
scrollToNodeFn: action.payload.scrollToNodeFn,
241-
},
242-
});
243234
}
235+
236+
return reducer(state, {
237+
type: "SELECT_NODE",
238+
payload: {
239+
id: action.payload.id,
240+
scrollToNode: action.payload.scrollToNode,
241+
scrollToNodeFn: action.payload.scrollToNodeFn,
242+
},
243+
});
244244
}
245245
case "EXPAND_NODE": {
246246
const newNodes = {
@@ -277,16 +277,16 @@ export function reducer(state: TreeState, action: Action): TreeState {
277277
type: "COLLAPSE_NODE",
278278
payload: { id: action.payload.id },
279279
});
280-
} else {
281-
return reducer(state, {
282-
type: "EXPAND_NODE",
283-
payload: {
284-
id: action.payload.id,
285-
scrollToNode: action.payload.scrollToNode,
286-
scrollToNodeFn: action.payload.scrollToNodeFn,
287-
},
288-
});
289280
}
281+
282+
return reducer(state, {
283+
type: "EXPAND_NODE",
284+
payload: {
285+
id: action.payload.id,
286+
scrollToNode: action.payload.scrollToNode,
287+
scrollToNodeFn: action.payload.scrollToNodeFn,
288+
},
289+
});
290290
}
291291
case "EXPAND_ALL_BELOW_DEPTH": {
292292
const nodesToExpand = state.tree.filter(
@@ -396,17 +396,17 @@ export function reducer(state: TreeState, action: Action): TreeState {
396396
level: action.payload.level,
397397
},
398398
});
399-
} else {
400-
return reducer(state, {
401-
type: "EXPAND_LEVEL",
402-
payload: {
403-
level: action.payload.level,
404-
},
405-
});
406399
}
400+
401+
return reducer(state, {
402+
type: "EXPAND_LEVEL",
403+
payload: {
404+
level: action.payload.level,
405+
},
406+
});
407407
}
408408
case "SELECT_FIRST_VISIBLE_NODE": {
409-
const node = firstVisibleNode(state.tree, state.nodes);
409+
const node = firstVisibleNode(state.tree, state.filteredNodes);
410410
if (node) {
411411
return reducer(state, {
412412
type: "SELECT_NODE",
@@ -417,9 +417,11 @@ export function reducer(state: TreeState, action: Action): TreeState {
417417
},
418418
});
419419
}
420+
421+
return state;
420422
}
421423
case "SELECT_LAST_VISIBLE_NODE": {
422-
const node = lastVisibleNode(state.tree, state.nodes);
424+
const node = lastVisibleNode(state.tree, state.filteredNodes);
423425
if (node) {
424426
return reducer(state, {
425427
type: "SELECT_NODE",
@@ -430,6 +432,8 @@ export function reducer(state: TreeState, action: Action): TreeState {
430432
},
431433
});
432434
}
435+
436+
return state;
433437
}
434438
case "SELECT_NEXT_VISIBLE_NODE": {
435439
const selected = selectedIdFromState(state.nodes);
@@ -443,7 +447,7 @@ export function reducer(state: TreeState, action: Action): TreeState {
443447
});
444448
}
445449

446-
const visible = visibleNodes(state.tree, state.nodes);
450+
const visible = visibleNodes(state.tree, state.filteredNodes);
447451
const selectedIndex = visible.findIndex((node) => node.id === selected);
448452
const nextNode = visible[selectedIndex + 1];
449453
if (nextNode) {
@@ -456,6 +460,8 @@ export function reducer(state: TreeState, action: Action): TreeState {
456460
},
457461
});
458462
}
463+
464+
return state;
459465
}
460466
case "SELECT_PREVIOUS_VISIBLE_NODE": {
461467
const selected = selectedIdFromState(state.nodes);
@@ -470,9 +476,9 @@ export function reducer(state: TreeState, action: Action): TreeState {
470476
});
471477
}
472478

473-
const visible = visibleNodes(state.tree, state.nodes);
479+
const visible = visibleNodes(state.tree, state.filteredNodes);
474480
const selectedIndex = visible.findIndex((node) => node.id === selected);
475-
const previousNode = visible[selectedIndex - 1];
481+
const previousNode = visible[Math.max(0, selectedIndex - 1)];
476482
if (previousNode) {
477483
return reducer(state, {
478484
type: "SELECT_NODE",

0 commit comments

Comments
 (0)