[GH-ISSUE #723] Idea: Add additional "Add..." methods to Form, returning pointer to the field instead of Form #528

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

Originally created by @moson-mo on GitHub (Apr 15, 2022).
Original GitHub issue: https://github.com/rivo/tview/issues/723

Currently there are methods to add form fields (like AddInputField, AddDropDown, etc...) that return a pointer to the form.
This is nice if you want to chain several of those method calls.

Now there might be situations where would rather like to have a pointer to the field that has just been added instead of the form because you need to customize it further.

We could do func (f *Form) AddButton(label string, selected func()) (*Form, *Button) {...
and return both, but that would break all apps currently using that method.

So, the only viable way seems to be to add another method like:
func (f *Form) AddButtonXXX(label string, selected func()) (*Button) {...

  1. Does that make sense?
    (I'm probably missing something but atm, it seems, the only way to get back to your fields is to call GetFormItem... and then do type assertion?)

  2. What should they be named?
    proposals (for the example AddInputField):
    AddInputFieldCustomize
    AddInputFieldCustom
    AddInputFieldExtend
    AddInputFieldReturn

Should be simple to implement, hence I'd volunteer to do this 😉

Originally created by @moson-mo on GitHub (Apr 15, 2022). Original GitHub issue: https://github.com/rivo/tview/issues/723 Currently there are methods to add form fields (like AddInputField, AddDropDown, etc...) that return a pointer to the form. This is nice if you want to chain several of those method calls. Now there might be situations where would rather like to have a pointer to the field that has just been added instead of the form because you need to customize it further. We could do `func (f *Form) AddButton(label string, selected func()) (*Form, *Button) {...` and return both, but that would break all apps currently using that method. So, the only viable way seems to be to add another method like: `func (f *Form) AddButtonXXX(label string, selected func()) (*Button) {...` 1. Does that make sense? (I'm probably missing something but atm, it seems, the only way to get back to your fields is to call GetFormItem... and then do type assertion?) 2. What should they be named? proposals (for the example AddInputField): `AddInputFieldCustomize` `AddInputFieldCustom` `AddInputFieldExtend` `AddInputFieldReturn` Should be simple to implement, hence I'd volunteer to do this :wink:
kerem closed this issue 2026-03-04 01:05:46 +03:00
Author
Owner

@moson-mo commented on GitHub (Jun 20, 2022):

  1. Does that make sense?
    (I'm probably missing something but atm, it seems, the only way to get back to your fields is to call GetFormItem... and then do type assertion?)

Not really. I'm just stupid and missed that there is AddFormItem 🤦
So one can do:

field := tview.NewInputField().
SetLabel("bla").
SetText("blubb").
...
form.AddFormItem(field)
<!-- gh-comment-id:1160596707 --> @moson-mo commented on GitHub (Jun 20, 2022): > 1. Does that make sense? > (I'm probably missing something but atm, it seems, the only way to get back to your fields is to call GetFormItem... and then do type assertion?) Not really. I'm just stupid and missed that there is `AddFormItem` :facepalm: So one can do: ```go field := tview.NewInputField(). SetLabel("bla"). SetText("blubb"). ... form.AddFormItem(field) ```
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#528
No description provided.