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

fix: allow default on select #1327

Closed
wants to merge 2 commits into from

Conversation

sakulstra
Copy link
Contributor

Setting a default for selects is quite handy, especially when the list of options is huge.

One can currently "work around", by sorting the choices to put the "default" first, but imo this is annoying enough to better support it natively.

Copy link

codecov bot commented Nov 1, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Files Coverage Δ
packages/select/src/index.mts 100.00% <100.00%> (ø)

📢 Thoughts on this report? Let us know!.

if (!_default) return -1;
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,

@@ -76,7 +77,17 @@ export default createPrompt(
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)

@@ -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. |

@SBoudrias
Copy link
Owner

Thanks for sending the PR! That's a great addition 😄

I merged your code inside #1329

@SBoudrias SBoudrias closed this Nov 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants