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

submitting paper-form with enter key #460

Closed
jpaas opened this issue Jul 30, 2016 · 30 comments
Closed

submitting paper-form with enter key #460

jpaas opened this issue Jul 30, 2016 · 30 comments

Comments

@jpaas
Copy link

jpaas commented Jul 30, 2016

I just spent a lot of hours trying to make my paper-form component using 1.0.0.alpha.3 submit with the enter key and in the end I found the magical incantation, but I'm left wondering if maybe paper-form should make this easier, or at least document it.

Here is the only way I could make it work:

  {{#paper-form onSubmit="submit" as |form|}}
    <form {{action form.onSubmit on="submit"}}>
        {{paper-input label="Email" onChange=(action (mut identification)) value=identification}}
        {{paper-input label="Password" onChange=(action (mut password)) type="password" value=password}}
        {{#paper-button disabled=form.isInvalid type="submit"}}Signin{{/paper-button}}
    </form>
  {{/paper-form}}
@BennyAlex
Copy link

BennyAlex commented Sep 2, 2016

this causes that the action 'submit' will fired two times

Sorry I had the action still in the paper-button

@miguelcobain
Copy link
Collaborator

<form onsubmit={{action form.onSubmit}}>

should also work.

I'm open to suggestions on how this could be made easier.
paper-form is tagless, which makes it difficult to listen for key events. Maybe we should make it have a tag and listen for enter key presses?

@jpaas
Copy link
Author

jpaas commented Sep 2, 2016

It would be nice to have enter key submission "just work" by default. If that means generating a form tag, is there any downside?

@miguelcobain
Copy link
Collaborator

miguelcobain commented Sep 2, 2016

Possibly some style issues can arise. The markup won't be the same.

@jpaas
Copy link
Author

jpaas commented Sep 2, 2016

If you're unsure, then just maybe just add a section to the docs?

@miguelcobain
Copy link
Collaborator

I think the benefits outweigh the cons (if any). So, I could take a PR for this + tests. :)

@jpaas
Copy link
Author

jpaas commented Sep 6, 2016

@miguelcobain Sorry but I don't think I'll have time. I've moved onto a new project.

@miguelcobain
Copy link
Collaborator

@shoxter what is your opinion on this?

@shoxter
Copy link
Contributor

shoxter commented Oct 31, 2016

@miguelcobain I think it's a good idea.

What if we add an action to the form that looks for the enter key being pressed (the event is passed in) and set the input(s) the user wants to have access to it to having the submit action (i.e. form.submit)?

@miguelcobain
Copy link
Collaborator

miguelcobain commented Oct 31, 2016

@shoxter another idea would be to use tagName: 'form' and then use the onSubmit event.
Is there any harm that could come from this?

onSubmit triggers on enter keypresses and on button type="submit" submits. Also, it would be better for accessibility.

@shoxter
Copy link
Contributor

shoxter commented Oct 31, 2016

@miguelcobain The primary issue I foresee as being problematic is styling.

For example, in my personal application I wrap entire paper-dialogs in paper-form for validation purposes.

It's very flexible without a tag. This goes away when we add one.

Do you have any alternative methods off the top of your head? If not, I'll think on it and see what I can come up with.

@miguelcobain
Copy link
Collaborator

Would it be harmful for styles to have a form tag in your dialog? We should be doing it any way for semantics/acessibility.

@ibarrick
Copy link
Contributor

I think tagless is much more flexible, especially if the other use case can be achieved by just adding a form tag.

@panthony
Copy link
Contributor

Any progress on this subject ?

When I try the following:

{{#paper-form onSubmit=(action 'submit') as |form|}}
  <form class="layout-row" {{action form.onSubmit on='submit'}}>
      {{form.input placeholder='placeholder' value=value onChange=(action (mutvalue))}}
     {{#form.submit-button raised=true primary=true type='submit'}}Submit{{/form.submit-button}}
  </form>
{{/paper-form}}

Using the latest alpha release I get:

ember.debug.js:6628 DEPRECATION: You modified (hash isValid=isValid isInvalid=isInvalid input=(component "paper-input" parentComponent=(mut ) onValidityChange=(action "onValidityChange")) submit-button=(component "paper-button" onClick=(action "onSubmit")) select=(component "paper-select" parentComponent=(mut ) onValidityChange=(action "onValidityChange")) autocomplete=(component "paper-autocomplete" parentComponent=(mut ) onValidityChange=(action "onValidityChange")) onSubmit=(action "onSubmit")).onSubmit twice in a single render. This was unreliable in Ember 1.x and will be removed in Ember 3.0 [deprecation id: ember-views.render-double-modify]

I'm left wondering how to properly use {{paper-form}}.

@miguelcobain
Copy link
Collaborator

@panthony did you check the docs on paper-form? http://miguelcobain.github.io/ember-paper/release-1/#/forms
Let me know what isn't clear.

@panthony
Copy link
Contributor

I did check the docs but a form done this way cannot be submitted by pressing enter. Or at least it did not for me.

The warning comes from this for me:

  {{#paper-form onSubmit="submit" as |form|}}
    <form {{action form.onSubmit on="submit"}}>

But since the comment is rather old I was wondering if it was still the way to do it.

I ended up ignoring the 'onSubmit' attribute of the paper-form to avoid this deprecation warning.

{{#paper-form |form|}}
  <form class="layout-row" {{action (action 'submit') on='submit'}}>

I think the only thing i loose is the submit action that does not fire if an input is invalid.

@miguelcobain
Copy link
Collaborator

@panthony doesn't this work for you?

{{#paper-form onSubmit="submit" as |form|}}
  <form onsubmit={{action form.onSubmit}}>
    {{form.input label="Name" value=name onChange=(action (mut name)) required=true}}
    {{#form.submit-button raised=true primary=true}}
      Submit
    {{/form.submit-button}}
  </form>
{{/paper-form}}

@panthony
Copy link
Contributor

@miguelcobain No it does not work.

  • there is a deprecation warning about ember-views.render-double-modify with every user interaction with the input
  • the form won't submit when I press enter

I can fix the later by adding type="submit" on the submit-button but the deprecation issue remains.

@miguelcobain
Copy link
Collaborator

@panthony

I can fix the later by adding type="submit" on the submit-button but the deprecation issue remains.

Makes sense. We aren't setting type to "submit" in our yielded button. Can you please PR that type="submit" here: https://github.com/miguelcobain/ember-paper/blob/master/app/templates/components/paper-form.hbs#L9 ?

@panthony
Copy link
Contributor

@miguelcobain done with #568

@jamesdixon
Copy link

To add some additional information to this thread, when using:

<form onsubmit={{action form.onSubmit}}>

and submitting my form, the url that's submitted is something like https://localhost:4200/? (not the ?), which causes the action to be cancelled and I'm returned back to the same form.

However, using <form {{action form.onSubmit on="submit"}}> works as expected.

Also, the deprecation still remains regardless.

@miguelcobain
Copy link
Collaborator

@jamesdixon you probably need to preventDefault on the first approach. Can you try that?

@jamesdixon
Copy link

@miguelcobain ill give it a try. thank you!

@ghost
Copy link

ghost commented Dec 21, 2016

Working like a charm :) Thanks @jamesdixon

@BennyAlex
Copy link

BennyAlex commented Jan 2, 2017

@miguelcobain
given the following form

{{#paper-form onSubmit=(action "addNumber" number  preventDefault=false) as |form|}}
 <form onsubmit={{action form.onSubmit preventDefault=false}}>
  {{form.input label="Nummer" value=number onChange=(action (mut number)) customValidations=phoneValidation}}
  {{#form.submit-button raised=true primary=true disabled=(or form.isInvalid (not number)) onClick=(action "addNumber" number  preventDefault=false)}}
   submit
  {{/form.submit-button}}
 </form>
{{/paper-form}}

The page will be 'reloaded' when hit the enter button...

@jamesdixon @miguelcobain

using <form {{action form.onSubmit on="submit"}}> works with enter, but the submit action will be triggered twice...

@miguelcobain
Copy link
Collaborator

@BennyAlex Why are you preventDefault=false? The default submit action is to change URL and POST, etc. This needs to be prevented, so preventDefault=true should work.

using <form {{action form.onSubmit on="submit"}}> works with enter, but the submit action will be triggered twice...

This is maybe due to the preventDefault=false on the submit button action.

@BennyAlex
Copy link

@miguelcobain Of course I tried false and true, both doesnt work.

Also of reading this guide https://guides.emberjs.com/v2.10.0/templates/actions/, ember should prevent the default browser action by default...

@miguelcobain
Copy link
Collaborator

@BennyAlex

Also of reading this guide https://guides.emberjs.com/v2.10.0/templates/actions/, ember should prevent the default browser action by default...

as you can see in this twiddle, element space actions (like in that guide) are default prevented, but closure actions like onsubmit={{action are not:

https://ember-twiddle.com/c51d320ba02b121537dae467458fb6dd?openFiles=templates.application.hbs%2C

paper-form may need to be refactored so that this use case works out of the box :/

@BennyAlex
Copy link

BennyAlex commented Jan 3, 2017

@miguelcobain
@jamesdixon
Finally it work for me with <form onsubmit={{action form.onSubmit}}>, use enter button to submit , the action will not be triggered twice and you wont get redirected to another url (https://localhost:4200/?)

Here is my code:

//hbs
{{#paper-form onSubmit=(action "addMail" email) as |form|}}
<form onsubmit={{action form.onSubmit}}>
{{form.input label="E-Mail" value=email onChange=(action (mut email))
                             customValidations=emailValidation isTouched=true}}
{{#form.submit-button raised=true primary=true disabled=(or form.isInvalid (not email))
                                      onClick=(action "addMail" email)}}
submit
 {{/form.submit-button}}
</form>
{{/paper-form}}
//js
actions: {
  addMail(mail, event){
      event.preventDefault();
      console.log('add mail');
     //do something with the mail
    },
}

@panthony
Copy link
Contributor

This issue seems resolved, maybe this ticket can be closed?

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

7 participants