Skip to content

Commit adb6af9

Browse files
authored
docs: add jsdoc for box, group, and group-multi-select (#542)
1 parent 3170ed9 commit adb6af9

4 files changed

Lines changed: 166 additions & 4 deletions

File tree

.changeset/giant-doors-rest.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 `box`, `group`, and `group-multi-select`

packages/prompts/src/box.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ import {
1616
S_CORNER_TOP_RIGHT,
1717
} from './common.js';
1818

19+
/**
20+
* Alignment for content or titles within the box.
21+
*/
1922
export type BoxAlignment = 'left' | 'center' | 'right';
2023

2124
type BoxSymbols = [topLeft: string, topRight: string, bottomLeft: string, bottomRight: string];
@@ -28,13 +31,49 @@ const roundedSymbols: BoxSymbols = [
2831
];
2932
const squareSymbols: BoxSymbols = [S_BAR_START, S_BAR_START_RIGHT, S_BAR_END, S_BAR_END_RIGHT];
3033

34+
/**
35+
* Options for the {@link box} prompt.
36+
*/
3137
export interface BoxOptions extends CommonOptions {
38+
/**
39+
* Alignment of the content (`'left'`, `'center'`, or `'right'`).
40+
* @default 'left'
41+
*/
3242
contentAlign?: BoxAlignment;
43+
44+
/**
45+
* Alignment of the title (`'left'`, `'center'`, or `'right'`).
46+
* @default 'left'
47+
*/
3348
titleAlign?: BoxAlignment;
49+
50+
/**
51+
* The width of the box, either `'auto'` to fit the content or a number for a fixed width.
52+
* @default 'auto'
53+
*/
3454
width?: number | 'auto';
55+
56+
/**
57+
* Padding around the title.
58+
* @default 1
59+
*/
3560
titlePadding?: number;
61+
62+
/**
63+
* Padding around the content.
64+
* @default 2
65+
*/
3666
contentPadding?: number;
67+
68+
/**
69+
* Use rounded corners when `true`, square corners when `false`.
70+
* @default true
71+
*/
3772
rounded?: boolean;
73+
74+
/**
75+
* Custom function to style the border characters.
76+
*/
3877
formatBorder?: (text: string) => string;
3978
}
4079

@@ -59,6 +98,28 @@ function getPaddingForLine(
5998

6099
const defaultFormatBorder = (text: string) => text;
61100

101+
/**
102+
* Renders a customizable box around text content. It's similar to {@link note} but offers
103+
* more styling options.
104+
*
105+
* @see https://bomb.sh/docs/clack/packages/prompts/#box
106+
*
107+
* @param message - The content to display inside the box.
108+
* @param title - The title to display in the top border of the box.
109+
* @param opts - Optional configuration for the box styling and behavior.
110+
*
111+
* @example
112+
* ```ts
113+
* import { box } from '@clack/prompts';
114+
*
115+
* box('This is the content of the box', 'Box Title', {
116+
* contentAlign: 'center',
117+
* titleAlign: 'center',
118+
* width: 'auto',
119+
* rounded: true,
120+
* });
121+
* ```
122+
*/
62123
export const box = (message = '', title = '', opts?: BoxOptions) => {
63124
const output: Writable = opts?.output ?? process.stdout;
64125
const columns = getColumns(output);

packages/prompts/src/group-multi-select.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,81 @@ import {
1212
import { limitOptions } from './limit-options.js';
1313
import type { Option } from './select.js';
1414

15+
/**
16+
* Options for the {@link groupMultiselect} prompt.
17+
*/
1518
export interface GroupMultiSelectOptions<Value> extends CommonOptions {
19+
/**
20+
* The message or question shown to the user above the input.
21+
*/
1622
message: string;
23+
24+
/**
25+
* Grouped options to display. Each key is a group label, and each value is an array of options.
26+
*/
1727
options: Record<string, Option<Value>[]>;
28+
29+
/**
30+
* The initially selected option(s).
31+
*/
1832
initialValues?: Value[];
33+
34+
/**
35+
* The maximum number of items/options to display at once.
36+
*/
1937
maxItems?: number;
38+
39+
/**
40+
* When `true` at least one option must be selected.
41+
* @default true
42+
*/
2043
required?: boolean;
44+
45+
/**
46+
* The value the cursor should be positioned at initially.
47+
*/
2148
cursorAt?: Value;
49+
50+
/**
51+
* Whether entire groups can be selected at once.
52+
* @default true
53+
*/
2254
selectableGroups?: boolean;
55+
56+
/**
57+
* Number of blank lines between groups.
58+
* @default 0
59+
*/
2360
groupSpacing?: number;
2461
}
62+
63+
/**
64+
* The `groupMultiselect` prompt extends the {@link multiselect} prompt to allow
65+
* arranging distinct Multi-Selects, whilst keeping all of them interactive.
66+
*
67+
* @see https://bomb.sh/docs/clack/packages/prompts/#group-multiselect
68+
*
69+
* @example
70+
* ```ts
71+
* import { groupMultiselect } from '@clack/prompts';
72+
*
73+
* const result = await groupMultiselect({
74+
* message: 'Define your project',
75+
* options: {
76+
* 'Testing': [
77+
* { value: 'Jest', hint: 'JavaScript testing framework' },
78+
* { value: 'Playwright', hint: 'End-to-end testing' },
79+
* ],
80+
* 'Language': [
81+
* { value: 'js', label: 'JavaScript', hint: 'Dynamic typing' },
82+
* { value: 'ts', label: 'TypeScript', hint: 'Static typing' },
83+
* ],
84+
* },
85+
* });
86+
* ```
87+
*
88+
* @param opts The options for the group multiselect prompt
89+
*/
2590
export const groupMultiselect = <Value>(opts: GroupMultiSelectOptions<Value>) => {
2691
const { selectableGroups = true, groupSpacing = 0 } = opts;
2792
const opt = (

packages/prompts/src/group.ts

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,58 @@ type Prettify<T> = {
44
[P in keyof T]: T[P];
55
} & {};
66

7+
/**
8+
* The return type of a {@link PromptGroup}.
9+
* Resolves all prompt results, excluding the cancel symbol.
10+
*/
711
export type PromptGroupAwaitedReturn<T> = {
812
[P in keyof T]: Exclude<Awaited<T[P]>, symbol>;
913
};
1014

15+
/**
16+
* Options for the {@link group} utility.
17+
*/
1118
export interface PromptGroupOptions<T> {
1219
/**
13-
* Control how the group can be canceled
14-
* if one of the prompts is canceled.
20+
* Called when any one of the prompts is canceled.
1521
*/
1622
onCancel?: (opts: { results: Prettify<Partial<PromptGroupAwaitedReturn<T>>> }) => void;
1723
}
1824

25+
/**
26+
* A group of prompts to be displayed sequentially, with each prompt receiving
27+
* the results of all previous prompts in the group.
28+
*/
1929
export type PromptGroup<T> = {
2030
[P in keyof T]: (opts: {
2131
results: Prettify<Partial<PromptGroupAwaitedReturn<Omit<T, P>>>>;
2232
}) => undefined | Promise<T[P] | undefined>;
2333
};
2434

2535
/**
26-
* Define a group of prompts to be displayed
27-
* and return a results of objects within the group
36+
* The `group` utility provides a consistent way to combine a series of prompts,
37+
* combining each answer into one object. Each prompt receives the results of
38+
* all previously completed prompts, and are displayed sequentially.
39+
*
40+
* @see https://bomb.sh/docs/clack/packages/prompts/#group
41+
*
42+
* @example
43+
* ```ts
44+
* import { group, text, password } from '@clack/prompts';
45+
*
46+
* const account = await group({
47+
* email: () => text({
48+
* message: 'What is your email address?',
49+
* }),
50+
* username: ({ results }) => text({
51+
* message: 'What is your username?',
52+
* placeholder: results.email?.replace(/@.+$/, '').toLowerCase() ?? '',
53+
* }),
54+
* password: () => password({
55+
* message: 'Define your password',
56+
* }),
57+
* });
58+
* ```
2859
*/
2960
export const group = async <T>(
3061
prompts: PromptGroup<T>,

0 commit comments

Comments
 (0)