chore: Tab for slash command autocomplete (#249)

This commit is contained in:
Devansh Jain
2025-12-16 16:46:13 -08:00
committed by GitHub
parent 0f6ec5e21b
commit e9d6b16e86
7 changed files with 110 additions and 46 deletions

View File

@@ -6,8 +6,10 @@ interface UseAutocompleteNavigationOptions<T> {
matches: T[];
/** Maximum number of visible items (for wrapping navigation) */
maxVisible?: number;
/** Callback when an item is selected via Tab or Enter */
/** Callback when an item is selected via Tab (autocomplete only) or Enter (when onAutocomplete is not provided) */
onSelect?: (item: T) => void;
/** Callback when an item is autocompleted via Tab (fill text without executing) */
onAutocomplete?: (item: T) => void;
/** Callback when active state changes (has matches or not) */
onActiveChange?: (isActive: boolean) => void;
/** Skip automatic active state management (for components with async loading) */
@@ -29,6 +31,7 @@ export function useAutocompleteNavigation<T>({
matches,
maxVisible,
onSelect,
onAutocomplete,
onActiveChange,
manageActiveState = true,
disabled = false,
@@ -65,7 +68,17 @@ export function useAutocompleteNavigation<T>({
setSelectedIndex((prev) => (prev > 0 ? prev - 1 : maxIndex));
} else if (key.downArrow) {
setSelectedIndex((prev) => (prev < maxIndex ? prev + 1 : 0));
} else if ((key.tab || key.return) && onSelect) {
} else if (key.tab) {
const selected = matches[selectedIndex];
if (selected) {
// Tab: use onAutocomplete if provided, otherwise fall back to onSelect
if (onAutocomplete) {
onAutocomplete(selected);
} else if (onSelect) {
onSelect(selected);
}
}
} else if (key.return && onSelect) {
const selected = matches[selectedIndex];
if (selected) {
onSelect(selected);