1
0
mirror of https://github.com/rivo/tview.git synced 2025-04-24 13:48:56 +08:00

Merge c51e9273fdd62104c3f1baa976ea0c8b3448f55d into c76f7879f592d17e9e68a1d795a85faae6cb7414

This commit is contained in:
Navid Yaghoobi 2024-11-16 11:16:11 -05:00 committed by GitHub
commit f0d80d67c8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -71,6 +71,10 @@ type DropDown struct {
// possible.
fieldWidth int
// Set true when automatic seletion of an item in the drop-down list
// based on the prefix is enabled (default is true).
autoSelection bool
// An optional function which is called when the user indicated that they
// are done selecting options. The key which was pressed is provided (tab,
// shift-tab, or escape).
@ -104,6 +108,7 @@ func NewDropDown() *DropDown {
fieldBackgroundColor: Styles.ContrastBackgroundColor,
fieldTextColor: Styles.PrimaryTextColor,
prefixTextColor: Styles.ContrastSecondaryTextColor,
autoSelection: true,
}
return d
@ -459,7 +464,7 @@ func (d *DropDown) InputHandler() func(event *tcell.EventKey, setFocus func(p Pr
d.prefix = ""
// If the first key was a letter already, it becomes part of the prefix.
if r := event.Rune(); key == tcell.KeyRune && r != ' ' {
if r := event.Rune(); key == tcell.KeyRune && r != ' ' && d.autoSelection {
d.prefix += string(r)
d.evalPrefix()
}
@ -515,7 +520,12 @@ func (d *DropDown) openList(setFocus func(Primitive)) {
currentOption.Selected()
}
}).SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
if event.Key() == tcell.KeyRune {
if event.Key() == tcell.KeyEscape {
d.currentOption = optionBefore
d.closeList(setFocus)
} else if !d.autoSelection {
d.prefix = ""
} else if event.Key() == tcell.KeyRune {
d.prefix += string(event.Rune())
d.evalPrefix()
} else if event.Key() == tcell.KeyBackspace || event.Key() == tcell.KeyBackspace2 {
@ -524,11 +534,6 @@ func (d *DropDown) openList(setFocus func(Primitive)) {
d.prefix = string(r[:len(r)-1])
}
d.evalPrefix()
} else if event.Key() == tcell.KeyEscape {
d.currentOption = optionBefore
d.closeList(setFocus)
} else {
d.prefix = ""
}
return event
@ -623,3 +628,9 @@ func (d *DropDown) MouseHandler() func(action MouseAction, event *tcell.EventMou
return
})
}
// SetAutomaticSelection enable/disable the automatic seletion
// of an item in the drop-down list based on the prefix.
func (d *DropDown) SetAutomaticSelection(status bool) {
d.autoSelection = status
}