-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
📢 Thoughts on this report? Let us know!. |
if (!_default) return -1; | ||
return items.findIndex( | ||
(item) => | ||
!Separator.isSeparator(item) && isSelectable(item) && item.value === _default, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
!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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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. | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 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. | |
Thanks for sending the PR! That's a great addition 😄 I merged your code inside #1329 |
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.