Skip to content

Commit

Permalink
fix: form and group styles
Browse files Browse the repository at this point in the history
  • Loading branch information
nervo committed Jun 21, 2024
1 parent 90406d7 commit 3ce3506
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
15 changes: 13 additions & 2 deletions form.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/paginator"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)

const defaultWidth = 80
Expand Down Expand Up @@ -65,6 +66,7 @@ type Form struct {
// options
width int
height int
theme *Theme
keymap *KeyMap
timeout time.Duration
teaOptions []tea.ProgramOption
Expand Down Expand Up @@ -240,9 +242,10 @@ func (f *Form) WithShowErrors(v bool) *Form {
// can be applied to each group and field individually for more granular
// control.
func (f *Form) WithTheme(theme *Theme) *Form {
if theme == nil {
if f.theme != nil {
return f
}
f.theme = theme
for _, group := range f.groups {
group.WithTheme(theme)
}
Expand Down Expand Up @@ -575,13 +578,21 @@ func (f *Form) isGroupHidden(page int) bool {
return hide()
}

func (f *Form) style() lipgloss.Style {
theme := f.theme
if theme == nil {
theme = ThemeCharm()
}
return theme.Form
}

// View renders the form.
func (f *Form) View() string {
if f.quitting {
return ""
}

return f.layout.View(f)
return f.style().Render(f.layout.View(f))
}

// Run runs the form.
Expand Down
16 changes: 15 additions & 1 deletion group.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type Group struct {
// group options
width int
height int
theme *Theme
keymap *KeyMap
hide func() bool
active bool
Expand Down Expand Up @@ -92,6 +93,10 @@ func (g *Group) WithShowErrors(show bool) *Group {

// WithTheme sets the theme on a group.
func (g *Group) WithTheme(t *Theme) *Group {
if g.theme != nil {
return g
}
g.theme = t
g.help.Styles = t.Help
for _, field := range g.fields {
field.WithTheme(t)
Expand Down Expand Up @@ -265,6 +270,14 @@ func (g *Group) fullHeight() int {
return height
}

func (g *Group) style() lipgloss.Style {
theme := g.theme
if theme == nil {
theme = ThemeCharm()
}
return theme.Group
}

func (g *Group) getContent() (int, string) {
var fields strings.Builder
offset := 0
Expand Down Expand Up @@ -320,8 +333,9 @@ func (g *Group) Footer() string {
}
if g.showErrors {
for _, err := range errors {
// !!! Non
view.WriteString(ThemeCharm().Focused.ErrorMessage.Render(err.Error()))
}
}
return view.String()
return g.style().Render(view.String())
}
2 changes: 2 additions & 0 deletions theme.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ const (
func ThemeBase() *Theme {
var t Theme

t.Form = lipgloss.NewStyle()
t.Group = lipgloss.NewStyle()
t.FieldSeparator = lipgloss.NewStyle().SetString("\n\n")

button := lipgloss.NewStyle().
Expand Down

0 comments on commit 3ce3506

Please sign in to comment.