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

How can I dynamically generate options in a list? #183

Closed
iloveicedgreentea opened this issue Apr 9, 2024 · 4 comments
Closed

How can I dynamically generate options in a list? #183

iloveicedgreentea opened this issue Apr 9, 2024 · 4 comments

Comments

@iloveicedgreentea
Copy link

iloveicedgreentea commented Apr 9, 2024

I have code like this

huh.NewGroup(
	huh.NewSelect[string]().
		Key(keyAppNames).
		// add the app names here
		Options(huh.NewOptions(m.appState.AppNames...)...).
		Title("Applications for "+m.appState.UserName).
		Description("Select your app. You may scroll or filter"),
),

m.appState.AppNames is fetched from some API. If I fetch it async after the form is completed, I have to remake the form to get it to show up. Calling form.Update() does not seem to refresh it. What is the correct way to update values in a form after the form is created?

In other words, lets say I have 4 groups. The first 3 ask the user for inputs. The fourth's list is based on the API response which depends on the items in the first 3 groups. Right now, I would have to remake the form and then call NextGroup to get to the page I was on, or make a second form and switch to that, but then I lose shift+tab functionality.

What I would like to happen is this:

  1. Form is generated
  2. Update() function receives some tea.Msg
  3. form.Update() remakes the fields because the underlying variable changed (m.appState.AppNames)
  4. the form is updated

Some alternatives I considered

  1. making the form again and using NextGroup to "scroll" back to the right place. Kind of hacky and loses the form variables that I will have to save externally and also requires me to keep track of positions. Having the page field external would be nice so I can specify which group to switch to

  2. Making a second form and switching model.form to it. This is simpler but loses the shift-tab functionality

@WTIGER001
Copy link

WTIGER001 commented Apr 10, 2024

I did your option 2... I have a lot of dynamic steps. I wish there was a JIT factory approach. Seems similar to #69.

@iloveicedgreentea
Copy link
Author

For now I ended up refactoring to go back with a single form and a helper function to scroll like this

func (m *Model) scrollGroupUp(scrollAmount int) {
	for i := 0; i < scrollAmount; i++ {
		m.form.NextGroup()
	}
}

It would be nice to have direct access to the selected group, as well as exporting much more fields from the Form struct

@meowgorithm
Copy link
Member

Hi! We're planning on supporting this is an official capacity. Please follow #69 for updates and discussion.

@maaslalani
Copy link
Contributor

Hi, closing this in favour of #69

@maaslalani maaslalani closed this as not planned Won't fix, can't repro, duplicate, stale May 25, 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

No branches or pull requests

4 participants