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

Form value binding messed up when using app.if #842

Closed
ystyle opened this issue Jul 10, 2023 · 6 comments
Closed

Form value binding messed up when using app.if #842

ystyle opened this issue Jul 10, 2023 · 6 comments

Comments

@ystyle
Copy link

ystyle commented Jul 10, 2023

type hello struct {
	app.Compo
	Switch bool
	name   string
	path   string
}

func (h *hello) OnClick(ctx app.Context, e app.Event) {
	fmt.Printf("switch: %v, name: %s, path: %s\n", h.Switch, h.name, h.path)
	h.Switch = !h.Switch
}

func (h *hello) Render() app.UI {
	return app.Div().Body(
		app.Button().Text("switch").OnClick(h.OnClick),
		app.If(h.Switch,
			app.Input().Type("text").Name("name").Placeholder("name").Value(h.name).OnChange(h.ValueTo(&h.name)),
		).Else(
			app.Input().Type("text").Name("path").Placeholder("path").Value(h.path).OnChange(h.ValueTo(&h.path)),
		),
	)
}

As in the above code, the actual binding values are all on the path
Peek 2023-07-10 11-53

@maxence-charriere
Copy link
Owner

Try unexport your Switch, here the variable is reset by the default value at each update.

@ystyle
Copy link
Author

ystyle commented Jul 10, 2023

still the same problem
Peek 2023-07-10 14-06

@oderwat
Copy link
Sponsor Contributor

oderwat commented Jul 10, 2023

See #632 ...I believe that because both conditional code paths are always executed, you always h.ValueTo(&h.path). I still think that app.If() should be deprecated.

@ystyle
Copy link
Author

ystyle commented Jul 11, 2023

So how can we switch between different form areas? Similar to pressing tabs to divide forms and form items to display and hide

@maxence-charriere
Copy link
Owner

Seem @oderwat is right. You could create 2 event handlers for each of your usecase without using ValueTo.

@oderwat
Copy link
Sponsor Contributor

oderwat commented Jul 11, 2023

Why not use a func(bool) app.UI that returns either one or the other version. This should work and avoid execution of code that is not needed.

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

3 participants