huh icon indicating copy to clipboard operation
huh copied to clipboard

Select doesn't render the elements before the selected elements if Value of it is set

Open UtkarshM-hub opened this issue 7 months ago • 4 comments

Describe the bug

  • Setting Value() of the select doesn't render the elements which are present before the selected element
  • Though it does show them once we reach the end of it

Setup

Please complete the following information along with version numbers, if applicable.

  • OS: macOS sonoma Version 14.3.1
  • Shell: bash
  • Terminal Emulator: Warp

To Reproduce

Steps to reproduce the behavior:

  1. Create a select
  2. Set value of it using Value() function
  3. If the selected element is not the first on the list, it doesn't render it's previous elements

Source Code

networkStruct.form = huh.NewForm(
	huh.NewGroup(
		huh.NewSelect[string]().
			Key("IP METHOD").
			Title("IPv4 Method").
			DescriptionFunc(func() string {
				if networkStruct.networkInterfaceDetails.IPv4Method != "Manual" {
					return "Press 'tab' to Change Method\nNote: Switch to Manual Method In Order To Configure The Fields Below"
				}
				return "Press 'tab' to Change Method\n"
			}, &networkStruct.networkInterfaceDetails.IPv4Method).
			Value(&networkStruct.networkInterfaceDetails.IPv4Method).
			Options(
				huh.Option[string]{
					Key:   "Automatic",
					Value: "Automatic",
				},
				huh.Option[string]{
					Key:   "Manual",
					Value: "Manual",
				},
				huh.Option[string]{
					Key:   "Disabled",
					Value: "Disabled",
				},
			),
	),
).WithKeyMap(&huh.KeyMap{
	Select: huh.SelectKeyMap{
		Down: key.NewBinding(key.WithKeys("tab")),
		Prev: key.NewBinding(key.WithKeys("up")),
		Next: key.NewBinding(key.WithKeys("down")),
	},
})

}

Expected behavior

It should render all the values instead of just rendering from the selected one

Screenshots

The value is set to Manual in the example below

https://github.com/user-attachments/assets/9acb2130-5d6e-42eb-8bd7-cb7ba583a8b0

UtkarshM-hub avatar Aug 01 '25 05:08 UtkarshM-hub

I'm facing the same issue with MultiSelect too. It "hides" all the options before the first default selected option, until I press the up arrow key. Setting the height or limit doesn't help at all

silicoflare avatar Sep 21 '25 06:09 silicoflare

I've got a fix for this mentioned in this discussion feel free to take a look and let me know

MatthewWaring avatar Sep 22 '25 20:09 MatthewWaring

I've got a fix for this mentioned in this discussion feel free to take a look and let me know

Yes, this works like a charm! Thanks a lot!

silicoflare avatar Sep 23 '25 01:09 silicoflare

I find that quite confusing as well, as there is no visual indication that there is more options if you scroll up. Even as I was writing my own app, it took me some time to figure it out.

Imho, auto-scrolling to the first selected value can make sense from a UX perspective, but it should be paired with either (ideally both):

  • having a way to force the scrolling position
  • having a visual clue about the scrolling (for example a gutter with and )

MichaelMure avatar Oct 18 '25 09:10 MichaelMure

any plan on opening a pr and merging to main branch?

LeeTeng2001 avatar Dec 04 '25 09:12 LeeTeng2001