Skip to content

Default value doesn't work for NewSelect #444

Open
@puyt

Description

Describe the bug

The default value isn't selected, instead the 1st available option is selected. Seems this is only happening when using the OptionsFunc instead of Options.

To Reproduce

The following code example works:

package main

import "github.com/charmbracelet/huh"

func main() {

	foo := "two"
	form := huh.NewForm(
		huh.NewGroup(
			huh.NewSelect[string]().
				Title("foo").
				Options(huh.NewOptions("one", "two", "three")...).
				Value(&foo),
		),
	)

	form.Run()
}

The following example uses OptionsFunc and the default value is not working:

package main

import "github.com/charmbracelet/huh"

func main() {

	foo := "two"
	form := huh.NewForm(
		huh.NewGroup(
			huh.NewSelect[string]().
				Title("foo").
				OptionsFunc(func() []huh.Option[string] {
				 	return getOptions()
				}, nil).
				Value(&foo),
		),
	)

	form.Run()
}

func getOptions() []huh.Option[string] {
	var options []huh.Option[string]

	options = append(options, huh.NewOption("one", "one"))
	options = append(options, huh.NewOption("two", "two"))
	options = append(options, huh.NewOption("three", "three"))

	return options
}

Expected behavior

Expect two to be selected by default in both cases.

Related issue: #70

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingselect

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions