story-research-zapwall/components/unlockAccount/autocompleteKeyDecision.ts
2026-01-13 14:49:19 +01:00

28 lines
905 B
TypeScript

export type AutocompleteKeyDecision =
| { action: 'none' }
| { action: 'escape' }
| { action: 'move'; nextIndex: number }
| { action: 'select'; index: number }
export function decideAutocompleteKeyAction(params: {
key: string
selectedIndex: number
suggestionsCount: number
}): AutocompleteKeyDecision {
if (params.key === 'Escape') {
return { action: 'escape' }
}
if (params.key === 'ArrowDown') {
const nextIndex = params.selectedIndex < params.suggestionsCount - 1 ? params.selectedIndex + 1 : params.selectedIndex
return { action: 'move', nextIndex }
}
if (params.key === 'ArrowUp') {
const nextIndex = params.selectedIndex > 0 ? params.selectedIndex - 1 : -1
return { action: 'move', nextIndex }
}
if (params.key === 'Enter' && params.selectedIndex >= 0) {
return { action: 'select', index: params.selectedIndex }
}
return { action: 'none' }
}