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

fix: prefill select and multiselect with value #62

Merged
merged 2 commits into from
Dec 18, 2023
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
21 changes: 19 additions & 2 deletions field_multiselect.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

// MultiSelect is a form multi-select field.
type MultiSelect[T any] struct {
type MultiSelect[T comparable] struct {
value *[]T
key string

Expand All @@ -38,7 +38,7 @@ type MultiSelect[T any] struct {
}

// NewMultiSelect returns a new multi-select field.
func NewMultiSelect[T any]() *MultiSelect[T] {
func NewMultiSelect[T comparable]() *MultiSelect[T] {
return &MultiSelect[T]{
options: []Option[T]{},
value: new([]T),
Expand All @@ -49,6 +49,14 @@ func NewMultiSelect[T any]() *MultiSelect[T] {
// Value sets the value of the multi-select field.
func (m *MultiSelect[T]) Value(value *[]T) *MultiSelect[T] {
m.value = value
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can nil be passed to Value? I think we should have a nil check here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, all the fields accept nil values and panic on dereferencing. Would it be more appropriate to fix all of them in a new PR instead?

for i, o := range m.options {
for _, v := range *value {
if o.Value == v {
m.options[i].selected = true
break
}
}
}
return m
}

Expand Down Expand Up @@ -76,6 +84,15 @@ func (m *MultiSelect[T]) Options(options ...Option[T]) *MultiSelect[T] {
if len(options) <= 0 {
return m
}

for i, o := range options {
for _, v := range *m.value {
if o.Value == v {
options[i].selected = true
break
}
}
}
m.options = options
return m
}
Expand Down
17 changes: 13 additions & 4 deletions field_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

// Select is a form select field.
type Select[T any] struct {
type Select[T comparable] struct {
value *T
key string

Expand Down Expand Up @@ -40,7 +40,7 @@ type Select[T any] struct {
}

// NewSelect returns a new select field.
func NewSelect[T any]() *Select[T] {
func NewSelect[T comparable]() *Select[T] {
filter := textinput.New()
filter.Prompt = "/"

Expand All @@ -56,6 +56,12 @@ func NewSelect[T any]() *Select[T] {
// Value sets the value of the select field.
func (s *Select[T]) Value(value *T) *Select[T] {
s.value = value
for i, o := range s.options {
if o.Value == *value {
s.selected = i
break
}
}
return s
}

Expand Down Expand Up @@ -86,9 +92,12 @@ func (s *Select[T]) Options(options ...Option[T]) *Select[T] {
s.options = options
s.filteredOptions = options

// Set the cursor to the last selected option.
// Set the cursor to the existing value or the last selected option.
for i, option := range options {
if option.selected {
if option.Value == *s.value {
s.selected = i
break
} else if option.selected {
s.selected = i
}
}
Expand Down
6 changes: 3 additions & 3 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package huh
import "fmt"

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

// NewOptions returns new options from a list of values.
func NewOptions[T any](values ...T) []Option[T] {
func NewOptions[T comparable](values ...T) []Option[T] {
options := make([]Option[T], len(values))
for i, o := range values {
options[i] = Option[T]{
Expand All @@ -22,7 +22,7 @@ func NewOptions[T any](values ...T) []Option[T] {
}

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

Expand Down
Loading