Skip to content

Commit

Permalink
Feat/checkbox disabled (#1205)
Browse files Browse the repository at this point in the history
* feat(checkbox): customizable text for disabled text

* docs(checkbox): update usage description
  • Loading branch information
zonemeen committed Mar 30, 2023
1 parent 500ff32 commit 8891098
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
7 changes: 6 additions & 1 deletion packages/checkbox/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ const answer = await checkbox({
{ name: 'npm', value: 'npm' },
{ name: 'yarn', value: 'yarn' },
{ name: 'pnpm', value: 'pnpm', disabled: true },
{
name: 'pnpm',
value: 'pnpm',
disabled: '(pnpm is not available)',
},
],
});
```
Expand All @@ -32,7 +37,7 @@ const answer = await checkbox({
| Property | Type | Required | Description |
| -------- | --------- | -------- | ------------------------------ |
| message | `string` | yes | The question to ask |
| choices | `Array<{ value: string, name?: string, disabled?: boolean }>` | yes | List of the available choices. The `value` will be returned as the answer, and used as display if no `name` is defined. Choices who're `disabled` will be displayed, but not selectable. |
| choices | `Array<{ value: string, name?: string, disabled?: boolean \| string }>` | yes | List of the available choices. The `value` will be returned as the answer, and used as display if no `name` is defined. Choices who're `disabled` will be displayed, but not selectable. |

# License

Expand Down
5 changes: 5 additions & 0 deletions packages/checkbox/demo.mts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import checkbox from './src/index.mjs';
{ name: 'npm', value: 'npm' },
{ name: 'yarn', value: 'yarn' },
{ name: 'jspm', value: 'jspm', disabled: true },
{
name: 'pnpm',
value: 'pnpm',
disabled: '(pnpm is not available)',
},
],
});
console.log('Answer:', answer);
Expand Down
6 changes: 4 additions & 2 deletions packages/checkbox/src/index.mts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import ansiEscapes from 'ansi-escapes';
export type Choice<Value> = {
name?: string;
value: Value;
disabled?: boolean;
disabled?: boolean | string;
};

type Config<Value> = {
Expand Down Expand Up @@ -131,7 +131,9 @@ export default createPrompt(
.map(({ name, value, checked, disabled }, index) => {
const line = name || value;
if (disabled) {
return chalk.dim(` - ${line} (disabled)`);
return chalk.dim(
`- ${line} ${typeof disabled === 'string' ? disabled : '(disabled)'}`
);
}

const checkbox = checked ? chalk.green(figures.circleFilled) : figures.circle;
Expand Down

0 comments on commit 8891098

Please sign in to comment.