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

Makes paper-form an actual html form #649

Merged
merged 5 commits into from
Feb 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion addon/components/paper-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,17 @@ const { Component, computed } = Ember;
*/
export default Component.extend(ParentMixin, {
layout,
tagName: '',
tagName: 'form',
isValid: computed.not('isInvalid'),
isInvalid: computed('[email protected]', function() {
return this.get('childComponents').isAny('isInvalid');
}),

submit() {
this.send('onSubmit');
return false;
},

actions: {
onValidityChange() {
if (this.get('lastIsValid') !== this.get('isValid')) {
Expand Down
2 changes: 0 additions & 2 deletions addon/templates/components/paper-form.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
onValidityChange=(action "onValidityChange")
)
submit-button=(component "paper-button"
onClick=(action "onSubmit")
type="submit"
)
select=(component "paper-select"
Expand All @@ -19,4 +18,3 @@
)
onSubmit=(action "onSubmit")
)}}

6 changes: 4 additions & 2 deletions tests/dummy/app/templates/forms.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
{{#paper-card-content}}
<p>
ember-paper provides a <code>paper-form</code> to help you build forms
and keep track of the form's global validity state. This component is tagless,
so it shouldn't interfere with your styles.
and keep track of the form's global validity state. This component uses
the html form tag by default, so expected form behavior will occur.
(For example, pressing the enter key from within one of the form's
inputs will submit the form.)
<code>paper-form</code> yields <code>{{#link-to "demo.input"}}paper-input{{/link-to}}</code>,
<code>{{#link-to "demo.select"}}paper-select{{/link-to}}</code>
and <code>{{#link-to "demo.autocomplete"}}paper-autocomplete{{/link-to}}</code> controls.
Expand Down
24 changes: 22 additions & 2 deletions tests/integration/components/paper-form-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ test('form `onSubmit` action is invoked', function(assert) {
{{form.input value=foo onChange=(action (mut foo)) label="Foo"}}
{{form.input value=bar onChange=(action (mut bar)) label="Bar"}}

<button onclick={{action form.onSubmit}}>Submit</button>
<a {{action form.onSubmit}}>Submit</a>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to change this to an <a?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@miguelcobain It needs to be something besides button, because html buttons submit forms automatically. When it's a button here, the onSubmit action is called twice. Once because the action is assigned directly to the button, and once because of the form's submit handler.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tyleryasaka That's what happens by default, but I changed it to <button type="button".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ok - I didn't realize that could be done. 👍


{{/paper-form}}
`);

this.$('button').click();
this.$('a').click();
});

test('form `onValidityChange` action is invoked', function(assert) {
Expand Down Expand Up @@ -182,3 +182,23 @@ test('form submit button is of type submit', function(assert) {

assert.equal(this.$('button').attr('type'), 'submit');
});

test('form `onSubmit` action is invoked when form element is submitted', function(assert) {
assert.expect(1);

this.set('onSubmit', () => {
assert.ok(true);
});

this.render(hbs`
{{#paper-form onSubmit=(action onSubmit) as |form|}}
{{form.input value=foo onChange=(action (mut foo)) label="Foo"}}
{{form.input value=bar onChange=(action (mut bar)) label="Bar"}}

<input type="submit" value="Submit">

{{/paper-form}}
`);

this.$('input').click();
});