MCPcopy Create free account
hub / github.com/SmartThingsCommunity/smartthings-cli / checkboxDef

Function checkboxDef

src/lib/item-input/array.ts:214–255  ·  view source on GitHub ↗
(
		name: string,
		items: CheckboxDefItem<T>[],
		options?: CheckboxDefOptions<T>,
)

Source from the content-addressed store, hash-verified

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

Callers 2

array.test.tsFile · 0.85

Calls 2

clipToMaximumFunction · 0.85
stringFromUnknownFunction · 0.85

Tested by

no test coverage detected