Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions packages/prompts/src/autocomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ export interface AutocompleteOptions<Value> extends CommonOptions {
}

export const autocomplete = <Value>(opts: AutocompleteOptions<Value>) => {
const prompt = new AutocompletePrompt<Option<Value>>({
const prompt = new AutocompletePrompt({
options: opts.options,
placeholder: opts.placeholder,
initialValue: opts.initialValue ? [{ value: opts.initialValue }] : undefined,
initialValue: opts.initialValue ? [opts.initialValue] : undefined,
filter: (search: string, opt: Option<Value>) => {
return getFilteredOption(search, opt);
},
Expand All @@ -83,10 +83,7 @@ export const autocomplete = <Value>(opts: AutocompleteOptions<Value>) => {
switch (this.state) {
case 'submit': {
// Show selected value
const selected = getSelectedOptions(
this.selectedValues.map((v) => v),
this.options
);
const selected = getSelectedOptions(this.selectedValues, this.options);
const label = selected.length > 0 ? selected.map(getLabel).join(', ') : '';
return `${title}${color.gray(S_BAR)} ${color.dim(label)}`;
}
Expand Down Expand Up @@ -208,11 +205,16 @@ export const autocompleteMultiselect = <Value>(opts: AutocompleteMultiSelectOpti
input: opts.input,
output: opts.output,
render() {
const formatOption = (option: Option<Value>, active: boolean, selectedValues: Value[]) => {
const formatOption = (
option: Option<Value>,
active: boolean,
selectedValues: Value[],
focusedValue: Value | undefined
) => {
const isSelected = selectedValues.includes(option.value);
const label = option.label ?? String(option.value ?? '');
const hint =
option.hint && this.focusedValue !== undefined && option.value === this.focusedValue
option.hint && focusedValue !== undefined && option.value === focusedValue
? color.dim(` (${option.hint})`)
: '';
const checkbox = isSelected
Expand Down Expand Up @@ -274,7 +276,8 @@ export const autocompleteMultiselect = <Value>(opts: AutocompleteMultiSelectOpti
const displayOptions = limitOptions({
cursor: this.cursor,
options: this.filteredOptions,
style: (option, active) => formatOption(option, active, this.selectedValues),
style: (option, active) =>
formatOption(option, active, this.selectedValues, this.focusedValue),
maxItems: opts.maxItems,
output: opts.output,
});
Expand Down
201 changes: 201 additions & 0 deletions packages/prompts/test/__snapshots__/autocomplete.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`autocomplete > limits displayed options when maxItems is set 1`] = `
[
"<cursor.hide>",
"│
◆ Select a fruit

│ Search: _
│ ● Apple
│ ○ Banana
│ ○ Cherry
│ ○ Grape
│ ○ Orange
│ ↑/↓ to select • Enter: confirm • Type: to search
└",
]
`;

exports[`autocomplete > renders initial UI with message and instructions 1`] = `
[
"<cursor.hide>",
"│
◆ Select a fruit

│ Search: _
│ ● Apple
│ ○ Banana
│ ○ Cherry
│ ○ Grape
│ ○ Orange
│ ↑/↓ to select • Enter: confirm • Type: to search
└",
]
`;

exports[`autocomplete > shows hint when option has hint and is focused 1`] = `
[
"<cursor.hide>",
"│
◆ Select a fruit

│ Search: _
│ ● Apple
│ ○ Banana
│ ○ Cherry
│ ○ Grape
│ ○ Orange
│ ○ Kiwi
│ ↑/↓ to select • Enter: confirm • Type: to search
└",
"<cursor.backward count=999><cursor.up count=11>",
"<cursor.down count=3>",
"<erase.down>",
"│ Search:
│ ○ Apple
│ ● Banana
│ ○ Cherry
│ ○ Grape
│ ○ Orange
│ ○ Kiwi
│ ↑/↓ to select • Enter: confirm • Type: to search
└",
"<cursor.backward count=999><cursor.up count=11>",
"<cursor.down count=5>",
"<erase.down>",
"│ ○ Banana
│ ● Cherry
│ ○ Grape
│ ○ Orange
│ ○ Kiwi
│ ↑/↓ to select • Enter: confirm • Type: to search
└",
"<cursor.backward count=999><cursor.up count=11>",
"<cursor.down count=6>",
"<erase.down>",
"│ ○ Cherry
│ ● Grape
│ ○ Orange
│ ○ Kiwi
│ ↑/↓ to select • Enter: confirm • Type: to search
└",
"<cursor.backward count=999><cursor.up count=11>",
"<cursor.down count=7>",
"<erase.down>",
"│ ○ Grape
│ ● Orange
│ ○ Kiwi
│ ↑/↓ to select • Enter: confirm • Type: to search
└",
"<cursor.backward count=999><cursor.up count=11>",
"<cursor.down count=8>",
"<erase.down>",
"│ ○ Orange
│ ● Kiwi (New Zealand)
│ ↑/↓ to select • Enter: confirm • Type: to search
└",
]
`;

exports[`autocomplete > shows no matches message when search has no results 1`] = `
[
"<cursor.hide>",
"│
◆ Select a fruit

│ Search: _
│ ● Apple
│ ○ Banana
│ ○ Cherry
│ ○ Grape
│ ○ Orange
│ ↑/↓ to select • Enter: confirm • Type: to search
└",
"<cursor.backward count=999><cursor.up count=10>",
"<cursor.down count=3>",
"<erase.down>",
"│ Search: z█ (0 matches)
│ No matches found
│ ↑/↓ to select • Enter: confirm • Type: to search
└",
]
`;

exports[`autocomplete > shows placeholder when provided 1`] = `
[
"<cursor.hide>",
"│
◆ Select a fruit

│ Search: _
│ ● Apple
│ ○ Banana
│ ○ Cherry
│ ○ Grape
│ ○ Orange
│ ↑/↓ to select • Enter: confirm • Type: to search
└",
]
`;

exports[`autocomplete > shows selected value in submit state 1`] = `
[
"<cursor.hide>",
"│
◆ Select a fruit

│ Search: _
│ ● Apple
│ ○ Banana
│ ○ Cherry
│ ○ Grape
│ ○ Orange
│ ↑/↓ to select • Enter: confirm • Type: to search
└",
"<cursor.backward count=999><cursor.up count=10>",
"<cursor.down count=3>",
"<erase.down>",
"│ Search:
│ ○ Apple
│ ● Banana
│ ○ Cherry
│ ○ Grape
│ ○ Orange
│ ↑/↓ to select • Enter: confirm • Type: to search
└",
"<cursor.backward count=999><cursor.up count=10>",
"<cursor.down count=1>",
"<erase.down>",
"◇ Select a fruit
│ Banana",
"
",
"<cursor.show>",
]
`;

exports[`autocomplete > shows strikethrough in cancel state 1`] = `
[
"<cursor.hide>",
"│
◆ Select a fruit

│ Search: _
│ ● Apple
│ ○ Banana
│ ○ Cherry
│ ○ Grape
│ ○ Orange
│ ↑/↓ to select • Enter: confirm • Type: to search
└",
"<cursor.backward count=999><cursor.up count=10>",
"<cursor.down count=1>",
"<erase.down>",
"■ Select a fruit
│",
"
",
"<cursor.show>",
]
`;
Loading