Skip to content

fix: set filterable not working on multi select #335

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

Merged
merged 4 commits into from
Aug 13, 2024
Merged
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
22 changes: 18 additions & 4 deletions field_multiselect.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func NewMultiSelect[T comparable]() *MultiSelect[T] {
title: Eval[string]{cache: make(map[uint64]string)},
description: Eval[string]{cache: make(map[uint64]string)},
spinner: s,
filterable: true,
}
}

Expand Down Expand Up @@ -225,15 +226,23 @@ func (m *MultiSelect[T]) KeyBinds() []key.Binding {
m.keymap.Toggle,
m.keymap.Up,
m.keymap.Down,
m.keymap.Filter,
m.keymap.SetFilter,
m.keymap.ClearFilter,
}
if m.filterable {
binds = append(
binds,
m.keymap.Filter,
m.keymap.SetFilter,
m.keymap.ClearFilter,
)
}
binds = append(
binds,
m.keymap.Prev,
m.keymap.Submit,
m.keymap.Next,
m.keymap.SelectAll,
m.keymap.SelectNone,
}
)
return binds
}

Expand Down Expand Up @@ -705,6 +714,11 @@ func (m *MultiSelect[T]) WithTheme(theme *Theme) Field {
// WithKeyMap sets the keymap of the multi-select field.
func (m *MultiSelect[T]) WithKeyMap(k *KeyMap) Field {
m.keymap = k.MultiSelect
if !m.filterable {
m.keymap.Filter.SetEnabled(false)
m.keymap.ClearFilter.SetEnabled(false)
m.keymap.SetFilter.SetEnabled(false)
}
return m
}

Expand Down
43 changes: 43 additions & 0 deletions huh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,49 @@ func TestMultiSelect(t *testing.T) {
}
}

func TestMultiSelectFiltering(t *testing.T) {
tests := []struct {
name string
filtering bool
}{
{"Filtering off", false},
{"Filtering on", true},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
field := NewMultiSelect[string]().Options(NewOptions("Foo", "Bar", "Baz")...).Title("Which one?").Filterable(tc.filtering)
f := NewForm(NewGroup(field))
f.Update(f.Init())
// Filter for values starting with a 'B' only.
f.Update(keys('/'))
f.Update(keys('B'))

view := ansi.Strip(f.View())
// When we're filtering, the list should change.
if tc.filtering && strings.Contains(view, "Foo") {
t.Log(pretty.Render(view))
t.Error("Foo should not in filtered list.")
}
// When we're not filtering, the list shouldn't change.
if !tc.filtering && !strings.Contains(view, "Foo") {
t.Log(pretty.Render(view))
t.Error("Expected list to contain Foo.")
}
})
}
t.Run("Remove filter option from help menu.", func(t *testing.T) {
field := NewMultiSelect[string]().Options(NewOptions("Foo", "Bar", "Baz")...).Title("Which one?").Filterable(false)
f := NewForm(NewGroup(field))
f.Update(f.Init())
view := ansi.Strip(f.View())
if strings.Contains(view, "filter") {
t.Log(pretty.Render(view))
t.Error("Expected list to hide filtering in help menu.")
}
})
}

func TestSelectPageNavigation(t *testing.T) {
opts := NewOptions(
"Qux",
Expand Down
Loading