Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement Minimum to MultiSelect #336

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions field_multiselect.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type MultiSelect[T comparable] struct {
filterable bool
filteredOptions []Option[T]
limit int
minimum int
height int

// error handling
Expand Down Expand Up @@ -174,6 +175,12 @@ func (m *MultiSelect[T]) Limit(limit int) *MultiSelect[T] {
return m
}

// Minimum sets the minimum of the multi-select field.
func (m *MultiSelect[T]) Minimum(minimum int) *MultiSelect[T] {
m.minimum = minimum
return m
}

// Height sets the height of the multi-select field.
func (m *MultiSelect[T]) Height(height int) *MultiSelect[T] {
// What we really want to do is set the height of the viewport, but we
Expand Down Expand Up @@ -379,13 +386,21 @@ func (m *MultiSelect[T]) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.updateValue()
case key.Matches(msg, m.keymap.Prev):
m.updateValue()
if m.minimum > 0 && m.numSelected() < m.minimum {
m.err = fmt.Errorf("you must select at least %d options", m.minimum)
return m, nil
}
m.err = m.validate(m.accessor.Get())
if m.err != nil {
return m, nil
}
return m, PrevField
case key.Matches(msg, m.keymap.Next, m.keymap.Submit):
m.updateValue()
if m.minimum > 0 && m.numSelected() < m.minimum {
m.err = fmt.Errorf("you must select at least %d options", m.minimum)
return m, nil
}
m.err = m.validate(m.accessor.Get())
if m.err != nil {
return m, nil
Expand Down
57 changes: 57 additions & 0 deletions huh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@ func TestForm(t *testing.T) {
Toppings []string
}

type Beverages struct {
Name []string
}

type Order struct {
Taco Taco
Beverages Beverages
Name string
Instructions string
Discount bool
Expand Down Expand Up @@ -83,6 +88,24 @@ func TestForm(t *testing.T) {
Limit(4),
),

// Prompt for beverages.
// The customer can ask for at least two beverages.
NewGroup(
NewMultiSelect[string]().
Title("Beverages").
Description("Choose at least two.").
Options(
NewOption("Coke", "coke").Selected(true),
NewOption("Pepsi", "pepsi").Selected(true),
NewOption("Sprite", "sprite"),
NewOption("Fanta", "fanta"),
NewOption("Dr. Pepper", "dr. pepper"),
).
Value(&order.Beverages.Name).
Filterable(true).
Minimum(2),
),

// Gather final details for the order.
NewGroup(
NewInput().
Expand Down Expand Up @@ -219,6 +242,40 @@ func TestForm(t *testing.T) {
m = batchUpdate(m.Update(tea.KeyMsg{Type: tea.KeyEnter}))
view = ansi.Strip(m.View())

//
// ┃ Beverages
// ┃ Choose at least two.
// ┃ > ✓ Coke
// ┃ ✓ Pepsi
// ┃ • Sprite
// ┃ • Fanta
// ┃ • Dr. Pepper
//
// x toggle • ↑ up • ↓ down • enter confirm • shift+tab back
//
if !strings.Contains(view, "Beverages") {
t.Log(pretty.Render(view))
t.Fatal("Expected form to show beverages group")
}

if !strings.Contains(view, "Choose at least two.") {
t.Log(pretty.Render(view))
t.Error("Expected form to show beverages description")
}

if !strings.Contains(view, "> ✓ Coke") {
t.Log(pretty.Render(view))
t.Error("Expected form to preselect coke")
}

if !strings.Contains(view, " ✓ Pepsi") {
t.Log(pretty.Render(view))
t.Error("Expected form to preselect pepsi")
}

m = batchUpdate(m.Update(tea.KeyMsg{Type: tea.KeyEnter}))
view = ansi.Strip(m.View())

if !strings.Contains(view, "What's your name?") {
t.Log(pretty.Render(view))
t.Error("Expected form to prompt for name")
Expand Down
Loading