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

@keydown doesn't work with vgform:input #171

Open
bmheenan opened this issue Nov 27, 2020 · 5 comments
Open

@keydown doesn't work with vgform:input #171

bmheenan opened this issue Nov 27, 2020 · 5 comments
Labels
enhancement New feature or request tbd Need to decide if this will be in v1 or not

Comments

@bmheenan
Copy link

bmheenan commented Nov 27, 2020

Describe the bug
I'm trying to create a small form where the user enters values into different fields, and can hit enter while focusing on any of them and have that data submit. It seems the way to do this would be something like:

  • use vgform: components to bind their values to fields of the component and make them easy to access and easy to clear on submit
  • add @keydown to point to a function that checks to see if the key was enter, and if so, submit. (Other options exist here, but they all take the form of using an @event on the vgform: component.

But this doesn't compile: vgcomp.keydown undefined (type *vgform.Input has no field or method keydown)

Software Versions
Vugu version: 0.3.3
Go version: 1.15.2
Browser and version: Testing on Chrome 86, though don't think it's applicable here

To Reproduce

<vgform:Input
    class="name"
    type="text"
    :Value='vgform.StringPtr{&c.Name}'
    @keydown='c.Keydown(event)'></vgform:Input>
func (c *Component) Keydown(e vugu.DOMEvent) {	
    if e.PropString("key") == "Enter" {
        // get all the bound values and submit to server, then clear them
    }
}

Expected behavior
This should compile. Keydown(e) should effectively do nothing until the enter key is pressed. When the user enters text into "name", c.Name should be updated to reflect it. When the user presses enter, the code at the comment is executed.

Thanks!

@bmheenan bmheenan changed the title @keydown doesn't work with vgform:select @keydown doesn't work with vgform:input Nov 27, 2020
@bmheenan
Copy link
Author

I should also point out that it's possible to use @keydown in the above code if you substitute out the <vgform:Input> for a regular old <input>. But if you do that, the values of the inputs aren't bound to anything easily accessible when you want to submit to the server

@bmheenan
Copy link
Author

bmheenan commented Nov 28, 2020

In case anyone else comes across this, the workaround that worked for me was to use github.com/vugu/vugu/js to read the current values from the input fields I needed:

import "github.com/vugu/vugu/js"

func (c *Component) getField(id string) string {
    jsDoc := js.Global().Get("document")
    if !jsDoc.Truthy() {
        return ""
    }
    f := jsDoc.Call("getElementById", id)
    if !f.Truthy() {
        return ""
    }
    return f.Get("value").String()
}

@bradleypeabody
Copy link
Contributor

bradleypeabody commented Nov 28, 2020

Yeah, this has to do with the fact that @eventName= means different things for components vs regular HTML elements. The design of the vgform package is still in flux and I'm not sure yet whether it makes sense to, and if so how, add support for all of the possible events to vgform:Input.

For now, your workaround seems okay or you might want to consider just using a regular <input tag - to which you can attach whatever events you need and capturing the change to the value is as simple as setting up a handler for @change=. As you can see from here https://github.com/vugu/vugu/blob/master/vgform/input.go#L19 the implementation is trivial.

I plan to do some more experimentation with forms soon and I'm sure better patterns will emerge from this.

@bmheenan
Copy link
Author

Yup, I'm using the workaround above with a regular <input> tag. It's working without issue

@owenwaller owenwaller added enhancement New feature or request tbd Need to decide if this will be in v1 or not labels Nov 2, 2023
@owenwaller
Copy link
Collaborator

See #250

We need to make a decision on what if anything needs to change in the vgforms package for a v1.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request tbd Need to decide if this will be in v1 or not
Projects
None yet
Development

No branches or pull requests

3 participants