Last active
March 25, 2021 20:13
-
-
Save ecool/f09f90c3da8f95d4f17ee95f3f6cb192 to your computer and use it in GitHub Desktop.
QuickPick Multi-Select Custom Input
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export async function quickPickTag( | |
tags: string[], | |
canSelectMany: boolean = false, | |
acceptInput: boolean = true): Promise<string[]> { | |
let options = tags.map(tag => ({ label: tag })); | |
return new Promise((resolve, _) => { | |
let quickPick = vscode.window.createQuickPick(); | |
let placeholder = "Select a tag."; | |
if (acceptInput) { | |
placeholder = "Select/Insert a tag."; | |
} | |
quickPick.placeholder = placeholder; | |
quickPick.canSelectMany = canSelectMany; | |
quickPick.items = options; | |
let selectedItems = []; | |
if (canSelectMany) { | |
quickPick.onDidChangeSelection((selected) => { | |
selectedItems = selected; | |
}); | |
} | |
quickPick.onDidAccept(_ => { | |
if (quickPick.value.trim().length > 0 || selectedItems.length > 0 || quickPick.activeItems.length > 0) { | |
if (canSelectMany) { | |
resolve(selectedItems.map((item) => item.label)); | |
} else { | |
resolve(quickPick.activeItems.map((item) => item.label)); | |
} | |
quickPick.hide(); | |
} | |
}); | |
if (acceptInput) { | |
quickPick.onDidChangeValue(_ => { | |
if (quickPick.value.trim().length === 0) { | |
quickPick.value = ''; | |
quickPick.items = options; | |
} else { | |
// include currently typed option | |
if (tags.indexOf(quickPick.value.trim()) === -1) { | |
quickPick.items = [{ label: quickPick.value.trim() }, ...options]; | |
} | |
} | |
}); | |
} | |
quickPick.onDidHide(_ => quickPick.dispose()); | |
quickPick.show(); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
One further modification -
quickPick.items = [{ label: quickPick.value }, ...options];
should changequickPick.value
toquickPick.value.trim()
or you end up with some entertaining things in the list!