Skip to content

Commit

Permalink
docs: field_input, field_select, field_text
Browse files Browse the repository at this point in the history
  • Loading branch information
maaslalani committed May 10, 2024
1 parent 33b0042 commit b2166ee
Show file tree
Hide file tree
Showing 5 changed files with 216 additions and 135 deletions.
82 changes: 50 additions & 32 deletions field_input.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,43 @@ import (
"github.com/charmbracelet/lipgloss"
)

// Input is a form input field.
// Input is a input field.
//
// The input field is a field that allows the user to enter text. Use it to user
// input. It can be used for collecting text, passwords, or other short input.
//
// The input field supports Suggestions, Placeholder, and Validation.
type Input struct {
id int
value *string
key string

// customization
title Eval[string]
description Eval[string]
placeholder Eval[string]
suggestions Eval[[]string]

inline bool
textinput textinput.Model

// error handling
inline bool
validate func(string) error
err error
focused bool

// model
textinput textinput.Model

// state
focused bool

// options
accessible bool
width int
height int
accessible bool
theme *Theme
keymap InputKeyMap

theme *Theme
keymap InputKeyMap
}

// NewInput returns a new input field.
// NewInput creates a new input field.
//
// The input field is a field that allows the user to enter text. Use it to user
// input. It can be used for collecting text, passwords, or other short input.
//
// The input field supports Suggestions, Placeholder, and Validation.
func NewInput() *Input {
input := textinput.New()

Expand Down Expand Up @@ -75,27 +79,43 @@ func (i *Input) Key(key string) *Input {
}

// Title sets the title of the input field.
//
// The Title is static for dynamic Title use `TitleFunc`.
func (i *Input) Title(title string) *Input {
i.title.val = title
i.title.fn = nil
return i
}

// Description sets the description of the input field.
//
// The Description is static for dynamic Description use `DescriptionFunc`.
func (i *Input) Description(description string) *Input {
i.description.val = description
i.description.fn = nil
return i
}

// TitleFunc sets the title func of the text field.
// TitleFunc sets the title func of the input field.
//
// The TitleFunc will be re-evaluated when the binding of the TitleFunc changes.
// This is useful when you want to display dynamic content and update the title
// when another part of your form changes.
//
// See README#Dynamic for more usage information.
func (i *Input) TitleFunc(f func() string, bindings any) *Input {
i.title.fn = f
i.title.bindings = bindings
return i
}

// DescriptionFunc sets the description func of the text field.
// DescriptionFunc sets the description func of the input field.
//
// The DescriptionFunc will be re-evaluated when the binding of the
// DescriptionFunc changes. This is useful when you want to display dynamic
// content and update the description when another part of your form changes.
//
// See README#Dynamic for more usage information.
func (i *Input) DescriptionFunc(f func() string, bindings any) *Input {
i.description.fn = f
i.description.bindings = bindings
Expand All @@ -116,6 +136,8 @@ func (i *Input) CharLimit(charlimit int) *Input {

// Suggestions sets the suggestions to display for autocomplete in the input
// field.
//
// The suggestions are static for dynamic suggestions use `SuggestionsFunc`.
func (i *Input) Suggestions(suggestions []string) *Input {
i.suggestions.fn = nil

Expand All @@ -127,6 +149,12 @@ func (i *Input) Suggestions(suggestions []string) *Input {

// SuggestionsFunc sets the suggestions func to display for autocomplete in the
// input field.
//
// The SuggestionsFunc will be re-evaluated when the binding of the
// SuggestionsFunc changes. This is useful when you want to display dynamic
// suggestions when another part of your form changes.
//
// See README#Dynamic for more usage information.
func (i *Input) SuggestionsFunc(f func() []string, bindings any) *Input {
i.suggestions.fn = f
i.suggestions.bindings = bindings
Expand Down Expand Up @@ -198,19 +226,13 @@ func (i *Input) Validate(validate func(string) error) *Input {
}

// Error returns the error of the input field.
func (i *Input) Error() error {
return i.err
}
func (i *Input) Error() error { return i.err }

// Skip returns whether the input should be skipped or should be blocking.
func (*Input) Skip() bool {
return false
}
func (*Input) Skip() bool { return false }

// Zoom returns whether the input should be zoomed.
func (*Input) Zoom() bool {
return false
}
func (*Input) Zoom() bool { return false }

// Focus focuses the input field.
func (i *Input) Focus() tea.Cmd {
Expand Down Expand Up @@ -455,11 +477,7 @@ func (i *Input) WithPosition(p FieldPosition) Field {
}

// GetKey returns the key of the field.
func (i *Input) GetKey() string {
return i.key
}
func (i *Input) GetKey() string { return i.key }

// GetValue returns the value of the field.
func (i *Input) GetValue() any {
return *i.value
}
func (i *Input) GetValue() any { return *i.value }
2 changes: 1 addition & 1 deletion field_note.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (n *Note) TitleFunc(f func() string, bindings any) *Note {

// Description sets the note field's description.
//
// This scription will be static, for dynamic descriptions use `DescriptionFunc`.
// This description will be static, for dynamic descriptions use `DescriptionFunc`.
func (n *Note) Description(description string) *Note {
n.description.val = description
n.description.fn = nil
Expand Down
Loading

0 comments on commit b2166ee

Please sign in to comment.