Skip to content

Commit

Permalink
feat: select and multiselect accept options only from Options field
Browse files Browse the repository at this point in the history
  • Loading branch information
maaslalani committed Oct 30, 2023
1 parent 89348a2 commit 5e50a9d
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 25 deletions.
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
$(V).SILENT:
taco:
cd examples/taco && go run .

theme:
cd examples/theme && go run .

gh:
cd examples/gh && go run .
3 changes: 2 additions & 1 deletion examples/gh/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ func main() {
Title("Body"),
),
huh.NewGroup(
huh.NewSelect("Submit", "Submit as draft", "Continue in browser", "Add metadata", "Cancel").
huh.NewSelect[string]().
Options(huh.NewOptions("Submit", "Submit as draft", "Continue in browser", "Add metadata", "Cancel")...).
Title("What's next?").Value(&nextAction),
),
).WithTheme(theme)
Expand Down
6 changes: 4 additions & 2 deletions examples/taco/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ func main() {
// What's a taco without a shell?
// We'll need to know what filling to put inside too.
huh.NewGroup(
huh.NewSelect("Soft", "Hard").
huh.NewSelect[string]().
Options(huh.NewOptions("Soft", "Hard")...).
Title("Shell?").
Description("Our tortillas are made fresh in-house, every day.").
Validate(func(t string) error {
Expand All @@ -76,7 +77,8 @@ func main() {
}).
Value(&order.Taco.Shell),

huh.NewSelect("Chicken", "Beef", "Fish", "Beans").
huh.NewSelect[string]().
Options(huh.NewOptions("Chicken", "Beef", "Fish", "Beans")...).
Value(&order.Taco.Base).
Title("Base"),
),
Expand Down
4 changes: 2 additions & 2 deletions examples/theme/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ func main() {
huh.NewGroup(
huh.NewInput().Title("Thoughts").Placeholder("What's on your mind?"),
huh.NewText().Title("More Thoughts").Placeholder("What else is on your mind?"),
huh.NewSelect("A", "B", "C").Title("Colors"),
huh.NewMultiSelect("Red", "Green", "Yellow").Title("Letters"),
huh.NewSelect[string]().Options(huh.NewOptions("A", "B", "C")...).Title("Colors"),
huh.NewMultiSelect[string]().Options(huh.NewOptions("Red", "Green", "Yellow")...).Title("Letters"),
huh.NewConfirm().Title("Again?").Description("Try another theme").Value(&repeat),
),
).WithTheme(theme).Run()
Expand Down
9 changes: 1 addition & 8 deletions field_multiselect.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,10 @@ type MultiSelect[T any] struct {
}

// NewMultiSelect returns a new multi-select field.
func NewMultiSelect[T any](options ...T) *MultiSelect[T] {
var opts []Option[T]
for _, o := range options {
opts = append(opts, Option[T]{Key: fmt.Sprint(o), Value: o})
}

func NewMultiSelect[T any]() *MultiSelect[T] {
return &MultiSelect[T]{
value: new([]T),
options: opts,
validate: func([]T) error { return nil },
limit: len(opts),
}
}

Expand Down
17 changes: 5 additions & 12 deletions field_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,15 @@ type Select[T any] struct {
}

// NewSelect returns a new select field.
func NewSelect[T any](options ...T) *Select[T] {
var opts []Option[T]
for _, option := range options {
opts = append(opts, Option[T]{Key: fmt.Sprint(option), Value: option})
}

func NewSelect[T any]() *Select[T] {
filter := textinput.New()
filter.Prompt = "/"

return &Select[T]{
value: new(T),
options: opts,
filteredOptions: opts,
validate: func(T) error { return nil },
filtering: false,
filter: filter,
value: new(T),
validate: func(T) error { return nil },
filtering: false,
filter: filter,
}
}

Expand Down
11 changes: 11 additions & 0 deletions option.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
package huh

import "fmt"

// Option is an option for select fields.
type Option[T any] struct {
Key string
Value T
selected bool
}

// NewOptions returns new options from a list of values.
func NewOptions[T any](values ...T) []Option[T] {
var options []Option[T]
for _, o := range values {
options = append(options, Option[T]{Key: fmt.Sprint(o), Value: o})
}
return options
}

// NewOption returns a new select option.
func NewOption[T any](key string, value T) Option[T] {
return Option[T]{Key: key, Value: value}
Expand Down

0 comments on commit 5e50a9d

Please sign in to comment.