[GH-ISSUE #204] Removing/adding form item dynamically #157

Closed
opened 2026-03-04 01:02:29 +03:00 by kerem · 1 comment
Owner

Originally created by @klashagel on GitHub (Dec 10, 2018).
Original GitHub issue: https://github.com/rivo/tview/issues/204

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)

		})

	}

}
Originally created by @klashagel on GitHub (Dec 10, 2018). Original GitHub issue: https://github.com/rivo/tview/issues/204 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) }) } } ```
kerem closed this issue 2026-03-04 01:02:29 +03:00
Author
Owner

@rivo commented on GitHub (Dec 15, 2018):

You closed this yourself. Let me know if any more clarification is needed.

<!-- gh-comment-id:447558866 --> @rivo commented on GitHub (Dec 15, 2018): You closed this yourself. Let me know if any more clarification is needed.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/tview#157
No description provided.