feat(inputfield): Add ability to show a note below the inputfield

This commit is contained in:
Andreas Bieber 2020-09-17 17:14:01 +02:00
parent 128c6692d4
commit 9142563f22
5 changed files with 64 additions and 1 deletions

View File

@ -215,6 +215,11 @@ func (c *CheckBox) SetFormAttributes(labelWidth int, bgColor, labelColor, labelC
return c
}
// GetFieldHeight returns the height of the field.
func (c *CheckBox) GetFieldHeight() int {
return 1
}
// GetFieldWidth returns this primitive's field width.
func (c *CheckBox) GetFieldWidth() int {
c.RLock()

View File

@ -11,6 +11,10 @@ func main() {
AddDropDownSimple("Title", 0, nil, "Mr.", "Ms.", "Mrs.", "Dr.", "Prof.").
AddInputField("First name", "", 20, nil, nil).
AddInputField("Last name", "", 20, nil, nil).
AddFormItem(cview.NewInputField().
SetLabel("Address").
SetFieldWidth(30).
SetFieldNote("Your complete address")).
AddPasswordField("Password", "", 10, '*', nil).
AddCheckBox("", "Age 18+", false, nil).
AddButton("Save", nil).

View File

@ -403,6 +403,11 @@ func (d *DropDown) SetFieldWidth(width int) *DropDown {
return d
}
// GetFieldHeight returns the height of the field.
func (d *DropDown) GetFieldHeight() int {
return 1
}
// GetFieldWidth returns this primitive's field screen width.
func (d *DropDown) GetFieldWidth() int {
d.RLock()

View File

@ -28,6 +28,9 @@ type FormItem interface {
// required.
GetFieldWidth() int
// GetFieldHeight returns the height of the form item.
GetFieldHeight() int
// SetFinishedFunc sets the handler function for when the user finished
// entering data into the item. The handler may receive events for the
// Enter key (we're done), the Escape key (cancel input), the Tab key (move to
@ -640,7 +643,7 @@ func (f *Form) Draw(screen tcell.Screen) {
if f.horizontal {
x += itemWidth + f.itemPadding
} else {
y += 1 + f.itemPadding
y += item.GetFieldHeight() + f.itemPadding
}
}

View File

@ -79,6 +79,12 @@ type InputField struct {
// The text color of the suggestion.
autocompleteSuggestionTextColor tcell.Color
// The text color of the note below the input field.
fieldNoteTextColor tcell.Color
// The note to show below the input field.
fieldNote string
// The screen width of the label area. A value of 0 means use the width of
// the label text.
labelWidth int
@ -147,6 +153,7 @@ func NewInputField() *InputField {
autocompleteListSelectedTextColor: Styles.PrimitiveBackgroundColor,
autocompleteListSelectedBackgroundColor: Styles.PrimaryTextColor,
autocompleteSuggestionTextColor: Styles.ContrastPrimaryTextColor,
fieldNoteTextColor: Styles.SecondaryTextColor,
}
}
@ -321,6 +328,30 @@ func (i *InputField) SetAutocompleteSuggestionTextColor(color tcell.Color) *Inpu
return i
}
// SetFieldNoteTextColor sets the text color of the note.
func (i *InputField) SetFieldNoteTextColor(color tcell.Color) *InputField {
i.Lock()
defer i.Unlock()
i.fieldNoteTextColor = color
return i
}
// SetFieldNote sets the text to show below the input field, e.g. when the input is invalid.
func (i *InputField) SetFieldNote(note string) *InputField {
i.Lock()
defer i.Unlock()
i.fieldNote = note
return i
}
// ResetFieldNote sets the note to an empty string.
func (i *InputField) ResetFieldNote() *InputField {
i.Lock()
defer i.Unlock()
i.fieldNote = ""
return i
}
// SetFormAttributes sets attributes shared by all form items.
func (i *InputField) SetFormAttributes(labelWidth int, bgColor, labelColor, labelColorFocused, fieldTextColor, fieldTextColorFocused, fieldBgColor, fieldBgColorFocused tcell.Color) FormItem {
i.Lock()
@ -355,6 +386,16 @@ func (i *InputField) GetFieldWidth() int {
return i.fieldWidth
}
// GetFieldHeight returns the height of the field.
func (i *InputField) GetFieldHeight() int {
i.RLock()
defer i.RUnlock()
if i.fieldNote == "" {
return 1
}
return 2
}
// GetCursorPosition returns the cursor position.
func (i *InputField) GetCursorPosition() int {
i.RLock()
@ -630,6 +671,11 @@ func (i *InputField) Draw(screen tcell.Screen) {
}
}
// Draw field note
if i.fieldNote != "" {
Print(screen, i.fieldNote, x, y+1, fieldWidth, AlignLeft, i.fieldNoteTextColor)
}
// Draw autocomplete list.
if i.autocompleteList != nil {
// How much space do we need?