[GH-ISSUE #1110] Bug: InputField SetChangedFunc callback gets called twice #807

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

Originally created by @kivattt on GitHub (Aug 17, 2025).
Original GitHub issue: https://github.com/rivo/tview/issues/1110

When you set a callback function with inputField.SetChangedFunc(), the callback is called twice instead of once on every change to the text.
I believe this was introduced with github.com/rivo/tview@8b7bcf9883

This code in NewInputField() forwards the changed function to the InputField's TextArea:

i.textArea.SetChangedFunc(func() {
    if i.changed != nil {
        i.changed(i.textArea.GetText())
    }
})

But the code in InputField's InputHandler() duplicates that call to changed:

if i.changed != nil {
    i.changed(newText)
}

I think the solution is to remove the duplicate call to i.changed() in the InputHandler() for inputfield.go

Code to reproduce this bug:

package main

import (
    "fmt"

    "github.com/rivo/tview"
)

func main() {
    app := tview.NewApplication()
    textView := tview.NewTextView()
    inputField := tview.NewInputField()

    counter := 0

    inputField.SetChangedFunc(func(text string) {
        counter += 1
        textView.SetText(fmt.Sprint(counter))
    })

    flex := tview.NewFlex().
        AddItem(inputField, 0, 1, true).
        AddItem(textView, 0, 1, false)

    if err := app.SetRoot(flex, true).EnableMouse(true).EnablePaste(true).Run(); err != nil {
        panic(err)
    }
}
Image

I wrote 3 letters, but the callback was called 6 times

Originally created by @kivattt on GitHub (Aug 17, 2025). Original GitHub issue: https://github.com/rivo/tview/issues/1110 When you set a callback function with `inputField.SetChangedFunc()`, the callback is called twice instead of once on every change to the text. I believe this was introduced with https://github.com/rivo/tview/commit/8b7bcf9883df5624cff65c169194c3b70148e4b9 This code in `NewInputField()` forwards the changed function to the InputField's TextArea: ```go i.textArea.SetChangedFunc(func() { if i.changed != nil { i.changed(i.textArea.GetText()) } }) ``` But the code in InputField's `InputHandler()` duplicates that call to changed: ```go if i.changed != nil { i.changed(newText) } ``` I think the solution is to remove the duplicate call to `i.changed()` in the `InputHandler()` for `inputfield.go` Code to reproduce this bug: ```go package main import ( "fmt" "github.com/rivo/tview" ) func main() { app := tview.NewApplication() textView := tview.NewTextView() inputField := tview.NewInputField() counter := 0 inputField.SetChangedFunc(func(text string) { counter += 1 textView.SetText(fmt.Sprint(counter)) }) flex := tview.NewFlex(). AddItem(inputField, 0, 1, true). AddItem(textView, 0, 1, false) if err := app.SetRoot(flex, true).EnableMouse(true).EnablePaste(true).Run(); err != nil { panic(err) } } ``` <img width="262" height="100" alt="Image" src="https://github.com/user-attachments/assets/7e9f955b-7a45-40fc-8242-8e6752673e09" /> I wrote 3 letters, but the callback was called 6 times
kerem closed this issue 2026-03-04 01:07:53 +03:00
Author
Owner

@rivo commented on GitHub (Aug 27, 2025):

Thanks. Fixed it in the latest commit.

<!-- gh-comment-id:3228203977 --> @rivo commented on GitHub (Aug 27, 2025): Thanks. Fixed it in the latest commit.
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#807
No description provided.