From b37ec746d7fe3d7c7eb64daa20bed1cd05c255ec Mon Sep 17 00:00:00 2001 From: Maas Lalani Date: Mon, 4 Dec 2023 10:15:58 -0500 Subject: [PATCH] docs: full bubbletea example --- README.md | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/README.md b/README.md index ec5b7844..25a92145 100644 --- a/README.md +++ b/README.md @@ -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