Skip to content

Commit

Permalink
fix: allow default on select
Browse files Browse the repository at this point in the history
  • Loading branch information
sakulstra committed Nov 1, 2023
1 parent 1c4c80a commit 89dc90d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
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 | 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. |

Expand Down
20 changes: 20 additions & 0 deletions packages/select/select.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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, {

Check failure on line 457 in packages/select/select.test.mts

View workflow job for this annotation

GitHub Actions / Linting

'answer' is assigned a value but never used

Check failure on line 457 in packages/select/select.test.mts

View workflow job for this annotation

GitHub Actions / Linting

'events' is assigned a value but never used
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)"
`);
});
});
15 changes: 13 additions & 2 deletions packages/select/src/index.mts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type SelectConfig<Value> = PromptConfig<{
choices: ReadonlyArray<Choice<Value> | Separator>;
pageSize?: number;
loop?: boolean;
default?: Value;
}>;

type Item<Value> = Separator | Choice<Value>;
Expand Down Expand Up @@ -60,7 +61,7 @@ export default createPrompt(
config: SelectConfig<Value>,
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');
Expand All @@ -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<Value>;
Expand Down

0 comments on commit 89dc90d

Please # to comment.