( name: string, items: CheckboxDefItem<T>[], options?: CheckboxDefOptions<T>, )
| 212 | export type CheckboxDefItem<T> = T extends string ? string | ComplexCheckboxDefItem<T> : ComplexCheckboxDefItem<T> |
| 213 | |
| 214 | export function checkboxDef<T>( |
| 215 | name: string, |
| 216 | items: CheckboxDefItem<T>[], |
| 217 | options?: CheckboxDefOptions<T>, |
| 218 | ): InputDefinition<T[]> { |
| 219 | const editValues = async (values: T[]): Promise<T[] | CancelAction> => { |
| 220 | // We can't add help to the inquirer `checkbox` so, at least for now, we'll display |
| 221 | // the help before we display the checkbox. |
| 222 | if (options?.helpText) { |
| 223 | console.log(`\n${options.helpText}\n`) |
| 224 | } |
| 225 | const choices: CheckboxChoice<T>[] = items.map(item => { |
| 226 | const value = typeof item === 'string' ? item as T : item.value |
| 227 | const retVal: CheckboxChoice<T> = typeof item === 'object' |
| 228 | ? { ...item } |
| 229 | : ({ name: item, value }) |
| 230 | if (values.includes(value)) { |
| 231 | retVal.checked = true |
| 232 | } |
| 233 | return retVal |
| 234 | }) |
| 235 | const question = { |
| 236 | message: `Select ${name}.`, |
| 237 | choices, |
| 238 | default: finishAction, |
| 239 | pageSize: inquirerPageSize, |
| 240 | validate: options?.validate, |
| 241 | } |
| 242 | const updatedValues = await checkbox(question) |
| 243 | |
| 244 | return updatedValues as T[] |
| 245 | } |
| 246 | |
| 247 | const buildFromUserInput = (): Promise<T[] | CancelAction> => editValues(options?.default ?? []) |
| 248 | |
| 249 | const summarizeForEdit = options?.summarizeForEdit |
| 250 | ?? ((value: T[]) => clipToMaximum(value.map(item => stringFromUnknown(item)).join(', '), maxItemValueLength)) |
| 251 | |
| 252 | const updateFromUserInput = (original: T[]): Promise<T[] | CancelAction> => editValues(original) |
| 253 | |
| 254 | return { name, buildFromUserInput, summarizeForEdit, updateFromUserInput } |
| 255 | } |
no test coverage detected