Skip to content

Added collection of FormItemArgs for each of the form FormItem types#300

Open
mikeschinkel wants to merge 1 commit intorivo:masterfrom
mikeschinkel:formitemargs
Open

Added collection of FormItemArgs for each of the form FormItem types#300
mikeschinkel wants to merge 1 commit intorivo:masterfrom
mikeschinkel:formitemargs

Conversation

@mikeschinkel
Copy link
Copy Markdown
Contributor

After working with Forms for a while and requesting several PRs to make working with them easier I finally had enough with the complexity and decided to implement an alternate method to add form fields than <form>.AddInputField(), <form>.AddPasswordField(), <form>.AddDropdown(), and <form>.AddCheckbox().

The *biggest problem I was trying to fix were that once a field was added it was tedious to then access the form to set other properties not allow to be set in the FormItems New...() method. So I tried numerous iterations with the absolute requirement of backward compatibility and came up with the following, by example:

type Object struct {
    Name     string
    Birthday string
    Password string
    Confirm  string
    FavColor string
    Website  string
    Customer bool
}
type Strings [] string

func (ss Strings) Index(str string) int {
    pos := -1
    for i, v := range ss {
        if v != str {
            continue
        }
        pos = i
        break
    }
    return pos
}

var colors = Strings{"Red", "Green", "Blue"}

func AddFields(form *tview.Form, obj Object) *tview.Form {
    return form.Clear(true).
        AddFormItem(NewInputField(), &InputFieldArgs{
            Label:       "Name:",
            Text:        obj.Name,
            FieldWidth:  40,
            Placeholder: "Enter your full name",
        }).
        AddFormItem(NewInputField(), &InputFieldArgs{
            Label:      "Birthday:",
            Text:       obj.Birthday,
            FieldWidth: 12,
            DoneFunc: func(key tcell.Key) {
                // do something with birthday
            },
        }).
        AddFormItem(NewDropDown(), &DropDownArgs{
            Label:         "Favorite Color?",
            Options:       colors,
            InitialOption: colors.Index(obj.FavColor),
            SelectedFunc: func(option string, optionIndex int) {
                // do something with selected option
            },
        }).
        AddFormItem(NewInputField(), &InputFieldArgs{
            Label:                "Website:",
            Text:                 obj.Website,
            FieldWidth:           60,
            FieldBackgroundColor: tcell.ColorAliceBlue,
            FieldTextColor:       tcell.ColorGreen,
        }).
        AddFormItem(NewCheckbox(), &CheckboxArgs{
            Label:   "Customer?",
            Checked: obj.Customer,

        }).
        AddFormItem(NewPasswordField(), &PasswordFieldArgs{
            Label:      "Password:",
            Text:       obj.Password,
            FieldWidth: 30,
        }).
        AddFormItem(NewPasswordField(), &PasswordFieldArgs{
            Label:      "Confirm:",
            Text:       obj.Confirm,
            FieldWidth: 30,
        })
}

I would have preferred to use the names AddInputField() etc. but could not do so w/o breaking old code so I extended AddFormItem() instead.

There is also a ton of effectively duplicated code, but given the structure of the existing code I could not find a way to consolidate code without breaking backward compatibility. If breaking backward compatibility is an option, I could change the code to be more maintainable, but I needed the functionality and I assumed you would be much less likely to access broken backward compatibility so I went ahead and implemented as is.

Hopefully you will concur this is a good addition to tview and accept this PR? Thanks in advance for considering.

@mikeschinkel
Copy link
Copy Markdown
Contributor Author

mikeschinkel commented Jun 10, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant