@@ -214,6 +214,7 @@ export interface SelectOptions<Value> {
214
214
options : Option < Value > [ ] ;
215
215
initialValue ?: Value ;
216
216
maxItems ?: number ;
217
+ validate ?: ( value : string ) => string | void ;
217
218
}
218
219
219
220
export const select = < Value > ( opts : SelectOptions < Value > ) => {
@@ -234,6 +235,7 @@ export const select = <Value>(opts: SelectOptions<Value>) => {
234
235
} ;
235
236
236
237
return new SelectPrompt ( {
238
+ validate : opts . validate ,
237
239
options : opts . options ,
238
240
initialValue : opts . initialValue ,
239
241
render ( ) {
@@ -247,6 +249,20 @@ export const select = <Value>(opts: SelectOptions<Value>) => {
247
249
this . options [ this . cursor ] ,
248
250
'cancelled'
249
251
) } \n${ color . gray ( S_BAR ) } `;
252
+ case 'error' : {
253
+ const footer = this . error
254
+ . split ( '\n' )
255
+ . map ( ( ln , i ) =>
256
+ i === 0 ? `${ color . yellow ( S_BAR_END ) } ${ color . yellow ( ln ) } ` : ` ${ ln } `
257
+ )
258
+ . join ( '\n' ) ;
259
+ return `${ title } ${ color . yellow ( S_BAR ) } ${ limitOptions ( {
260
+ cursor : this . cursor ,
261
+ options : this . options ,
262
+ maxItems : opts . maxItems ,
263
+ style : ( item , active ) => opt ( item , active ? 'active' : 'inactive' ) ,
264
+ } ) . join ( `\n${ color . yellow ( S_BAR ) } ` ) } \n${ footer } `;
265
+ }
250
266
default : {
251
267
return `${ title } ${ color . cyan ( S_BAR ) } ${ limitOptions ( {
252
268
cursor : this . cursor ,
@@ -313,6 +329,7 @@ export interface MultiSelectOptions<Value> {
313
329
maxItems ?: number ;
314
330
required ?: boolean ;
315
331
cursorAt ?: Value ;
332
+ validate ?: ( value : Value [ ] ) => string | void ;
316
333
}
317
334
export const multiselect = < Value > ( opts : MultiSelectOptions < Value > ) => {
318
335
const opt = (
@@ -344,6 +361,10 @@ export const multiselect = <Value>(opts: MultiSelectOptions<Value>) => {
344
361
required : opts . required ?? true ,
345
362
cursorAt : opts . cursorAt ,
346
363
validate ( selected : Value [ ] ) {
364
+ if ( opts . validate ) {
365
+ const error = opts . validate ( selected ) ;
366
+ if ( error ) return error ;
367
+ }
347
368
if ( this . required && selected . length === 0 )
348
369
return `Please select at least one option.\n${ color . reset (
349
370
color . dim (
0 commit comments