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(dynamic): enable form options with any generic type #312

Closed
wants to merge 1 commit into from

Conversation

ChrisRx
Copy link

@ChrisRx ChrisRx commented Jul 13, 2024

Hi! PR #233 introduced dynamic options to be used in forms and initially I was unable to get the following example working:

	var workspaceId, folderId int
	form := huh.NewForm(
		huh.NewGroup(
			huh.NewSelect[int]().
				Title("Choose your workspace").
				Height(15).
				Options(options...). // initial options constructed from API call
				Value(&workspaceId),
			huh.NewSelect[int]().
				Value(&folderId).
				Title("Choose your folder").
				Height(5).
				OptionsFunc(func() (options []huh.Option[int]) {
					if workspaceId == 0 {
						return
					}
					// API call using workspace ID to fetch list of folder ids
					return
				}, &workspaceId),
			huh.NewConfirm().
				Title("Use selected?").
				Value(&proceed),
		),
	)

but I was able to get the examples included in the PR working. Digging further I was able to get my example working using huh.Option[string] and the issue ended up being this particular type switch not matching the generic type huh.Option[int]:

huh/group.go

Lines 251 to 261 in ccca06d

switch msg := msg.(type) {
case spinner.TickMsg,
updateTitleMsg,
updateDescriptionMsg,
updateSuggestionsMsg,
updateOptionsMsg[string],
updatePlaceholderMsg:
m, cmd := g.fields[i].Update(msg)
g.fields[i] = m.(Field)
cmds = append(cmds, cmd)
}

I tried bumping the project to Go 1.20 and adding UpdateOptionsMsg[any] to the switch case, but that didn't work, so instead I made the isUpdateOptionsMsg interface and embedded it in UpdateOptionsMsg. I'm unsure if this is the best approach but it does seem to work and I couldn't find in my searching if there is a more intended way to solve this in Go.

@ChrisRx ChrisRx requested a review from maaslalani as a code owner July 13, 2024 12:36
@maaslalani
Copy link
Contributor

Hey @ChrisRx, thanks so much for this PR. I've fixed this issue in #318. Let me know if this works for you!

@maaslalani maaslalani closed this Jul 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants