MCPcopy Create free account
hub / github.com/bombshell-dev/clack / date

Function date

packages/prompts/src/date.ts:78–137  ·  view source on GitHub ↗
(opts: DateOptions)

Source from the content-addressed store, hash-verified

76 * ```
77 */
78export const date = (opts: DateOptions) => {
79 const validate = opts.validate;
80 return new DatePrompt({
81 ...opts,
82 validate(value: Date | undefined) {
83 if (value === undefined) {
84 if (opts.defaultValue !== undefined) return undefined;
85 if (validate) return runValidation(validate, value);
86 return settings.date.messages.required;
87 }
88 const iso = (d: Date) => d.toISOString().slice(0, 10);
89 if (opts.minDate && iso(value) < iso(opts.minDate)) {
90 return settings.date.messages.afterMin(opts.minDate);
91 }
92 if (opts.maxDate && iso(value) > iso(opts.maxDate)) {
93 return settings.date.messages.beforeMax(opts.maxDate);
94 }
95 if (validate) return runValidation(validate, value);
96 return undefined;
97 },
98 render() {
99 const hasGuide = (opts?.withGuide ?? settings.withGuide) !== false;
100 const titlePrefix = `${hasGuide ? `${styleText('gray', S_BAR)}\n` : ''}${symbol(this.state)} `;
101 const title = `${titlePrefix}${opts.message}\n`;
102
103 const state = this.state !== 'initial' ? this.state : 'active';
104
105 const userInput = renderDate(this, state);
106 const value = this.value instanceof Date ? this.formattedValue : '';
107
108 switch (this.state) {
109 case 'error': {
110 const errorText = this.error ? ` ${styleText('yellow', this.error)}` : '';
111 const bar = hasGuide ? `${styleText('yellow', S_BAR)} ` : '';
112 const barEnd = hasGuide ? styleText('yellow', S_BAR_END) : '';
113 return `${title.trim()}\n${bar}${userInput}\n${barEnd}${errorText}\n`;
114 }
115 case 'submit': {
116 const valueText = value ? ` ${styleText('dim', value)}` : '';
117 const bar = hasGuide ? styleText('gray', S_BAR) : '';
118 return `${title}${bar}${valueText}`;
119 }
120 case 'cancel': {
121 const valueText = value ? ` ${styleText(['strikethrough', 'dim'], value)}` : '';
122 const bar = hasGuide ? styleText('gray', S_BAR) : '';
123 return `${title}${bar}${valueText}${value.trim() ? `\n${bar}` : ''}`;
124 }
125 default: {
126 const bar = hasGuide ? `${styleText('cyan', S_BAR)} ` : '';
127 const barEnd = hasGuide ? styleText('cyan', S_BAR_END) : '';
128 const inlineBar = hasGuide ? `${styleText('cyan', S_BAR)} ` : '';
129 const inlineError = this.inlineError
130 ? `\n${inlineBar}${styleText('yellow', this.inlineError)}`
131 : '';
132 return `${title}${bar}${userInput}${inlineError}\n${barEnd}\n`;
133 }
134 }
135 },

Callers

nothing calls this directly

Calls 1

promptMethod · 0.80

Tested by

no test coverage detected