forked from aws/agentcore-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseMultiSelectNavigation.ts
More file actions
123 lines (112 loc) · 3.63 KB
/
Copy pathuseMultiSelectNavigation.ts
File metadata and controls
123 lines (112 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import { useInput } from 'ink';
import { useCallback, useState } from 'react';
interface UseMultiSelectNavigationOptions<T> {
/** The list of items to navigate */
items: T[];
/** Extract item ID for selection tracking */
getId: (item: T) => string;
/** Callback when selection is confirmed via Enter */
onConfirm?: (selectedIds: string[]) => void;
/** Callback when Escape is pressed */
onExit?: () => void;
/** Whether navigation is active (default: true) */
isActive?: boolean;
/** Whether a text input is currently focused - disables j/k keys (default: false) */
textInputActive?: boolean;
/** Whether to require at least one selection before confirm (default: false) */
requireSelection?: boolean;
/** Initial set of selected item IDs (default: empty set) */
initialSelectedIds?: string[];
}
interface UseMultiSelectNavigationResult {
/** Current cursor index */
cursorIndex: number;
/** Set the cursor index */
setCursorIndex: React.Dispatch<React.SetStateAction<number>>;
/** Currently selected item IDs */
selectedIds: Set<string>;
/** Toggle selection of item at cursor */
toggleSelection: () => void;
/** Reset cursor and selection */
reset: () => void;
}
/**
* Hook for managing multi-select list navigation with arrow keys, Space toggle, Enter confirm, Escape exit.
* Reduces boilerplate for screens with multi-selectable lists.
*
* @example
* ```tsx
* const { cursorIndex, selectedIds } = useMultiSelectNavigation({
* items: agents,
* getId: (agent) => agent.name,
* onConfirm: (ids) => wizard.setAgents(ids),
* onExit: () => wizard.goBack(),
* isActive: wizard.step === 'agents',
* });
*
* return <MultiSelectList items={agents} selectedIndex={cursorIndex} selectedIds={selectedIds} />;
* ```
*/
export function useMultiSelectNavigation<T>({
items,
getId,
onConfirm,
onExit,
isActive = true,
textInputActive = false,
requireSelection = false,
initialSelectedIds,
}: UseMultiSelectNavigationOptions<T>): UseMultiSelectNavigationResult {
const [cursorIndex, setCursorIndex] = useState(0);
const [selectedIds, setSelectedIds] = useState<Set<string>>(() => new Set(initialSelectedIds ?? []));
const toggleSelection = useCallback(() => {
const item = items[cursorIndex];
if (!item) return;
const id = getId(item);
setSelectedIds(prev => {
const next = new Set(prev);
if (next.has(id)) {
next.delete(id);
} else {
next.add(id);
}
return next;
});
}, [items, cursorIndex, getId]);
const reset = useCallback(() => {
setCursorIndex(0);
setSelectedIds(new Set(initialSelectedIds ?? []));
}, [initialSelectedIds]);
useInput(
(input, key) => {
// Handle exit
if (key.escape) {
onExit?.();
return;
}
// Handle arrow navigation (and j/k when no text input)
if ((key.upArrow || (!textInputActive && input === 'k')) && items.length > 0) {
setCursorIndex(i => Math.max(0, i - 1));
return;
}
if ((key.downArrow || (!textInputActive && input === 'j')) && items.length > 0) {
setCursorIndex(i => Math.min(items.length - 1, i + 1));
return;
}
// Handle Space toggle
if (input === ' ' && items.length > 0) {
toggleSelection();
return;
}
// Handle Enter confirm
if (key.return) {
if (requireSelection && selectedIds.size === 0) {
return; // Don't confirm if selection required but none selected
}
onConfirm?.(Array.from(selectedIds));
}
},
{ isActive }
);
return { cursorIndex, setCursorIndex, selectedIds, toggleSelection, reset };
}