Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions .changeset/silent-sides-call.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@clack/core": patch
---

Only apply autocomplete default filter if it has been explicitly set or if options is not a getter.
13 changes: 8 additions & 5 deletions packages/core/src/prompts/autocomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export default class AutocompletePrompt<T extends OptionLike> extends Prompt<
focusedValue: T['value'] | undefined;
#cursor = 0;
#lastUserInput = '';
#filterFn: FilterFunction<T>;
#filterFn: FilterFunction<T> | undefined;
#options: T[] | (() => T[]);
#placeholder: string | undefined;

Expand Down Expand Up @@ -106,7 +106,8 @@ export default class AutocompletePrompt<T extends OptionLike> extends Prompt<
const options = this.options;
this.filteredOptions = [...options];
this.multiple = opts.multiple === true;
this.#filterFn = opts.filter ?? defaultFilter;
this.#filterFn =
typeof opts.options === 'function' ? opts.filter : (opts.filter ?? defaultFilter);
let initialValues: unknown[] | undefined;
if (opts.initialValue && Array.isArray(opts.initialValue)) {
if (this.multiple) {
Expand Down Expand Up @@ -160,7 +161,9 @@ export default class AutocompletePrompt<T extends OptionLike> extends Prompt<
const placeholderMatchesOption =
placeholder !== undefined &&
placeholder !== '' &&
options.some((opt) => !opt.disabled && this.#filterFn(placeholder, opt));
options.some(
(opt) => !opt.disabled && (this.#filterFn ? this.#filterFn(placeholder, opt) : true)
);
if (key.name === 'tab' && isEmptyOrOnlyTab && placeholderMatchesOption) {
if (this.userInput === '\t') {
this._clearUserInput();
Expand Down Expand Up @@ -225,8 +228,8 @@ export default class AutocompletePrompt<T extends OptionLike> extends Prompt<

const options = this.options;

if (value) {
this.filteredOptions = options.filter((opt) => this.#filterFn(value, opt));
if (value && this.#filterFn) {
this.filteredOptions = options.filter((opt) => this.#filterFn?.(value, opt));
} else {
this.filteredOptions = [...options];
}
Expand Down
41 changes: 41 additions & 0 deletions packages/core/test/prompts/autocomplete.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,47 @@ describe('AutocompletePrompt', () => {
expect(result).to.equal('apple');
});

test('options as function skips default filter', () => {
const dynamicOptions = [
{ value: 'apple', label: 'Apple' },
{ value: 'banana', label: 'Banana' },
];
const instance = new AutocompletePrompt({
input,
output,
render: () => 'foo',
options: () => dynamicOptions,
});

instance.prompt();

input.emit('keypress', 'z', { name: 'z' });

expect(instance.filteredOptions).toEqual(dynamicOptions);
});

test('options as function applies user-provided filter', () => {
const dynamicOptions = [
{ value: 'apple', label: 'Apple' },
{ value: 'banana', label: 'Banana' },
{ value: 'cherry', label: 'Cherry' },
];
const instance = new AutocompletePrompt({
input,
output,
render: () => 'foo',
options: () => dynamicOptions,
filter: (search, opt) => (opt.label ?? '').toLowerCase().endsWith(search.toLowerCase()),
});

instance.prompt();

input.emit('keypress', 'a', { name: 'a' });

// 'endsWith' matches Banana but not Apple or Cherry
expect(instance.filteredOptions).toEqual([{ value: 'banana', label: 'Banana' }]);
});

test('Tab with non-matching placeholder does not fill input', async () => {
const instance = new AutocompletePrompt({
input,
Expand Down
Loading