Skip to content

PoC field level hide funcs#597

Draft
miniscruff wants to merge 1 commit intocharmbracelet:mainfrom
miniscruff:dynamic-group-form
Draft

PoC field level hide funcs#597
miniscruff wants to merge 1 commit intocharmbracelet:mainfrom
miniscruff:dynamic-group-form

Conversation

@miniscruff
Copy link
Copy Markdown

@miniscruff miniscruff commented Mar 20, 2025

I am working on moving over to huh for prompts in changie. My goal is to create a single form with all your options but based on what kind of change and now that kind is configured you may have additional custom values as well as skipping some built in ones. The exact details here are maybe not that important but here is my PR on that side of things miniscruff/changie#685 .

The important parts are like so:

fields := make([]huh.Field, 0)

if len(p.Config.Kinds) > 0 && len(p.Kind) == 0 {
	kindOptions := make([]huh.Option[string], len(p.Config.Kinds))
	for i, kindConfig := range p.Config.Kinds {
		kindOptions[i] = huh.NewOption(kindConfig.Label, kindConfig.Key)
	}

	fields = append(fields, huh.NewSelect[string]().
		Title("Kind").
		Options(kindOptions...).
		// validation skipped
		Value(&p.Kind),
	)
}

fields = append(fields, huh.NewText().
	Title("Body").
	WithHideFunc(func() bool {
		kc := p.Config.KindFromKeyOrLabel(p.Kind)
		if kc == nil {
			return false
		}
		return kc.SkipBody
	}, &p.Kind).
	Value(&p.Body),
)

// see below

form := huh.NewForm(huh.NewGroup(fields...))

err = form.Run()
if err != nil {
	return nil, err
}

This works just like dynamic groups do now so you might be wondering what is the benefit to dynamic fields? Well, dynamic fields provide the same benefit but within a single form, so previous form responses remain and you can preview what new or different fields may be presented based on your current inputs.

In the gif below ( created with vhs 😏 ) as you go up and down the "kinds" input, the next choices show and hide dynamically. The exact rules are kinda changie specific but all configured through the changie config file. If this was done via groups you would have to select a kind, it would clear the screen, hiding your previous inputs, as you continue on through the form.

hide

Related to #582

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.

1 participant