Skip to content

Commit

Permalink
feat: ✨ add confirm button
Browse files Browse the repository at this point in the history
close: #155
  • Loading branch information
yi-Xu-0100 committed Aug 29, 2021
1 parent 0bc3cee commit fcbe20d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/lib/prompts/prompt-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/
import * as vscode from 'vscode';
import * as configuration from '../configuration';
import createSimpleQuickPick from './quick-pick';
import createSimpleQuickPick, { confirmButton } from './quick-pick';
import localize from '../localize';
import * as output from '../output';

Expand Down Expand Up @@ -93,9 +93,7 @@ function createInputBox({
if (value) {
input.value = value;
}
if (buttons) {
input.buttons = buttons;
}
input.buttons = [...(buttons ?? []), confirmButton];
input.onDidChangeValue(function () {
try {
input.validationMessage = validate(input.value);
Expand All @@ -118,6 +116,20 @@ function createInputBox({
}
});
input.onDidTriggerButton(function (e) {
if (e === confirmButton) {
try {
input.validationMessage = validate(input.value);
if (input.validationMessage) {
return;
}
resolve({ value: input.value, activeItems: [] });
input.dispose();
} catch (e) {
output.error(`step.${input.step}`, e);
reject(e);
}
}

if (e === vscode.QuickInputButtons.Back) {
reject({
button: e,
Expand Down
21 changes: 21 additions & 0 deletions src/lib/prompts/quick-pick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
*/
import * as vscode from 'vscode';

export const confirmButton: vscode.QuickInputButton = {
iconPath: new vscode.ThemeIcon('arrow-right'),
tooltip: 'confirm',
};

export default function createQuickPick<T extends vscode.QuickPickItem>({
placeholder,
items = [],
Expand All @@ -29,6 +34,7 @@ export default function createQuickPick<T extends vscode.QuickPickItem>({
picker.totalSteps = totalSteps;
picker.buttons = buttons;
picker.show();
picker.buttons = [...buttons, confirmButton];
picker.onDidAccept(function () {
if (picker.activeItems.length) {
resolve({
Expand All @@ -39,6 +45,21 @@ export default function createQuickPick<T extends vscode.QuickPickItem>({
}
});
picker.onDidTriggerButton(function (e) {
if (e === confirmButton) {
if (picker.activeItems.length) {
resolve({
value: picker.value,
activeItems: picker.activeItems as T[],
});
} else {
resolve({
value: picker.value,
activeItems: [picker.items[0]] as T[],
});
}
picker.dispose();
}

if (e === vscode.QuickInputButtons.Back) {
reject({
button: e,
Expand Down

0 comments on commit fcbe20d

Please sign in to comment.