Added a listener for changes in input fields. Also in the Form. Fixes #4.
parent
2874294d89
commit
3fbea7a99b
|
@ -64,5 +64,6 @@ Add your issue here on GitHub. Feel free to get in touch if you have any questio
|
|||
|
||||
- v0.2 (2018-01-10)
|
||||
- Added `Styles` variable with default colors for primitives.
|
||||
- Completed some missing InputField functions.
|
||||
- v0.1 (2018-01-06)
|
||||
- First Release
|
||||
|
|
|
@ -52,6 +52,11 @@ func (c *Checkbox) SetChecked(checked bool) *Checkbox {
|
|||
return c
|
||||
}
|
||||
|
||||
// IsChecked returns whether or not the box is checked.
|
||||
func (c *Checkbox) IsChecked() bool {
|
||||
return c.checked
|
||||
}
|
||||
|
||||
// SetLabel sets the text to be displayed before the input area.
|
||||
func (c *Checkbox) SetLabel(label string) *Checkbox {
|
||||
c.label = label
|
||||
|
|
|
@ -9,8 +9,8 @@ func main() {
|
|||
app := tview.NewApplication()
|
||||
form := tview.NewForm().
|
||||
AddDropDown("Title", []string{"Mr.", "Ms.", "Mrs.", "Dr.", "Prof."}, 0, nil).
|
||||
AddInputField("First name", "", 20, nil).
|
||||
AddInputField("Last name", "", 20, nil).
|
||||
AddInputField("First name", "", 20, nil, nil).
|
||||
AddInputField("Last name", "", 20, nil, nil).
|
||||
AddCheckbox("Age 18+", false, nil).
|
||||
AddButton("Save", nil).
|
||||
AddButton("Quit", func() {
|
||||
|
|
|
@ -12,8 +12,8 @@ const form = `[green]package[white] main
|
|||
|
||||
[green]func[white] [yellow]main[white]() {
|
||||
form := tview.[yellow]NewForm[white]().
|
||||
[yellow]AddInputField[white]([red]"First name:"[white], [red]""[white], [red]20[white], nil).
|
||||
[yellow]AddInputField[white]([red]"Last name:"[white], [red]""[white], [red]20[white], nil).
|
||||
[yellow]AddInputField[white]([red]"First name:"[white], [red]""[white], [red]20[white], nil, nil).
|
||||
[yellow]AddInputField[white]([red]"Last name:"[white], [red]""[white], [red]20[white], nil, nil).
|
||||
[yellow]AddDropDown[white]([red]"Role:"[white], [][green]string[white]{
|
||||
[red]"Engineer"[white],
|
||||
[red]"Manager"[white],
|
||||
|
@ -30,8 +30,8 @@ const form = `[green]package[white] main
|
|||
// Form demonstrates forms.
|
||||
func Form(nextSlide func()) (title string, content tview.Primitive) {
|
||||
f := tview.NewForm().
|
||||
AddInputField("First name:", "", 20, nil).
|
||||
AddInputField("Last name:", "", 20, nil).
|
||||
AddInputField("First name:", "", 20, nil, nil).
|
||||
AddInputField("Last name:", "", 20, nil, nil).
|
||||
AddDropDown("Role:", []string{"Engineer", "Manager", "Administration"}, 0, nil).
|
||||
AddCheckbox("On vacation:", false, nil).
|
||||
AddButton("Save", nextSlide).
|
||||
|
|
10
dropdown.go
10
dropdown.go
|
@ -81,6 +81,16 @@ func (d *DropDown) SetCurrentOption(index int) *DropDown {
|
|||
return d
|
||||
}
|
||||
|
||||
// GetCurrentOption returns the index of the currently selected option as well
|
||||
// as its text. If no option was selected, -1 and an empty string is returned.
|
||||
func (d *DropDown) GetCurrentOption() (int, string) {
|
||||
var text string
|
||||
if d.currentOption >= 0 && d.currentOption < len(d.options) {
|
||||
text = d.options[d.currentOption].Text
|
||||
}
|
||||
return d.currentOption, text
|
||||
}
|
||||
|
||||
// SetLabel sets the text to be displayed before the input area.
|
||||
func (d *DropDown) SetLabel(label string) *DropDown {
|
||||
d.label = label
|
||||
|
|
14
form.go
14
form.go
|
@ -129,9 +129,10 @@ func (f *Form) SetButtonTextColor(color tcell.Color) *Form {
|
|||
|
||||
// AddInputField adds an input field to the form. It has a label, an optional
|
||||
// initial value, a field length (a value of 0 extends it as far as possible),
|
||||
// and an optional accept function to validate the item's value (set to nil to
|
||||
// accept any text).
|
||||
func (f *Form) AddInputField(label, value string, fieldLength int, accept func(textToCheck string, lastChar rune) bool) *Form {
|
||||
// an optional accept function to validate the item's value (set to nil to
|
||||
// accept any text), and an (optional) callback function which is invoked when
|
||||
// the input field's text has changed.
|
||||
func (f *Form) AddInputField(label, value string, fieldLength int, accept func(textToCheck string, lastChar rune) bool, changed func(text string)) *Form {
|
||||
f.items = append(f.items, NewInputField().
|
||||
SetLabel(label).
|
||||
SetText(value).
|
||||
|
@ -169,6 +170,13 @@ func (f *Form) AddButton(label string, selected func()) *Form {
|
|||
return f
|
||||
}
|
||||
|
||||
// GetElement returns the form element at the given position, starting with
|
||||
// index 0. Elements are referenced in the order they were added. Buttons are
|
||||
// not included.
|
||||
func (f *Form) GetElement(index int) Primitive {
|
||||
return f.items[index]
|
||||
}
|
||||
|
||||
// SetCancelFunc sets a handler which is called when the user hits the Escape
|
||||
// key.
|
||||
func (f *Form) SetCancelFunc(callback func()) *Form {
|
||||
|
|
|
@ -36,6 +36,9 @@ type InputField struct {
|
|||
// An optional function which may reject the last character that was entered.
|
||||
accept func(text string, ch rune) bool
|
||||
|
||||
// An optional function which is called when the input has changed.
|
||||
changed func(text string)
|
||||
|
||||
// An optional function which is called when the user indicated that they
|
||||
// are done entering text. The key which was pressed is provided (tab,
|
||||
// shift-tab, enter, or escape).
|
||||
|
@ -55,6 +58,9 @@ func NewInputField() *InputField {
|
|||
// SetText sets the current text of the input field.
|
||||
func (i *InputField) SetText(text string) *InputField {
|
||||
i.text = text
|
||||
if i.changed != nil {
|
||||
i.changed(text)
|
||||
}
|
||||
return i
|
||||
}
|
||||
|
||||
|
@ -119,6 +125,13 @@ func (i *InputField) SetAcceptanceFunc(handler func(textToCheck string, lastChar
|
|||
return i
|
||||
}
|
||||
|
||||
// SetChangedFunc sets a handler which is called whenever the text of the input
|
||||
// field has changed. It receives the current text (after the change).
|
||||
func (i *InputField) SetChangedFunc(handler func(text string)) *InputField {
|
||||
i.changed = handler
|
||||
return i
|
||||
}
|
||||
|
||||
// SetDoneFunc sets a handler which is called when the user is done entering
|
||||
// text. The callback function is provided with the key that was pressed, which
|
||||
// is one of the following:
|
||||
|
@ -202,6 +215,14 @@ func (i *InputField) setCursor(screen tcell.Screen) {
|
|||
// InputHandler returns the handler for this primitive.
|
||||
func (i *InputField) InputHandler() func(event *tcell.EventKey, setFocus func(p Primitive)) {
|
||||
return func(event *tcell.EventKey, setFocus func(p Primitive)) {
|
||||
// Trigger changed events.
|
||||
currentText := i.text
|
||||
defer func() {
|
||||
if i.text != currentText && i.changed != nil {
|
||||
i.changed(i.text)
|
||||
}
|
||||
}()
|
||||
|
||||
// Process key event.
|
||||
switch key := event.Key(); key {
|
||||
case tcell.KeyRune: // Regular character.
|
||||
|
|
Loading…
Reference in New Issue