feat(choose): Preselect the Selected Option When Limit is One (#166)

* Add startingIndex variable to choose model

* refactor: simplify code branches

Co-authored-by: Matthew Farstad <matthewwilliamfarstad@gmail.com>
Co-authored-by: Maas Lalani <maas@lalani.dev>
This commit is contained in:
Matt Farstad 2022-10-02 12:51:35 -04:00 committed by GitHub
parent 5c98432070
commit fa533691c4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -49,16 +49,23 @@ func (o Options) Run() error {
// Keep track of the selected items.
currentSelected := 0
// Check if selected items should be used.
hasSelectedItems := o.Limit > 1 && len(o.Selected) > 0
hasSelectedItems := len(o.Selected) > 0
startingIndex := 0
var items = make([]item, len(o.Options))
for i, option := range o.Options {
// Check if the option should be selected.
isSelected := hasSelectedItems && currentSelected < o.Limit && arrayContains(o.Selected, option)
// If the option is selected then increment the current selected count.
if isSelected {
currentSelected++
if o.Limit == 1 {
startingIndex = i
}
}
items[i] = item{text: option, selected: isSelected}
}
@ -78,6 +85,7 @@ func (o Options) Run() error {
pager.UsePgUpPgDownKeys = false
tm, err := tea.NewProgram(model{
index: startingIndex,
height: o.Height,
cursor: o.Cursor,
selectedPrefix: o.SelectedPrefix,