Skip to content

Commit

Permalink
docs: full bubbletea example
Browse files Browse the repository at this point in the history
  • Loading branch information
maaslalani committed Dec 4, 2023
1 parent b01b7ab commit b37ec74
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,47 @@ type Model struct {
// embed form in parent model, use as bubble.
form *huh.Form
}

func NewModel() Model {
return Model{
form: huh.NewForm(
huh.NewGroup(
huh.NewSelect[string]().
Key("class").
Options(huh.NewOptions("Warrior", "Mage", "Rogue")...).
Title("Choose your class"),

huh.NewSelect[int]().
Key("level").
Options(huh.NewOptions(1, 20, 9999)...).
Title("Choose your level"),
),
)
}
}

func (m Model) Init() tea.Cmd {
return m.form.Init()
}

func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
// ...

form, cmd := m.form.Update(msg)
if f, ok := form.(*huh.Form); ok {
m.form = f
}

return m, cmd
}

func (m Model) View() string {
if m.form.State == huh.StateCompleted {
return fmt.Sprintf("You selected: %s, Lvl. %d", m.form.GetString("class"), m.form.GetInt("level"))
}
return m.form.View()
}

```

See [the Bubble Tea example][example] for how to embed `huh` forms in [Bubble
Expand Down

0 comments on commit b37ec74

Please sign in to comment.