Skip to content

Commit

Permalink
fix(multiselect): set viewport height in WithHeight(#350)
Browse files Browse the repository at this point in the history
* chore: add example of multiple groups with varying heights

* fix(multiselect): set viewport height in initial render
  • Loading branch information
bashbunni authored Aug 3, 2024
1 parent 67d4a43 commit 2eb851f
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 2 deletions.
79 changes: 79 additions & 0 deletions examples/multiple-groups/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
5 changes: 3 additions & 2 deletions field_multiselect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 2eb851f

Please sign in to comment.