Skip to content

Instantly share code, notes, and snippets.

@ecool
Last active March 25, 2021 20:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ecool/f09f90c3da8f95d4f17ee95f3f6cb192 to your computer and use it in GitHub Desktop.
Save ecool/f09f90c3da8f95d4f17ee95f3f6cb192 to your computer and use it in GitHub Desktop.
QuickPick Multi-Select Custom Input
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();
});
}
@bfitzpat
Copy link

One further modification - quickPick.items = [{ label: quickPick.value }, ...options]; should change quickPick.value to quickPick.value.trim() or you end up with some entertaining things in the list!

@ecool
Copy link
Author

ecool commented Mar 25, 2021

Thanks, @bfitzpat. I'll update it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment