Skip to content
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

Feat (select): Allow default config in Select prompt #1329

Merged
merged 3 commits into from
Nov 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/select/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const answer = await select({
| -------- | ---------------------------------------------------------------------------------------------------------- | -------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| message | `string` | yes | The question to ask |
| choices | `Array<{ value: string, name?: string, description?: string, disabled?: boolean \| string } \| Separator>` | 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. The `description` will be displayed under the prompt when the cursor land over the choice. |
| default | `string` | no | Defines in front of which item the cursor will initially appear. When omitted, the cursor will appear on the first selectable item. |
| pageSize | `number` | no | By default, lists of choice longer than 7 will be paginated. Use this option to control how many choices will appear on the screen at once. |
| loop | `boolean` | no | Defaults to `true`. When set to `false`, the cursor will be constrained to the top and bottom of the choice list without looping. |

Expand Down
23 changes: 23 additions & 0 deletions packages/select/select.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -452,4 +452,27 @@ describe('select prompt', () => {

await expect(answer).resolves.toEqual('pineapple');
});

it('Allows setting a default value', async () => {
const { answer, events, getScreen } = await render(select, {
message: 'Select a number',
choices: numberedChoices,
default: numberedChoices[3].value,
});

expect(getScreen()).toMatchInlineSnapshot(`
"? Select a number (Use arrow keys)
1
2
3
❯ 4
5
6
7
(Use arrow keys to reveal more choices)"
`);

events.keypress('enter');
await expect(answer).resolves.toEqual(4);
});
});
12 changes: 11 additions & 1 deletion packages/select/src/index.mts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
choices: ReadonlyArray<Choice<Value> | Separator>;
pageSize?: number;
loop?: boolean;
default?: Value;
}>;

type Item<Value> = Separator | Choice<Value>;
Expand Down Expand Up @@ -67,7 +68,7 @@

const bounds = useMemo(() => {
const first = items.findIndex(isSelectable);
// TODO: Replace with `findLastIndex` when it's available.

Check warning on line 71 in packages/select/src/index.mts

View workflow job for this annotation

GitHub Actions / Linting

Unexpected 'todo' comment: 'TODO: Replace with `findLastIndex` when...'

Check warning on line 71 in packages/select/src/index.mts

View workflow job for this annotation

GitHub Actions / Linting

Unexpected 'todo' comment: 'TODO: Replace with `findLastIndex` when...'
const last = items.length - 1 - [...items].reverse().findIndex(isSelectable);
if (first < 0)
throw new Error(
Expand All @@ -76,7 +77,16 @@
return { first, last };
}, [items]);

const [active, setActive] = useState(bounds.first);
const defaultItemIndex = useMemo(() => {
if (!('default' in config)) return -1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree that it's nicer, but perhaps we should just rename all default to defaultValue as it's a bit annoying to work with the reserved word imo.

return items.findIndex(
(item) => isSelectable(item) && item.value === config.default,
);
}, [config.default, items]);

const [active, setActive] = useState(
defaultItemIndex === -1 ? bounds.first : defaultItemIndex,
);

// Safe to assume the cursor position always point to a Choice.
const selectedChoice = items[active] as Choice<Value>;
Expand Down