mirror of
https://github.com/charmbracelet/gum.git
synced 2024-09-11 06:16:03 +03:00
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:
parent
5c98432070
commit
fa533691c4
@ -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,
|
||||
|
Loading…
Reference in New Issue
Block a user