Skip to content
New issue

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

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

Already on GitHub? # to your account

fix: allow default on select #1327

Closed
wants to merge 2 commits into from
Closed
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
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. |
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
| default | `string` | no | Selects the default value that is initially. If omitted, the first selectable item is selected. |
| 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);
});
});
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 @@
choices: ReadonlyArray<Choice<Value> | Separator>;
pageSize?: number;
loop?: boolean;
default?: Value;
}>;

type Item<Value> = Separator | Choice<Value>;
Expand Down Expand Up @@ -60,14 +61,14 @@
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');

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...'
const last = items.length - 1 - [...items].reverse().findIndex(isSelectable);
if (first < 0)
throw new Error(
Expand All @@ -76,7 +77,17 @@
return { first, last };
}, [items]);

const [active, setActive] = useState(bounds.first);
const defaultItemIndex = useMemo(() => {
if (!_default) return -1;
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
if (!_default) return -1;
if (!'default' in config) return -1;

Thinking we might want to be more liberal with the value we allow to provide 🤔 (like allowing falsy values, null or undefined)

return items.findIndex(
(item) =>
!Separator.isSeparator(item) && isSelectable(item) && item.value === _default,
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
!Separator.isSeparator(item) && isSelectable(item) && item.value === _default,
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
Loading