Skip to content

Commit 56e9d67

Browse files
authored
docs: add jsdoc for date, limit-options, and messages (#546)
1 parent 0d45e99 commit 56e9d67

5 files changed

Lines changed: 167 additions & 6 deletions

File tree

‎.changeset/twenty-rockets-lose.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 `date`, `limit-options`, and `messages`

‎packages/prompts/README.md‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,16 +93,16 @@ const shouldContinue = await confirm({
9393

9494
### Date
9595

96-
The date component accepts a calendar date and returns a `Date` value.
96+
The `date` prompt provides an interactive date picker, allowing users to navigate between year, month, and day segments and increment/decrement values using keyboard controls.
9797

9898
```js
9999
import { date } from '@clack/prompts';
100100

101-
const dueDate = await date({
102-
message: 'Pick a due date.',
103-
format: 'YMD',
104-
minDate: new Date(Date.UTC(2026, 0, 1)),
105-
maxDate: new Date(Date.UTC(2026, 11, 31)),
101+
const birthday = await date({
102+
message: 'Pick your birthday',
103+
minDate: new Date('1900-01-01'),
104+
initialValue: new Date(),
105+
maxDate: new Date(),
106106
});
107107
```
108108

‎packages/prompts/src/date.ts‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,47 @@ import { type CommonOptions, S_BAR, S_BAR_END, symbol } from './common.js';
55

66
export type { DateFormat };
77

8+
/**
9+
* Options for the {@link date} prompt.
10+
*/
811
export interface DateOptions extends CommonOptions {
12+
/**
13+
* The message or question shown to the user above the input.
14+
*/
915
message: string;
16+
17+
/**
18+
* The date format for the input segments.
19+
* @deafult based on locale
20+
*/
1021
format?: DateFormat;
22+
23+
/**
24+
* The BCP 47 language tag to use for formatting
25+
* @see https://developer.mozilla.org/en-US/docs/Glossary/BCP_47_language_tag
26+
* @example "en-GB"
27+
*/
1128
locale?: string;
29+
30+
/**
31+
* The default value returned when the user doesn't select a date.
32+
*/
1233
defaultValue?: Date;
34+
35+
/**
36+
* The starting date shown when the prompt first renders.
37+
* Users can edit this value before submitting.
38+
*/
1339
initialValue?: Date;
40+
41+
/**
42+
* The minimum allowed date for validation.
43+
*/
1444
minDate?: Date;
45+
46+
/**
47+
* The maximum allowed date for validation.
48+
*/
1549
maxDate?: Date;
1650

1751
/**
@@ -22,6 +56,25 @@ export interface DateOptions extends CommonOptions {
2256
validate?: Validate<Date>;
2357
}
2458

59+
/**
60+
* The `date` prompt provides an interactive date picker, allowing users
61+
* to navigate between year, month, and day segments and
62+
* increment/decrement values using keyboard controls.
63+
*
64+
* @see https://bomb.sh/docs/clack/packages/prompts/#date-input
65+
*
66+
* @example
67+
* ```ts
68+
* import { date } from '@clack/prompts';
69+
*
70+
* const birthday = await date({
71+
* message: 'Pick your birthday',
72+
* minDate: new Date('1900-01-01'),
73+
* initialValue: new Date(),
74+
* maxDate: new Date(),
75+
* });
76+
* ```
77+
*/
2578
export const date = (opts: DateOptions) => {
2679
const validate = opts.validate;
2780
return new DatePrompt({

‎packages/prompts/src/limit-options.ts‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,44 @@ import { getColumns, getRows } from '@clack/core';
33
import { wrapAnsi } from 'fast-wrap-ansi';
44
import type { CommonOptions } from './common.js';
55

6+
/**
7+
* Options for the {@link limitOptions} function.
8+
*/
69
export interface LimitOptionsParams<TOption> extends CommonOptions {
10+
/**
11+
* The list of options to display.
12+
*/
713
options: TOption[];
14+
15+
/**
16+
* The index of the currently active/selected option.
17+
*/
818
cursor: number;
19+
20+
/**
21+
* A function that styles the given option string.
22+
*
23+
* @param option - The option string to style.
24+
* @param active - Whether the option is currently selected.
25+
*/
926
style: (option: TOption, active: boolean) => string;
27+
28+
/**
29+
* Maximum number of options to display at once.
30+
* @default Infinity
31+
*/
1032
maxItems?: number;
33+
34+
/**
35+
* Number of columns to reserve for padding.
36+
* @default 0
37+
*/
1138
columnPadding?: number;
39+
40+
/**
41+
* Number of rows to reserve for padding.
42+
* @default 4
43+
*/
1244
rowPadding?: number;
1345
}
1446

@@ -38,6 +70,29 @@ const trimLines = (
3870
return { lineCount, removals };
3971
};
4072

73+
/**
74+
* Trims an option list to what fits the terminal, while keeping the active
75+
* option (cursor) visible using a Clack style sliding window.
76+
*
77+
* @returns The lines to render.
78+
*
79+
* @see https://bomb.sh/docs/clack/packages/prompts/#limitoptions
80+
*
81+
* @example
82+
* ```ts
83+
* import { limitOptions } from '@clack/prompts';
84+
* import { styleText } from 'node:util';
85+
*
86+
* const options = ['apple', 'banana', 'cherry', 'date'];
87+
* const lines = limitOptions({
88+
* options,
89+
* cursor: 2,
90+
* maxItems: 8,
91+
* style: (opt, active) =>
92+
* active ? styleText('cyan', opt) : styleText('dim', opt),
93+
* });
94+
* ```
95+
*/
4196
export const limitOptions = <TOption>({
4297
cursor,
4398
options,

‎packages/prompts/src/messages.ts‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,68 @@ import { styleText } from 'node:util';
33
import { settings } from '@clack/core';
44
import { type CommonOptions, S_BAR, S_BAR_END, S_BAR_START } from './common.js';
55

6+
/**
7+
* The `cancel` function defines an interruption of an interaction
8+
* and therefore its end.
9+
*
10+
* @param title Optional closing message to be displayed.
11+
* @param opts Additional configuration options.
12+
*
13+
* @see https://bomb.sh/docs/clack/packages/prompts/#cancel
14+
*
15+
* @example
16+
* ```ts
17+
* import { cancel } from '@clack/prompts';
18+
* import process from 'node:process';
19+
*
20+
* cancel('Installation canceled');
21+
* process.exit(1);
22+
* ```
23+
*/
624
export const cancel = (message = '', opts?: CommonOptions) => {
725
const output: Writable = opts?.output ?? process.stdout;
826
const hasGuide = opts?.withGuide ?? settings.withGuide;
927
const prefix = hasGuide ? `${styleText('gray', S_BAR_END)} ` : '';
1028
output.write(`${prefix}${styleText('red', message)}\n\n`);
1129
};
1230

31+
/**
32+
* The `intro` function defines the beginning of an interaction.
33+
*
34+
* @param title Optional title to be displayed.
35+
* @param opts Additional configuration options.
36+
*
37+
* @see https://bomb.sh/docs/clack/packages/prompts/#intro
38+
*
39+
* @example
40+
* ```ts
41+
* import { intro } from '@clack/prompts';
42+
*
43+
* intro('Welcome to clack');
44+
* ```
45+
*/
1346
export const intro = (title = '', opts?: CommonOptions) => {
1447
const output: Writable = opts?.output ?? process.stdout;
1548
const hasGuide = opts?.withGuide ?? settings.withGuide;
1649
const prefix = hasGuide ? `${styleText('gray', S_BAR_START)} ` : '';
1750
output.write(`${prefix}${title}\n`);
1851
};
1952

53+
/**
54+
* The `outro` function defines the end of an interaction.
55+
*
56+
* @param title Optional closing message to be displayed.
57+
* @param opts Additional configuration options.
58+
*
59+
* @see https://bomb.sh/docs/clack/packages/prompts/#outro
60+
*
61+
* @example
62+
* ```ts
63+
* import { outro } from '@clack/prompts';
64+
*
65+
* outro('All operations are finished');
66+
* ```
67+
*/
2068
export const outro = (message = '', opts?: CommonOptions) => {
2169
const output: Writable = opts?.output ?? process.stdout;
2270
const hasGuide = opts?.withGuide ?? settings.withGuide;

0 commit comments

Comments
 (0)