From 89dc90d311007a4a7f9cc7e03042b571ac6181aa Mon Sep 17 00:00:00 2001 From: sakulstra Date: Wed, 1 Nov 2023 15:15:45 +0100 Subject: [PATCH 1/2] fix: allow default on select --- packages/select/README.md | 1 + packages/select/select.test.mts | 20 ++++++++++++++++++++ packages/select/src/index.mts | 15 +++++++++++++-- 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/packages/select/README.md b/packages/select/README.md index 66ba63f28..2543c0702 100644 --- a/packages/select/README.md +++ b/packages/select/README.md @@ -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 | Selects the default value that is initially. If omitted, the first selectable item is selected. | | 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. | diff --git a/packages/select/select.test.mts b/packages/select/select.test.mts index 5bb0cfd89..a7174cfd7 100644 --- a/packages/select/select.test.mts +++ b/packages/select/select.test.mts @@ -452,4 +452,24 @@ 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)" + `); + }); }); diff --git a/packages/select/src/index.mts b/packages/select/src/index.mts index 5c52ffd35..75a1cd8a1 100644 --- a/packages/select/src/index.mts +++ b/packages/select/src/index.mts @@ -30,6 +30,7 @@ type SelectConfig = PromptConfig<{ choices: ReadonlyArray | Separator>; pageSize?: number; loop?: boolean; + default?: Value; }>; type Item = Separator | Choice; @@ -60,7 +61,7 @@ export default createPrompt( config: SelectConfig, done: (value: Value) => void, ): string => { - const { choices: items, loop = true, pageSize } = config; + const { choices: items, loop = true, pageSize, default: _default } = config; const firstRender = useRef(true); const prefix = usePrefix(); const [status, setStatus] = useState('pending'); @@ -76,7 +77,17 @@ export default createPrompt( return { first, last }; }, [items]); - const [active, setActive] = useState(bounds.first); + const defaultItemIndex = useMemo(() => { + if (!_default) return -1; + return items.findIndex( + (item) => + !Separator.isSeparator(item) && isSelectable(item) && item.value === _default, + ); + }, [_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; From f0585fc09a823c742d7b5053b5f6e49c377bc3db Mon Sep 17 00:00:00 2001 From: sakulstra Date: Wed, 1 Nov 2023 15:24:13 +0100 Subject: [PATCH 2/2] fix: improve test --- packages/select/select.test.mts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/select/select.test.mts b/packages/select/select.test.mts index a7174cfd7..fe4631588 100644 --- a/packages/select/select.test.mts +++ b/packages/select/select.test.mts @@ -471,5 +471,8 @@ describe('select prompt', () => { 7 (Use arrow keys to reveal more choices)" `); + + events.keypress('enter'); + await expect(answer).resolves.toEqual(4); }); });