From 2eb851f67645596719af1da2c40f7a5d9424a872 Mon Sep 17 00:00:00 2001 From: bashbunni <15822994+bashbunni@users.noreply.github.com> Date: Sat, 3 Aug 2024 00:00:13 +0000 Subject: [PATCH] fix(multiselect): set viewport height in WithHeight(#350) * chore: add example of multiple groups with varying heights * fix(multiselect): set viewport height in initial render --- examples/multiple-groups/main.go | 79 ++++++++++++++++++++++++++++++++ field_multiselect.go | 5 +- 2 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 examples/multiple-groups/main.go diff --git a/examples/multiple-groups/main.go b/examples/multiple-groups/main.go new file mode 100644 index 00000000..fb1353ad --- /dev/null +++ b/examples/multiple-groups/main.go @@ -0,0 +1,79 @@ +package main + +import ( + "fmt" + "os" + + "github.com/charmbracelet/huh" +) + +func main() { + f := huh.NewForm( + huh.NewGroup( + huh.NewSelect[string](). + Options( + huh.NewOption("A", "a"), + huh.NewOption("B", "b"), + huh.NewOption("C", "c"), + huh.NewOption("D", "d"), + huh.NewOption("E", "e"), + huh.NewOption("F", "f"), + huh.NewOption("G", "g"), + huh.NewOption("H", "h"), + huh.NewOption("I", "i"), + huh.NewOption("J", "j"), + huh.NewOption("K", "k").Selected(true), + huh.NewOption("L", "l"), + huh.NewOption("M", "m"), + huh.NewOption("N", "n"), + huh.NewOption("O", "o"), + huh.NewOption("P", "p"), + ), + ).WithHeight(8), + huh.NewGroup( + huh.NewMultiSelect[string](). + Options( + huh.NewOption("A", "a"), + huh.NewOption("B", "b"), + huh.NewOption("C", "c"), + huh.NewOption("D", "d"), + huh.NewOption("E", "e"), + huh.NewOption("F", "f"), + huh.NewOption("G", "g"), + huh.NewOption("H", "h"), + huh.NewOption("I", "i"), + huh.NewOption("K", "k").Selected(true), + huh.NewOption("L", "l"), + huh.NewOption("M", "m"), + huh.NewOption("N", "n"), + huh.NewOption("O", "o").Selected(true), + huh.NewOption("P", "p"), + ), + ).WithHeight(10), + huh.NewGroup( + huh.NewSelect[string](). + Options( + huh.NewOption("A", "a"), + huh.NewOption("B", "b"), + huh.NewOption("C", "c"), + huh.NewOption("D", "d"), + huh.NewOption("E", "e"), + huh.NewOption("F", "f"), + huh.NewOption("G", "g"), + huh.NewOption("H", "h"), + huh.NewOption("I", "i"), + huh.NewOption("J", "j"), + huh.NewOption("K", "k").Selected(true), + huh.NewOption("L", "l"), + huh.NewOption("M", "m"), + huh.NewOption("N", "n"), + huh.NewOption("O", "o"), + huh.NewOption("P", "p"), + ), + ).WithHeight(5), + ) + + if err := f.Run(); err != nil { + fmt.Fprintf(os.Stderr, "Oof: %v", err) + } +} diff --git a/field_multiselect.go b/field_multiselect.go index 4afba3bd..ce150b7f 100644 --- a/field_multiselect.go +++ b/field_multiselect.go @@ -692,9 +692,10 @@ func (m *MultiSelect[T]) WithWidth(width int) Field { return m } -// WithHeight sets the height of the multi-select field. +// WithHeight sets the total height of the multi-select field. Including padding +// and help menu heights. func (m *MultiSelect[T]) WithHeight(height int) Field { - m.height = height + m.Height(height) return m }