-
-
Notifications
You must be signed in to change notification settings - Fork 380
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
Can't clear HTML <input> value #447
Comments
Look like you are setting back the value by the previous content c.newMessageContent = e.Get("target").Get("value").String() |
@maxence-charriere Yeah sure, in the |
Just in case you're still looking into this; I've found a solution after a few hours of troubleshooting. I'll post it here; the gist of it is that there is a difference between the HTML |
Code repo with the implementation: https://github.com/pojntfx/liwasc-frontend-web The implementation (syncing the package components
import (
"github.com/maxence-charriere/go-app/v7/pkg/app"
)
type OnOffSwitchComponent struct {
app.Compo
On bool
OnToggle func(ctx app.Context, e app.Event)
ref app.HTMLInput
}
func (c *OnOffSwitchComponent) Render() app.UI {
c.Sync()
return c.ref
}
func (c *OnOffSwitchComponent) OnMount(ctx app.Context) {
c.Sync()
}
func (c *OnOffSwitchComponent) Sync() {
if c.ref == nil {
c.ref = app.Input().Type("checkbox").Checked(c.On).OnChange(c.OnToggle)
} else {
c.ref.JSValue().Set("checked", c.On)
}
} And a small example of the DOM Sync (unidirectional dataflow; there is no two-way bindings magic here) as a video: @maxence-charriere If you don't mind I'd add this to the documentation wiki ;) |
Just one more small issue: With this approach, I can't pass components as "props" anymore because they won't be updated. I tried to implement the behaviour like so: package components
import (
"fmt"
"log"
"github.com/maxence-charriere/go-app/v7/pkg/app"
)
type ExpandableSectionComponent struct {
app.Compo
Open bool
OnToggle func(ctx app.Context, e app.Event)
Title string
Content app.UI
}
func (c *ExpandableSectionComponent) Render() app.UI {
ref := app.Div().Class("pf-c-expandable-section__content").Hidden(!c.Open).Body(
c.Content,
)
app.Dispatch(func() {
if ref.JSValue() != nil {
log.Println("Setting hidden")
ref.JSValue().Set("hidden", !c.Open)
}
})
return app.Div().Class(fmt.Sprintf("pf-c-expandable-section pf-u-mb-md %v", func() string {
if c.Open {
return "pf-m-expanded"
}
return ""
}())).Body(
app.Button().Class("pf-c-expandable-section__toggle").Body(
app.Span().Class("pf-c-expandable-section__toggle-icon").Body(
app.I().Class("fas fa-angle-right"),
),
app.Span().Class("pf-c-expandable-section__toggle-text").Body(
app.Text(c.Title),
),
).OnClick(c.OnToggle),
ref,
)
} I would expect the code in |
Alright, one night later I actually got this to work. Even forked the repo to add a package components
import (
"fmt"
"github.com/maxence-charriere/go-app/v7/pkg/app"
)
type ExpandableSectionComponent struct {
app.Compo
Open bool
OnToggle func(ctx app.Context, e app.Event)
Title string
Content app.UI
}
func (c *ExpandableSectionComponent) Render() app.UI {
return app.Div().Class(fmt.Sprintf("pf-c-expandable-section pf-u-mb-md %v", func() string {
if c.Open {
return "pf-m-expanded"
}
return ""
}())).Body(
app.Button().Class("pf-c-expandable-section__toggle").Body(
app.Span().Class("pf-c-expandable-section__toggle-icon").Body(
app.I().Class("fas fa-angle-right"),
),
app.Span().Class("pf-c-expandable-section__toggle-text").Body(
app.Text(c.Title),
),
).OnClick(c.OnToggle),
&ExpandableSectionComponentContent{Content: c.Content, Open: c.Open},
)
}
type ExpandableSectionComponentContent struct {
app.Compo
Content app.UI
Open bool
}
func (c *ExpandableSectionComponentContent) Render() app.UI {
app.Dispatch(func() {
c.JSValue().Set("hidden", !c.Open)
})
return app.Div().Class("pf-c-expandable-section__content").Hidden(!c.Open).Body(
c.Content,
)
} Pretty simple actually. @maxence-charriere Would it be possible to add this to the Wiki, in an article like "Syncing DOM properties"? The wiki isn't editable directly but I could write the article and send it to you. Or maybe even adopt the |
Inm gonna takena look to make the wiki editable. |
Hi!
First of all, thanks for this library! I've had a lot of fun using it so far and I plan to use it for some serious frontends in the future. Sadly, I can't seem to be able to set the
value
of a HTML<input>
element, even when callingUpdate()
afterwards:I'd expect to the be able to clear the input in the
OnChange
handler, but nothing happens.newMessageContent
gets set, but the input still shows that last value. Any ideas?The text was updated successfully, but these errors were encountered: