Skip to content

Commit

Permalink
refactor: use FirstField and LastField
Browse files Browse the repository at this point in the history
  • Loading branch information
maaslalani committed Jan 24, 2024
1 parent 9cd22a8 commit 355812c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
4 changes: 3 additions & 1 deletion field_note.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,9 @@ func (n *Note) WithHeight(height int) Field {

// WithPosition sets the position information of the note field.
func (n *Note) WithPosition(p FieldPosition) Field {
if p.FieldCount == 1 {
// if the note is the only field on the screen,
// we shouldn't skip the entire group.
if p.Field == p.FirstField && p.Field == p.LastField {
n.skip = false
}
n.keymap.Prev.SetEnabled(!p.IsFirst())
Expand Down
10 changes: 6 additions & 4 deletions form.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,20 +147,21 @@ type Field interface {
type FieldPosition struct {
Group int
Field int
FieldCount int
FirstField int
LastField int
GroupCount int
FirstGroup int
LastGroup int
}

// IsFirst returns whether a field is the form's first field.
func (p FieldPosition) IsFirst() bool {
return p.Field == 0 && p.Group == p.FirstGroup
return p.Field == p.FirstField && p.Group == p.FirstGroup
}

// IsLast returns whether a field is the form's last field.
func (p FieldPosition) IsLast() bool {
return p.Field == p.FieldCount-1 && p.Group == p.LastGroup
return p.Field == p.LastField && p.Group == p.LastGroup
}

// nextGroupMsg is a message to move to the next group.
Expand Down Expand Up @@ -296,7 +297,8 @@ func (f *Form) UpdateFieldPositions() *Form {
field.WithPosition(FieldPosition{
Group: g,
Field: i,
FieldCount: len(group.fields),
FirstField: 0,
LastField: len(group.fields) - 1,
FirstGroup: firstGroup,
LastGroup: lastGroup,
})
Expand Down

0 comments on commit 355812c

Please sign in to comment.