[GH-ISSUE #1042] Form Input and Checkbox Colors Not Changing in tview Form #755

Open
opened 2026-03-04 01:07:30 +03:00 by kerem · 2 comments
Owner

Originally created by @0xJWLabs on GitHub (Oct 20, 2024).
Original GitHub issue: https://github.com/rivo/tview/issues/1042

Description

In my application using tview, I've created a login form with input fields for email, password, an optional code, and a checkbox. I am trying to set the background and text colors for these elements, but they don't seem to change, even though the code is correctly detecting the form items.

Extra Note: If I use lf.SetFieldBackgroundColor(...) and lf.SetFieldTextColor(...) it works, but it changes everything.

Code Snippet

Here's a snippet of the relevant part of the code:

func newLoginForm(done doneFn, cfg *config.Config) *loginForm {
	if done == nil {
		done = func(_ string, _ error) {}
	}

	lf := &loginForm{
		Form: tview.NewForm(),
		done: done,
	}

	lf.AddInputField("Email", "", 0, nil, nil)
	lf.AddPasswordField("Password", "", 0, 0, nil)
	lf.AddPasswordField("Code (optional)", "", 0, 0, nil)
	lf.AddCheckbox("Remember Me", false, nil)
	lf.AddButton("Login", lf.login)

	lf.SetButtonBackgroundColor(tcell.ColorGreen)

	lf.SetTitle("Login")
	lf.SetTitleColor(tcell.GetColor(cfg.Theme.TitleColor))
	lf.SetTitleAlign(tview.AlignLeft)

	p := cfg.Theme.BorderPadding
	lf.SetBorder(cfg.Theme.Border)
	lf.SetBorderColor(tcell.GetColor(cfg.Theme.BorderColor))
	lf.SetBorderPadding(p[0], p[1], p[2], p[3])

	lf.updateColor()

	return lf
}

func (lf *loginForm) updateColor() {
	emailInput := lf.GetFormItem(0).(*tview.InputField)
	passwordInput := lf.GetFormItem(1).(*tview.InputField)
	codeInput := lf.GetFormItem(2).(*tview.InputField)
	checkbox := lf.GetFormItem(3).(*tview.Checkbox)

	// Log checks for the form items
	backgroundColor := tcell.ColorBlack
	textColor := tcell.ColorWhite
	checkboxColor := tcell.ColorRed

	// Check if form items exist before updating
	if emailInput != nil {
		slog.Info("Email Input: exist")
		emailInput.SetFieldBackgroundColor(backgroundColor)
		emailInput.SetFieldTextColor(textColor)
	}
	if passwordInput != nil {
		slog.Info("Password: exist")
		passwordInput.SetFieldBackgroundColor(backgroundColor)
		passwordInput.SetFieldTextColor(textColor)
	}
	if codeInput != nil {
		slog.Info("Code Input: exist")
		codeInput.SetFieldBackgroundColor(backgroundColor)
		codeInput.SetFieldTextColor(textColor)
	}
	if checkbox != nil {
		slog.Info("Checkbox: exist")
		checkbox.SetFieldBackgroundColor(backgroundColor)
		checkbox.SetFieldTextColor(checkboxColor)
	}
}

Expected Behavior

  • Input fields for Email, Password, and Code should change to a black background with white text.
    Checkbox should change to a black background with red text.

Actual Behavior

  • The form items exist and are detected (verified via logs), but the colors remain unchanged.
Originally created by @0xJWLabs on GitHub (Oct 20, 2024). Original GitHub issue: https://github.com/rivo/tview/issues/1042 ## Description In my application using tview, I've created a login form with input fields for email, password, an optional code, and a checkbox. I am trying to set the background and text colors for these elements, but they don't seem to change, even though the code is correctly detecting the form items. > Extra Note: If I use `lf.SetFieldBackgroundColor(...)` and `lf.SetFieldTextColor(...)` it works, but it changes everything. ## Code Snippet Here's a snippet of the relevant part of the code: ```go func newLoginForm(done doneFn, cfg *config.Config) *loginForm { if done == nil { done = func(_ string, _ error) {} } lf := &loginForm{ Form: tview.NewForm(), done: done, } lf.AddInputField("Email", "", 0, nil, nil) lf.AddPasswordField("Password", "", 0, 0, nil) lf.AddPasswordField("Code (optional)", "", 0, 0, nil) lf.AddCheckbox("Remember Me", false, nil) lf.AddButton("Login", lf.login) lf.SetButtonBackgroundColor(tcell.ColorGreen) lf.SetTitle("Login") lf.SetTitleColor(tcell.GetColor(cfg.Theme.TitleColor)) lf.SetTitleAlign(tview.AlignLeft) p := cfg.Theme.BorderPadding lf.SetBorder(cfg.Theme.Border) lf.SetBorderColor(tcell.GetColor(cfg.Theme.BorderColor)) lf.SetBorderPadding(p[0], p[1], p[2], p[3]) lf.updateColor() return lf } func (lf *loginForm) updateColor() { emailInput := lf.GetFormItem(0).(*tview.InputField) passwordInput := lf.GetFormItem(1).(*tview.InputField) codeInput := lf.GetFormItem(2).(*tview.InputField) checkbox := lf.GetFormItem(3).(*tview.Checkbox) // Log checks for the form items backgroundColor := tcell.ColorBlack textColor := tcell.ColorWhite checkboxColor := tcell.ColorRed // Check if form items exist before updating if emailInput != nil { slog.Info("Email Input: exist") emailInput.SetFieldBackgroundColor(backgroundColor) emailInput.SetFieldTextColor(textColor) } if passwordInput != nil { slog.Info("Password: exist") passwordInput.SetFieldBackgroundColor(backgroundColor) passwordInput.SetFieldTextColor(textColor) } if codeInput != nil { slog.Info("Code Input: exist") codeInput.SetFieldBackgroundColor(backgroundColor) codeInput.SetFieldTextColor(textColor) } if checkbox != nil { slog.Info("Checkbox: exist") checkbox.SetFieldBackgroundColor(backgroundColor) checkbox.SetFieldTextColor(checkboxColor) } } ``` ## Expected Behavior - Input fields for Email, Password, and Code should change to a black background with white text. Checkbox should change to a black background with red text. ## Actual Behavior - The form items exist and are detected (verified via logs), but the colors remain unchanged.
Author
Owner

@jsumners-nr commented on GitHub (Oct 23, 2024):

Have you tried using https://pkg.go.dev/github.com/rivo/tview#InputField.SetFieldStyle? It should look something like:

table.SetSelectedStyle(
	tcell.Style{}.
		Background(tcell.GetColor("#40ea37")).
		Foreground(tcell.ColorBlack),
)
<!-- gh-comment-id:2433183109 --> @jsumners-nr commented on GitHub (Oct 23, 2024): Have you tried using https://pkg.go.dev/github.com/rivo/tview#InputField.SetFieldStyle? It should look something like: ``` table.SetSelectedStyle( tcell.Style{}. Background(tcell.GetColor("#40ea37")). Foreground(tcell.ColorBlack), ) ```
Author
Owner

@rivo commented on GitHub (Nov 2, 2024):

The documentation for AddFormItem() explains this:

Note, however, that the Form class will override some of its attributes to make it work in the form context. Specifically, these are:

  • The label width
  • The label color
  • The background color
  • The field text color
  • The field background color

You'll want to use Form.SetFieldBackgroundColor() and Form.SetFieldTextColor(), or, even better, the new Form.SetFieldStyle() function if you want to change the appearance of the form elements.

<!-- gh-comment-id:2453022817 --> @rivo commented on GitHub (Nov 2, 2024): The documentation for [`AddFormItem()`](https://pkg.go.dev/github.com/rivo/tview#Form.AddFormItem) explains this: > Note, however, that the Form class will override some of its attributes to make it work in the form context. Specifically, these are: > > - The label width > - The label color > - The background color > - The field text color > - The field background color You'll want to use [`Form.SetFieldBackgroundColor()`](https://pkg.go.dev/github.com/rivo/tview#Form.SetFieldBackgroundColor) and [`Form.SetFieldTextColor()`](https://pkg.go.dev/github.com/rivo/tview#Form.SetFieldTextColor), or, even better, the new `Form.SetFieldStyle()` function if you want to change the appearance of the form elements.
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#755
No description provided.