mirror of
https://github.com/charmbracelet/gum
synced 2024-05-23 16:42:22 +02: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.
|
// Keep track of the selected items.
|
||||||
currentSelected := 0
|
currentSelected := 0
|
||||||
// Check if selected items should be used.
|
// 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))
|
var items = make([]item, len(o.Options))
|
||||||
|
|
||||||
for i, option := range o.Options {
|
for i, option := range o.Options {
|
||||||
// Check if the option should be selected.
|
// Check if the option should be selected.
|
||||||
isSelected := hasSelectedItems && currentSelected < o.Limit && arrayContains(o.Selected, option)
|
isSelected := hasSelectedItems && currentSelected < o.Limit && arrayContains(o.Selected, option)
|
||||||
// If the option is selected then increment the current selected count.
|
// If the option is selected then increment the current selected count.
|
||||||
if isSelected {
|
if isSelected {
|
||||||
currentSelected++
|
currentSelected++
|
||||||
|
if o.Limit == 1 {
|
||||||
|
startingIndex = i
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
items[i] = item{text: option, selected: isSelected}
|
items[i] = item{text: option, selected: isSelected}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -78,6 +85,7 @@ func (o Options) Run() error {
|
||||||
pager.UsePgUpPgDownKeys = false
|
pager.UsePgUpPgDownKeys = false
|
||||||
|
|
||||||
tm, err := tea.NewProgram(model{
|
tm, err := tea.NewProgram(model{
|
||||||
|
index: startingIndex,
|
||||||
height: o.Height,
|
height: o.Height,
|
||||||
cursor: o.Cursor,
|
cursor: o.Cursor,
|
||||||
selectedPrefix: o.SelectedPrefix,
|
selectedPrefix: o.SelectedPrefix,
|
||||||
|
|
Loading…
Reference in a new issue