Added SetFocus() to Form which changes the focus to a specific form element. Resolves #286

This commit is contained in:
Oliver 2019-06-02 21:31:59 +02:00
parent 29b0d3cfbf
commit 384f577a62
1 changed files with 14 additions and 0 deletions

14
form.go
View File

@ -154,6 +154,20 @@ func (f *Form) SetButtonTextColor(color tcell.Color) *Form {
return f
}
// SetFocus shifts the focus to the form element with the given index, counting
// non-button items first and buttons last. Note that this index is only used
// when the form itself receives focus.
func (f *Form) SetFocus(index int) *Form {
if index < 0 {
f.focusedElement = 0
} else if index >= len(f.items)+len(f.buttons) {
f.focusedElement = len(f.items) + len(f.buttons)
} else {
f.focusedElement = index
}
return f
}
// AddInputField adds an input field to the form. It has a label, an optional
// initial value, a field width (a value of 0 extends it as far as possible),
// an optional accept function to validate the item's value (set to nil to