[GH-ISSUE #908] InputField calls changed function three times when using .SetText #663

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

Originally created by @vitorqb on GitHub (Nov 1, 2023).
Original GitHub issue: https://github.com/rivo/tview/issues/908

Hi 👋, thanks for this great library.

Using the latest version (github.com/rivo/tview v0.0.0-20231031172508-2dfe06011790) I have found an inconsistent behavior for InputField. I believe it was introduced at commit 6416d6b.

When calling .SetText on an InputField, the changed function gets called three times.

Here is a repo that reproduces the issue: https://github.com/vitorqb/tview-bug

Here is the base snippet:

package main

import (
	"fmt"

	"github.com/rivo/tview"
)

var ChangeCalls []string

func main() {
	inputField := tview.NewInputField()
	inputField.SetChangedFunc(func(text string) {
		ChangeCalls = append(ChangeCalls, text)
	})
	inputField.SetText("Hello, World!")
	fmt.Println("Text: " + inputField.GetText())
	fmt.Println("ChangeCalls: " + fmt.Sprintf("%v", ChangeCalls))	
	fmt.Println("ChangeCallCount: " + fmt.Sprintf("%v", len(ChangeCalls)))
}

Which prints

Text: Hello, World!
ChangeCalls: [Hello, World! Hello, World! Hello, World!]
ChangeCallCount: 3

As you can see the function given to SetChangedFunc was called 3 times with the same string, even though the input just changed once (call to .SetText). I expected it to be called only once.

Originally created by @vitorqb on GitHub (Nov 1, 2023). Original GitHub issue: https://github.com/rivo/tview/issues/908 Hi :wave:, thanks for this great library. Using the latest version (`github.com/rivo/tview v0.0.0-20231031172508-2dfe06011790`) I have found an inconsistent behavior for InputField. I believe it was introduced at commit [6416d6b](https://github.com/rivo/tview/commit/6416d6b23c67b0b87aafc9af8d4a9f0b7840df10). When calling `.SetText` on an InputField, the `changed` function gets called three times. Here is a repo that reproduces the issue: https://github.com/vitorqb/tview-bug Here is the base snippet: ```go package main import ( "fmt" "github.com/rivo/tview" ) var ChangeCalls []string func main() { inputField := tview.NewInputField() inputField.SetChangedFunc(func(text string) { ChangeCalls = append(ChangeCalls, text) }) inputField.SetText("Hello, World!") fmt.Println("Text: " + inputField.GetText()) fmt.Println("ChangeCalls: " + fmt.Sprintf("%v", ChangeCalls)) fmt.Println("ChangeCallCount: " + fmt.Sprintf("%v", len(ChangeCalls))) } ``` Which prints ``` Text: Hello, World! ChangeCalls: [Hello, World! Hello, World! Hello, World!] ChangeCallCount: 3 ``` As you can see the function given to `SetChangedFunc` was called 3 times with the same string, even though the input just changed once (call to `.SetText`). I expected it to be called only once.
kerem closed this issue 2026-03-04 01:06:52 +03:00
Author
Owner

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

Thank you for letting me know about this. Yes, there were indeed a few too many calls to the "changed" callback in the code. The latest commit should fix this.

<!-- gh-comment-id:1791314500 --> @rivo commented on GitHub (Nov 2, 2023): Thank you for letting me know about this. Yes, there were indeed a few too many calls to the "changed" callback in the code. The latest commit should fix this.
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#663
No description provided.