Skip to content

Commit

Permalink
feat: Filtering on Select and MultiSelect fields
Browse files Browse the repository at this point in the history
  • Loading branch information
maaslalani committed Jun 18, 2024
1 parent 7923781 commit f255579
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 8 deletions.
1 change: 0 additions & 1 deletion examples/readme/confirm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ func main() {
Description("Please confirm. ").
Affirmative("Yes!").
Negative("No.").
Inline(true).
Value(&happy)

huh.NewForm(huh.NewGroup(confirm)).Run()
Expand Down
1 change: 1 addition & 0 deletions examples/readme/multiselect/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import "github.com/charmbracelet/huh"
func main() {
var toppings []string
s := huh.NewMultiSelect[string]().
Filtering(true).
Options(
huh.NewOption("Lettuce", "Lettuce").Selected(true),
huh.NewOption("Tomatoes", "Tomatoes").Selected(true),
Expand Down
1 change: 1 addition & 0 deletions examples/readme/select/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ func main() {
var country string
s := huh.NewSelect[string]().
Title("Pick a country.").
Filtering(true).
Options(
huh.NewOption("United States", "US"),
huh.NewOption("Germany", "DE"),
Expand Down
15 changes: 12 additions & 3 deletions field_multiselect.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,13 @@ func (m *MultiSelect[T]) Filterable(filterable bool) *MultiSelect[T] {
return m
}

// Filtering sets the filtering state of the multi-select field.
func (m *MultiSelect[T]) Filtering(filtering bool) *MultiSelect[T] {
m.filtering = filtering
m.filter.Focus()
return m
}

// Limit sets the limit of the multi-select field.
func (m *MultiSelect[T]) Limit(limit int) *MultiSelect[T] {
m.limit = limit
Expand Down Expand Up @@ -513,9 +520,11 @@ func (m *MultiSelect[T]) WithTheme(theme *Theme) Field {
return m
}
m.theme = theme
m.filter.Cursor.Style = m.theme.Focused.TextInput.Cursor
m.filter.Cursor.TextStyle = m.theme.Focused.TextInput.CursorText
m.filter.PromptStyle = m.theme.Focused.TextInput.Prompt
m.filter.Cursor.Style = theme.Focused.TextInput.Cursor
m.filter.Cursor.TextStyle = theme.Focused.TextInput.CursorText
m.filter.PromptStyle = theme.Focused.TextInput.Prompt
m.filter.TextStyle = theme.Focused.TextInput.Text
m.filter.PlaceholderStyle = theme.Focused.TextInput.Placeholder
m.updateViewportHeight()
return m
}
Expand Down
10 changes: 6 additions & 4 deletions field_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ func (s *Select[T]) titleView() string {
sb = strings.Builder{}
)
if s.filtering {
sb.WriteString(styles.Title.Render(s.filter.View()))
sb.WriteString(s.filter.View())
} else if s.filter.Value() != "" && !s.inline {
sb.WriteString(styles.Title.Render(s.title) + styles.Description.Render("/"+s.filter.Value()))
} else {
Expand Down Expand Up @@ -496,9 +496,11 @@ func (s *Select[T]) WithTheme(theme *Theme) Field {
return s
}
s.theme = theme
s.filter.Cursor.Style = s.theme.Focused.TextInput.Cursor
s.filter.Cursor.TextStyle = s.theme.Focused.TextInput.CursorText
s.filter.PromptStyle = s.theme.Focused.TextInput.Prompt
s.filter.Cursor.Style = theme.Focused.TextInput.Cursor
s.filter.Cursor.TextStyle = theme.Focused.TextInput.CursorText
s.filter.PromptStyle = theme.Focused.TextInput.Prompt
s.filter.TextStyle = theme.Focused.TextInput.Text
s.filter.PlaceholderStyle = theme.Focused.TextInput.Placeholder
s.updateViewportHeight()
return s
}
Expand Down

0 comments on commit f255579

Please sign in to comment.