Skip to content

Commit c697439

Browse files
authored
feat: add wraparound to autocomplete (#463)
1 parent ba10721 commit c697439

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

‎.changeset/chilly-buses-sort.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@clack/prompts": minor
3+
---
4+
5+
add wraparound behaviour to autocomplete components

‎packages/core/src/prompts/autocomplete.ts‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import type { Key } from 'node:readline';
22
import color from 'picocolors';
33
import Prompt, { type PromptOptions } from './prompt.js';
4+
import { findCursor } from '../utils/cursor.js';
45

56
interface OptionLike {
67
value: unknown;
78
label?: string;
9+
disabled?: boolean;
810
}
911

1012
type FilterFunction<T extends OptionLike> = (search: string, opt: T) => boolean;
@@ -143,10 +145,7 @@ export default class AutocompletePrompt<T extends OptionLike> extends Prompt<
143145

144146
// Start navigation mode with up/down arrows
145147
if (isUpKey || isDownKey) {
146-
this.#cursor = Math.max(
147-
0,
148-
Math.min(this.#cursor + (isUpKey ? -1 : 1), this.filteredOptions.length - 1)
149-
);
148+
this.#cursor = findCursor(this.#cursor, isUpKey ? -1 : 1, this.filteredOptions);
150149
this.focusedValue = this.filteredOptions[this.#cursor]?.value;
151150
if (!this.multiple) {
152151
this.selectedValues = [this.focusedValue];

0 commit comments

Comments
 (0)