Skip to content

Allow to unset the defaultChoice for confirm-input #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/confirm-input.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ When disabled, user input is ignored.

### defaultChoice

Type: `'confirm' | 'cancel'`\
Type: `'confirm' | 'cancel' | 'unset'`\
Default: `'confirm'`

Default choice.
Expand Down
28 changes: 22 additions & 6 deletions source/components/confirm-input/confirm-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export type ConfirmInputProps = {
*
* @default "confirm"
*/
defaultChoice?: 'confirm' | 'cancel';
defaultChoice?: 'confirm' | 'cancel' | 'unset';

/**
* Confirm or cancel when user presses enter, depending on the `defaultChoice` value.
Expand Down Expand Up @@ -44,6 +44,12 @@ export function ConfirmInput({
onConfirm,
onCancel,
}: ConfirmInputProps) {
const choicePrompts = {
confirm: 'Y/n',
cancel: 'y/N',
unset: 'y/n',
};

useInput(
(input, key) => {
if (input.toLowerCase() === 'y') {
Expand All @@ -55,10 +61,20 @@ export function ConfirmInput({
}

if (key.return && submitOnEnter) {
if (defaultChoice === 'confirm') {
onConfirm();
} else {
onCancel();
switch (defaultChoice) {
case 'confirm': {
onConfirm();
break;
}

case 'cancel': {
onCancel();
break;
}

default: {
break;
}
}
}
},
Expand All @@ -69,7 +85,7 @@ export function ConfirmInput({

return (
<Text {...styles.input({isFocused: !isDisabled})}>
{defaultChoice === 'confirm' ? 'Y/n' : 'y/N'}
{choicePrompts[defaultChoice]}
</Text>
);
}
66 changes: 66 additions & 0 deletions test/confirm-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,69 @@ test('cancel with disabled enter', async t => {
t.false(confirmed);
t.true(cancelled);
});

test('unset the default choice, confirm via "y"', async t => {
let confirmed = false;
let cancelled = false;

const {lastFrame, stdin} = render(
<ConfirmInput
defaultChoice="unset"
onConfirm={() => {
confirmed = true;
}}
onCancel={() => {
cancelled = true;
}}
/>,
);

t.is(lastFrame(), 'y/n');

await delay(50);
stdin.write(enter);
await delay(50);

t.false(confirmed);
t.false(cancelled);

await delay(50);
stdin.write('y');
await delay(50);

t.true(confirmed);
t.false(cancelled);
});

test('unset the default choice, cancle via "n"', async t => {
let confirmed = false;
let cancelled = false;

const {lastFrame, stdin} = render(
<ConfirmInput
defaultChoice="unset"
onConfirm={() => {
confirmed = true;
}}
onCancel={() => {
cancelled = true;
}}
/>,
);

t.is(lastFrame(), 'y/n');

await delay(50);
stdin.write(enter);
await delay(50);

t.false(confirmed);
t.false(cancelled);

await delay(50);
stdin.write('n');
await delay(50);

t.false(confirmed);
t.true(cancelled);
});