diff --git a/checkbox.go b/checkbox.go index 3141faa..76f8e59 100644 --- a/checkbox.go +++ b/checkbox.go @@ -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() diff --git a/demos/form/main.go b/demos/form/main.go index 62f69a2..a38907a 100644 --- a/demos/form/main.go +++ b/demos/form/main.go @@ -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). diff --git a/dropdown.go b/dropdown.go index 58105f9..bd18599 100644 --- a/dropdown.go +++ b/dropdown.go @@ -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() diff --git a/form.go b/form.go index 8efcb3a..806171a 100644 --- a/form.go +++ b/form.go @@ -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 } } diff --git a/inputfield.go b/inputfield.go index bd29d7a..1763298 100644 --- a/inputfield.go +++ b/inputfield.go @@ -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?