Skip to content

Commit

Permalink
feat: implement results map through GetKey and GetValue methods on field
Browse files Browse the repository at this point in the history
  • Loading branch information
maaslalani committed Nov 14, 2023
1 parent 52b4986 commit 65d2d5a
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 53 deletions.
19 changes: 10 additions & 9 deletions field_confirm.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ import (

// Confirm is a form confirm field.
type Confirm struct {
value *bool
results map[string]any
key string
value *bool
key string

// customization
title string
Expand Down Expand Up @@ -103,7 +102,6 @@ func (c *Confirm) Focus() tea.Cmd {
func (c *Confirm) Blur() tea.Cmd {
c.focused = false
c.err = c.validate(*c.value)
c.results[c.key] = *c.value
return nil
}

Expand All @@ -130,7 +128,6 @@ func (c *Confirm) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case key.Matches(msg, c.keymap.Toggle):
v := !*c.value
*c.value = v
c.results[c.key] = v
case key.Matches(msg, c.keymap.Prev):
cmds = append(cmds, prevField)
case key.Matches(msg, c.keymap.Next):
Expand Down Expand Up @@ -225,8 +222,12 @@ func (c *Confirm) WithWidth(width int) Field {
return c
}

// WithResults sets the results of the confirm field.
func (c *Confirm) WithResults(r map[string]any) Field {
c.results = r
return c
// GetKey returns the key of the field.
func (c *Confirm) GetKey() string {
return c.key
}

// GetValue returns the value of the field.
func (c *Confirm) GetValue() any {
return *c.value
}
19 changes: 10 additions & 9 deletions field_input.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ import (

// Input is a form input field.
type Input struct {
value *string
results map[string]any
key string
value *string
key string

// customization
title string
Expand Down Expand Up @@ -55,7 +54,6 @@ func NewInput() *Input {
// Value sets the value of the input field.
func (i *Input) Value(value *string) *Input {
i.value = value
i.results[i.key] = value
i.textinput.SetValue(*value)
return i
}
Expand Down Expand Up @@ -156,7 +154,6 @@ func (i *Input) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
i.textinput, cmd = i.textinput.Update(msg)
cmds = append(cmds, cmd)
*i.value = i.textinput.Value()
i.results[i.key] = i.textinput.Value()

switch msg := msg.(type) {
case tea.KeyMsg:
Expand Down Expand Up @@ -253,8 +250,12 @@ func (i *Input) WithWidth(width int) Field {
return i
}

// WithResults sets the results of the input field.
func (i *Input) WithResults(r map[string]any) Field {
i.results = r
return i
// GetKey returns the key of the field.
func (i *Input) GetKey() string {
return i.key
}

// GetValue returns the value of the field.
func (i *Input) GetValue() any {
return *i.value
}
18 changes: 10 additions & 8 deletions field_multiselect.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ import (

// MultiSelect is a form multi-select field.
type MultiSelect[T any] struct {
value *[]T
results map[string]any
key string
value *[]T
key string

// customization
title string
Expand Down Expand Up @@ -174,7 +173,6 @@ func (m *MultiSelect[T]) finalize() {
*m.value = append(*m.value, option.Value)
}
}
m.results[m.key] = *m.value
m.err = m.validate(*m.value)
}

Expand Down Expand Up @@ -309,8 +307,12 @@ func (m *MultiSelect[T]) WithWidth(width int) Field {
return m
}

// WithResults sets the results of the multi-select field.
func (m *MultiSelect[T]) WithResults(r map[string]any) Field {
m.results = r
return m
// GetKey returns the multi-select's key.
func (m *MultiSelect[T]) GetKey() string {
return m.key
}

// GetValue returns the multi-select's value.
func (m *MultiSelect[T]) GetValue() any {
return *m.value
}
11 changes: 8 additions & 3 deletions field_note.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,14 @@ func (n *Note) WithWidth(width int) Field {
return n
}

// WithResults sets the results map of the note field.
func (n *Note) WithResults(r map[string]any) Field {
return n
// GetValue satisfies the Field interface, notes do not have values.
func (n *Note) GetValue() any {
return nil
}

// GetKey satisfies the Field interface, notes do not have keys.
func (n *Note) GetKey() string {
return ""
}

// pointerTo returns a pointer to a value.
Expand Down
19 changes: 10 additions & 9 deletions field_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ import (

// Select is a form select field.
type Select[T any] struct {
value *T
results map[string]any
key string
value *T
key string

// customization
title string
Expand Down Expand Up @@ -173,7 +172,6 @@ func (s *Select[T]) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return s, nil
}
*s.value = value
s.results[s.key] = value
return s, prevField
case key.Matches(msg, s.keymap.Next):
if s.selected >= len(s.filteredOptions) {
Expand All @@ -186,7 +184,6 @@ func (s *Select[T]) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return s, nil
}
*s.value = value
s.results[s.key] = value
return s, nextField
}

Expand Down Expand Up @@ -327,8 +324,12 @@ func (s *Select[T]) WithWidth(width int) Field {
return s
}

// WithResults sets the results of the select field.
func (s *Select[T]) WithResults(r map[string]any) Field {
s.results = r
return s
// GetKey returns the key of the field.
func (s *Select[T]) GetKey() string {
return s.key
}

// GetValue returns the value of the field.
func (s *Select[T]) GetValue() any {
return *s.value
}
18 changes: 10 additions & 8 deletions field_text.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ import (

// Text is a form text field. It allows for a multi-line string input.
type Text struct {
value *string
results map[string]any
key string
value *string
key string

// error handling
validate func(string) error
Expand Down Expand Up @@ -155,7 +154,6 @@ func (t *Text) Blur() tea.Cmd {
*t.value = t.textarea.Value()
t.textarea.Blur()
t.err = t.validate(*t.value)
t.results[t.key] = t.textarea.Value()
return nil
}

Expand Down Expand Up @@ -288,8 +286,12 @@ func (t *Text) WithWidth(width int) Field {
return t
}

// WithResults sets the results of the text field.
func (t *Text) WithResults(r map[string]any) Field {
t.results = r
return t
// GetKey returns the key of the field.
func (t *Text) GetKey() string {
return t.key
}

// GetValue returns the value of the field.
func (t *Text) GetValue() any {
return *t.value
}
16 changes: 11 additions & 5 deletions form.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ func NewForm(groups ...*Group) *Form {
theme: NewCharmTheme(),
keymap: NewDefaultKeyMap(),
width: defaultWidth,
width: 80,
results: map[string]any{},
results: make(map[string]any),
}

// NB: If dynamic forms come into play this will need to be applied when
Expand Down Expand Up @@ -125,8 +124,11 @@ type Field interface {
// WithWidth sets the width of a field.
WithWidth(int) Field

// WithResults sets where to store the result of a field.
WithResults(map[string]any) Field
// GetKey returns the field's key.
GetKey() string

// GetValue returns the field's value.
GetValue() any
}

// nextGroupMsg is a message to move to the next group.
Expand Down Expand Up @@ -222,7 +224,6 @@ func (f *Form) Get(key string) any {
func (f *Form) Init() tea.Cmd {
cmds := make([]tea.Cmd, len(f.groups))
for i, group := range f.groups {
group.results = f.results
cmds[i] = group.Init()
}
return tea.Batch(cmds...)
Expand All @@ -243,6 +244,11 @@ func (f *Form) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return f, f.cancelCmd
}

case nextFieldMsg:
// Form is progressing to the next field, let's save the value of the current field.
field := group.fields[group.paginator.Page]
f.results[field.GetKey()] = field.GetValue()

case nextGroupMsg:
if len(group.Errors()) > 0 {
return f, nil
Expand Down
2 changes: 0 additions & 2 deletions group.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ type Group struct {
// collection of fields
fields []Field

results map[string]any

// information
title string
description string
Expand Down

0 comments on commit 65d2d5a

Please sign in to comment.