tview
tview copied to clipboard
Removing/adding form item dynamically
Hi
Thanks for a great package , trying to add and remove a form item in a Flexbox which seems to work fine when I call the function from another primitive i.e list. When I try to remove it , calling RemoveItem from form fails so I tried to use channels and code runs without crashing but nothing happens. Hoping you could point me in the right direction
Test code
package main
import (
"github.com/gdamore/tcell"
"github.com/rivo/tview"
)
var modal *tview.Modal
var list *tview.List
var rightcol *tview.Flex
var addtit *tview.Form
var c chan int
func Klas(nextSlide func()) (title string, content tview.Primitive) {
c := make(chan int)
go frmman(c)
showModal := func() {
if err := app.SetRoot(modal, false).Run(); err != nil {
panic(err)
}
}
modal = tview.NewModal().
SetText("Do you want to quit the application?").
AddButtons([]string{"Quit", "Cancel"}).
SetDoneFunc(func(buttonIndex int, buttonLabel string) {
if buttonLabel == "Quit" {
app.Stop()
}
})
addtit := tview.NewForm().
AddInputField("First name:", "", 20, nil, nil).
AddInputField("Last name:", "", 20, nil, nil).
AddButton("Test", func() {
addtit.RemoveFormItem(1)
}).
AddButton("Cancel", func() {
//app.SetFocus(list)
c <- 1
})
//addtit.SetBorder(true).SetTitle("Employee Information")
rightcol = tview.NewFlex().SetDirection(tview.FlexRow).
AddItem(tview.NewBox().SetBorder(true).SetTitle("Top"), 0, 1, false).
AddItem(tview.NewBox().SetBorder(true).SetTitle("Middle (3 x height of Top)"), 0, 1, false)
showadd := func() {
rightcol.AddItem(addtit, 0, 1, true)
app.SetFocus(addtit)
}
list = tview.NewList().
AddItem("Modal", "Some explanatory text", 'a', showModal).
AddItem("Add...", "Some explanatory text", 'b', showadd).
AddItem("Remove", "Some explanatory text", 'c', func() {
rightcol.RemoveItem(addtit)
}).
AddItem("Redraw", "Some explanatory text", 'd', func() {
app.ForceDraw()
}).
AddItem("Quit", "Press to exit", 'q', func() {
app.Stop()
})
list.SetBorder(true).SetTitle("Middle (3 x height of Top)")
list.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
if event.Key() == tcell.KeyTAB {
app.SetFocus(addtit)
}
return event
})
flex := tview.NewFlex().
AddItem(list, 0, 1, true).
//AddItem(tview.NewBox().SetBorder(true).SetTitle("Right (20 cols)"), 0, 3, false)
AddItem(rightcol, 0, 2, false)
//AddItem(tview.NewBox().SetBorder(true).SetTitle("Right (20 cols)"), 20, 1, false) */
return "Klas", flex
}
func frmman(c chan int) {
for {
y := <-c
_ = y
app.QueueUpdateDraw(func() {
app.SetFocus(list)
rightcol.RemoveItem(addtit)
})
}
}
You closed this yourself. Let me know if any more clarification is needed.