[READ-ONLY] Mirror of https://github.com/bombshell-dev/clack. Effortlessly build beautiful command-line apps bomb.sh/docs/clack/basics/getting-started/
cli command-line command-line-app node prompt prompts
5

Configure Feed

Select the types of activity you want to include in your feed.

fix(#44) complex values (#86)

authored by

Nate Moore and committed by
GitHub
(Feb 26, 2023, 4:35 PM -0600) c729bf06 2be262bf

+30 -28
+5
.changeset/olive-birds-brush.md
··· 1 + --- 2 + "@clack/prompts": patch 3 + --- 4 + 5 + Support complex value types for `select`, `multiselect` and `groupMultiselect`.
+25 -28
packages/prompts/src/index.ts
··· 169 169 }; 170 170 171 171 type Primitive = Readonly<string | boolean | number>; 172 - interface Option<Value extends Primitive> { 173 - value: Value; 174 - label?: string; 175 - hint?: string; 176 - } 177 - export interface SelectOptions<Options extends Option<Value>[], Value extends Primitive> { 172 + 173 + type Option<Value> = Value extends Primitive 174 + ? { value: Value; label?: string; hint?: string } 175 + : { value: Value; label: string; hint?: string }; 176 + 177 + export interface SelectOptions<Options extends Option<Value>[], Value> { 178 178 message: string; 179 179 options: Options; 180 - initialValue?: Options[number]['value']; 180 + initialValue?: Value; 181 181 } 182 182 183 - export const select = <Options extends Option<Value>[], Value extends Primitive>( 183 + export const select = <Options extends Option<Value>[], Value>( 184 184 opts: SelectOptions<Options, Value> 185 185 ) => { 186 - const opt = ( 187 - option: Options[number], 188 - state: 'inactive' | 'active' | 'selected' | 'cancelled' 189 - ) => { 186 + const opt = (option: Option<Value>, state: 'inactive' | 'active' | 'selected' | 'cancelled') => { 190 187 const label = option.label ?? String(option.value); 191 188 if (state === 'active') { 192 189 return `${color.green(S_RADIO_ACTIVE)} ${label} ${ ··· 221 218 } 222 219 } 223 220 }, 224 - }).prompt() as Promise<Options[number]['value'] | symbol>; 221 + }).prompt() as Promise<Value | symbol>; 225 222 }; 226 223 227 224 export const selectKey = <Options extends Option<Value>[], Value extends string>( 228 225 opts: SelectOptions<Options, Value> 229 226 ) => { 230 227 const opt = ( 231 - option: Options[number], 228 + option: Option<Value>, 232 229 state: 'inactive' | 'active' | 'selected' | 'cancelled' = 'inactive' 233 230 ) => { 234 231 const label = option.label ?? String(option.value); ··· 269 266 } 270 267 } 271 268 }, 272 - }).prompt() as Promise<Options[number]['value'] | symbol>; 269 + }).prompt() as Promise<Value | symbol>; 273 270 }; 274 271 275 - export interface MultiSelectOptions<Options extends Option<Value>[], Value extends Primitive> { 272 + export interface MultiSelectOptions<Options extends Option<Value>[], Value> { 276 273 message: string; 277 274 options: Options; 278 - initialValues?: Options[number]['value'][]; 275 + initialValues?: Value[]; 279 276 required?: boolean; 280 - cursorAt?: Options[number]['value']; 277 + cursorAt?: Value; 281 278 } 282 - export const multiselect = <Options extends Option<Value>[], Value extends Primitive>( 279 + export const multiselect = <Options extends Option<Value>[], Value>( 283 280 opts: MultiSelectOptions<Options, Value> 284 281 ) => { 285 282 const opt = ( 286 - option: Options[number], 283 + option: Option<Value>, 287 284 state: 'inactive' | 'active' | 'selected' | 'active-selected' | 'submitted' | 'cancelled' 288 285 ) => { 289 286 const label = option.label ?? String(option.value); ··· 387 384 } 388 385 } 389 386 }, 390 - }).prompt() as Promise<Options[number]['value'][] | symbol>; 387 + }).prompt() as Promise<Value[] | symbol>; 391 388 }; 392 389 393 - export interface GroupMultiSelectOptions<Options extends Option<Value>[], Value extends Primitive> { 390 + export interface GroupMultiSelectOptions<Options extends Option<Value>[], Value> { 394 391 message: string; 395 392 options: Record<string, Options>; 396 - initialValues?: Options[number]['value'][]; 393 + initialValues?: Value[]; 397 394 required?: boolean; 398 - cursorAt?: Options[number]['value']; 395 + cursorAt?: Value; 399 396 } 400 - export const groupMultiselect = <Options extends Option<Value>[], Value extends Primitive>( 397 + export const groupMultiselect = <Options extends Option<Value>[], Value>( 401 398 opts: GroupMultiSelectOptions<Options, Value> 402 399 ) => { 403 400 const opt = ( 404 - option: Options[number], 401 + option: Option<Value>, 405 402 state: 406 403 | 'inactive' 407 404 | 'active' ··· 411 408 | 'group-active-selected' 412 409 | 'submitted' 413 410 | 'cancelled', 414 - options: Options[number][] = [] as any 411 + options: Option<Value>[] = [] 415 412 ) => { 416 413 const label = option.label ?? String(option.value); 417 414 const isItem = typeof (option as any).group === 'string'; ··· 531 528 } 532 529 } 533 530 }, 534 - }).prompt() as Promise<Options[number]['value'][] | symbol>; 531 + }).prompt() as Promise<Value[] | symbol>; 535 532 }; 536 533 537 534 const strip = (str: string) => str.replace(ansiRegex(), '');