Skip to content

Commit c49d794

Browse files
committed
docs: add jsdoc for autocomplete, confirm, and path prompts
1 parent 3dcb31a commit c49d794

5 files changed

Lines changed: 192 additions & 17 deletions

File tree

.changeset/yellow-bats-listen.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@clack/prompts": patch
3+
---
4+
5+
docs: add jsdoc for `autocomplete`, `confirm`, and `path` prompts

packages/prompts/README.md

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ const secret = await password({
8181

8282
### Confirm
8383

84-
The confirm component accepts a yes or no answer. The result is a boolean value of `true` or `false`.
84+
The `confirm` prompt accepts a yes or no choice, returning a boolean value corresponding to the user's selection.
8585

8686
```js
8787
import { confirm } from '@clack/prompts';
@@ -125,7 +125,7 @@ const projectType = await select({
125125

126126
### Autocomplete
127127

128-
The autocomplete component lets a user filter a list by typing, then choose one option from the matching results. By default, matching uses each option's `label`, `hint`, and `value`. The result is the selected option's `value`.
128+
The `autocomplete` prompt combines text input with a searchable list of options. It's perfect for when you have a large list of options and want to help users find what they're looking for quickly.
129129

130130
```js
131131
import { autocomplete } from '@clack/prompts';
@@ -142,6 +142,27 @@ const framework = await autocomplete({
142142
});
143143
```
144144

145+
### Autocomplete Multi-Select
146+
147+
The `autocompleteMultiselect` prompt combines the search functionality of [autocomplete](#autocomplete) with the ability to select multiple options.
148+
149+
```js
150+
import { autocomplete } from '@clack/prompts';
151+
152+
const framework = await autocomplete({
153+
message: 'Search for a framework',
154+
options: [
155+
{ value: 'next', label: 'Next.js', hint: 'React framework' },
156+
{ value: 'astro', label: 'Astro', hint: 'Content-focused' },
157+
{ value: 'svelte', label: 'SvelteKit', hint: 'Compile-time framework' },
158+
{ value: 'remix', label: 'Remix', hint: 'Full stack framework' },
159+
{ value: 'nuxt', label: 'Nuxt', hint: 'Vue framework' },
160+
],
161+
placeholder: 'Type to search...',
162+
maxItems: 5,
163+
});
164+
```
165+
145166
### Select Key
146167

147168
The `selectKey` component lets a user choose an option by pressing its single-character string `value` key directly.
@@ -226,7 +247,7 @@ const bio = await multiline({
226247

227248
### Path
228249

229-
The path component offers filesystem path suggestions and returns the selected path as a string. When `directory: true` is set, only directories can be selected.
250+
The `path` prompt extends [`autocomplete`](#autocomplete) to provide file and directory suggestions.
230251

231252
```js
232253
import { path } from '@clack/prompts';

packages/prompts/src/autocomplete.ts

Lines changed: 76 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,45 +41,83 @@ function getSelectedOptions<T>(values: T[], options: Option<T>[]): Option<T>[] {
4141
return results;
4242
}
4343

44+
/**
45+
* Options for the {@link autocomplete} prompt.
46+
*/
4447
interface AutocompleteSharedOptions<Value> extends CommonOptions {
4548
/**
46-
* The message to display to the user.
49+
* The prompt message or question shown to the user above the input.
4750
*/
4851
message: string;
52+
4953
/**
50-
* Available options for the autocomplete prompt.
54+
* The options to present, or a function that returns the options to present
55+
* allowing for custom search/filtering.
56+
*
57+
* @see https://bomb.sh/docs/clack/packages/prompts/#dynamic-options-getter
5158
*/
5259
options: Option<Value>[] | ((this: AutocompletePrompt<Option<Value>>) => Option<Value>[]);
60+
5361
/**
54-
* Maximum number of items to display at once.
62+
* The maximum number of items/options to display in the autocomplete list at once.
5563
*/
5664
maxItems?: number;
65+
5766
/**
58-
* Placeholder text to display when no input is provided.
67+
* Placeholder text displayed when the search field is empty. When set, pressing
68+
* tab copies the placeholder into the input.
5969
*/
6070
placeholder?: string;
71+
6172
/**
62-
* Validates the value
73+
* A function that validates user input. Return a `string` or `Error` to show as a
74+
* validation error, or `undefined` to accept the result.
6375
*/
6476
validate?: (value: Value | Value[] | undefined) => string | Error | undefined;
77+
6578
/**
66-
* Custom filter function to match options against search input.
67-
* If not provided, a default filter that matches label, hint, and value is used.
79+
* Custom filter function to match options against the search input.
6880
*/
6981
filter?: (search: string, option: Option<Value>) => boolean;
7082
}
7183

7284
export interface AutocompleteOptions<Value> extends AutocompleteSharedOptions<Value> {
7385
/**
74-
* The initial selected value.
86+
* The initially selected option from the list.
7587
*/
7688
initialValue?: Value;
89+
7790
/**
78-
* The initial user input
91+
* The starting value shown in the users input box.
7992
*/
8093
initialUserInput?: string;
8194
}
8295

96+
/**
97+
* The `autocomplete` prompt combines a text input with a searchable list of options.
98+
* It's perfect for when you have a large list of options and want to help users
99+
* find what they're looking for quickly.
100+
*
101+
* @see https://bomb.sh/docs/clack/packages/prompts/#autocomplete
102+
*
103+
* @example
104+
* ```ts
105+
* import { autocomplete } from '@clack/prompts';
106+
*
107+
* const framework = await autocomplete({
108+
* message: 'Search for a framework',
109+
* options: [
110+
* { value: 'next', label: 'Next.js', hint: 'React framework' },
111+
* { value: 'astro', label: 'Astro', hint: 'Content-focused' },
112+
* { value: 'svelte', label: 'SvelteKit', hint: 'Compile-time framework' },
113+
* { value: 'remix', label: 'Remix', hint: 'Full stack framework' },
114+
* { value: 'nuxt', label: 'Nuxt', hint: 'Vue framework' },
115+
* ],
116+
* placeholder: 'Type to search...',
117+
* maxItems: 5,
118+
* });
119+
* ```
120+
*/
83121
export const autocomplete = <Value>(opts: AutocompleteOptions<Value>) => {
84122
const prompt = new AutocompletePrompt({
85123
options: opts.options,
@@ -223,20 +261,45 @@ export const autocomplete = <Value>(opts: AutocompleteOptions<Value>) => {
223261
return prompt.prompt() as Promise<Value | symbol>;
224262
};
225263

226-
// Type definition for the autocompleteMultiselect component
264+
/**
265+
* Options for the {@link autocompleteMultiselect} prompt
266+
*/
227267
export interface AutocompleteMultiSelectOptions<Value> extends AutocompleteSharedOptions<Value> {
228268
/**
229-
* The initial selected values
269+
* The initially selected option(s) from the list.
230270
*/
231271
initialValues?: Value[];
272+
232273
/**
233-
* If true, at least one option must be selected
274+
* When `true`, at least one option must be selected.
275+
* @default false
234276
*/
235277
required?: boolean;
236278
}
237279

238280
/**
239-
* Integrated autocomplete multiselect - combines type-ahead filtering with multiselect in one UI
281+
* The `autocompleteMultiselect` prompt combines the search functionality of autocomplete
282+
* with the ability to select multiple options.
283+
*
284+
* @see https://bomb.sh/docs/clack/packages/prompts/#autocomplete-multiselect
285+
*
286+
* @example
287+
* ```ts
288+
* import { autocompleteMultiselect } from '@clack/prompts';
289+
*
290+
* const frameworks = await autocompleteMultiselect({
291+
* message: 'Select frameworks',
292+
* options: [
293+
* { value: 'next', label: 'Next.js', hint: 'React framework' },
294+
* { value: 'astro', label: 'Astro', hint: 'Content-focused' },
295+
* { value: 'svelte', label: 'SvelteKit', hint: 'Compile-time framework' },
296+
* { value: 'remix', label: 'Remix', hint: 'Full stack framework' },
297+
* { value: 'nuxt', label: 'Nuxt', hint: 'Vue framework' },
298+
* ],
299+
* placeholder: 'Type to search...',
300+
* maxItems: 5,
301+
* });
302+
* ```
240303
*/
241304
export const autocompleteMultiselect = <Value>(opts: AutocompleteMultiSelectOptions<Value>) => {
242305
const formatOption = (

packages/prompts/src/confirm.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,55 @@ import {
99
symbol,
1010
} from './common.js';
1111

12+
/**
13+
* Options for the {@link confirm} prompt.
14+
*/
1215
export interface ConfirmOptions extends CommonOptions {
16+
/**
17+
* The prompt message or question shown to the user above the input.
18+
*/
1319
message: string;
20+
21+
/**
22+
* The label to use for the active (true) option.
23+
* @default 'Yes'
24+
*/
1425
active?: string;
26+
27+
/**
28+
* The label to use for the inactive (false) option.
29+
* @default 'No'
30+
*/
1531
inactive?: string;
32+
33+
/**
34+
* The initial selected value (true or false).
35+
* @default true
36+
*/
1637
initialValue?: boolean;
38+
39+
/**
40+
* Whether to render the options vertically instead of horizontally.
41+
* @default false
42+
*/
1743
vertical?: boolean;
1844
}
45+
46+
/**
47+
* The `confirm` prompt accepts a yes or no choice, returning a boolean value
48+
* corresponding to the user's selection.
49+
*
50+
* @see https://bomb.sh/docs/clack/packages/prompts/#confirmation
51+
*
52+
* @example
53+
* ```ts
54+
* import { confirm } from '@clack/prompts';
55+
*
56+
* const shouldProceed = await confirm({
57+
* message: 'Do you want to continue?',
58+
* });
59+
* ```
60+
*/
1961
export const confirm = (opts: ConfirmOptions) => {
2062
const active = opts.active ?? 'Yes';
2163
const inactive = opts.inactive ?? 'No';

packages/prompts/src/path.ts

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,58 @@ import { dirname, join } from 'node:path';
33
import { autocomplete } from './autocomplete.js';
44
import type { CommonOptions } from './common.js';
55

6+
/**
7+
* Options for the {@link path} prompt.
8+
*/
69
export interface PathOptions extends CommonOptions {
10+
/**
11+
* The prompt message or question shown to the user above the input.
12+
*/
13+
message: string;
14+
15+
/**
16+
* The starting directory for path suggestions (defaults to current working directory).
17+
*/
718
root?: string;
19+
20+
/**
21+
* When `true`, only **directories** appear in suggestions while you navigate.
22+
*/
823
directory?: boolean;
24+
25+
/**
26+
* The starting path shown when the prompt first renders, which users can edit
27+
* before submitting. If not provided it will fall back to the given `root`,
28+
* or the current working directory.
29+
*
30+
* In `directory` mode, if the initial value points to a directory that exists,
31+
* pressing enter will submit the input instead of jumping to the first child.
32+
*/
933
initialValue?: string;
10-
message: string;
34+
35+
/**
36+
* A function that validates the given path. Return a `string` or `Error` to show as a
37+
* validation error, or `undefined` to accept the result.
38+
*/
1139
validate?: (value: string | undefined) => string | Error | undefined;
1240
}
1341

42+
/**
43+
* The `path` prompt extends `autocomplete` to provide file and directory suggestions.
44+
*
45+
* @see https://bomb.sh/docs/clack/packages/prompts/#path-selection
46+
*
47+
* @example
48+
* ```ts
49+
* import { path } from '@clack/prompts';
50+
*
51+
* const result = await path({
52+
* message: 'Select a file:',
53+
* root: process.cwd(),
54+
* directory: false,
55+
* });
56+
* ```
57+
*/
1458
export const path = (opts: PathOptions) => {
1559
const validate = opts.validate;
1660

0 commit comments

Comments
 (0)