Skip to content

Commit 513e3b0

Browse files
Nick Eaketmiguelcobain
authored andcommitted
Prevent submitting a form when clicking clear in paper-autocomplete (#1089)
- update the paper-reset-button type to button to avoid the default of type="submit" when inside a form - add tests for paper-reset-button and paper-autocomplete components
1 parent 9037a0d commit 513e3b0

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

addon/components/paper-reset-button.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import TransitionMixin from 'ember-css-transitions/mixins/transition-mixin';
33

44
export default Component.extend(TransitionMixin, {
55
tagName: 'button',
6-
attributeBindings: ['tabindex'],
6+
type: 'button',
7+
attributeBindings: ['tabindex', 'type'],
78
transitionClass: 'ng',
89
onReset: null,
910

tests/integration/components/paper-autocomplete-test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,4 +291,30 @@ module('Integration | Component | paper-autocomplete', function(hooks) {
291291
assert.dom('md-input-container').hasClass('md-input-invalid');
292292
assert.dom('md-autocomplete .paper-input-error').hasText('validation error!');
293293
});
294+
295+
test('it does not submit a form when clear is clicked', async function(assert) {
296+
assert.expect(3);
297+
298+
this.set('selectedItem', '1');
299+
this.set('items', ['1', '2']);
300+
this.set('submitForm', () => {
301+
this.set('formSubmitted', true);
302+
});
303+
304+
await render(hbs`
305+
{{#paper-form onSubmit=(action submitForm) as |form|}}
306+
{{form.autocomplete
307+
allowClear=true
308+
options=items
309+
selected=selectedItem
310+
onSelectionChange=(action (mut selectedItem))
311+
}}
312+
{{/paper-form}}
313+
`);
314+
315+
assert.dom('form md-autocomplete button').hasAttribute('type', 'button', 'Clear has type="button"');
316+
await click('form md-autocomplete button');
317+
assert.notOk(this.get('selectedItem'), 'The selected item is cleared');
318+
assert.notOk(this.get('formSubmitted'), 'The outer form should not be submitted when the clear button is clicked');
319+
});
294320
});

tests/integration/components/paper-reset-button-test.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { module, test } from 'qunit';
22
import { setupRenderingTest } from 'ember-qunit';
3-
import { render } from '@ember/test-helpers';
3+
import { render, click } from '@ember/test-helpers';
44
import hbs from 'htmlbars-inline-precompile';
55

66
module('Integration | Component | paper reset button', function(hooks) {
@@ -23,4 +23,27 @@ module('Integration | Component | paper reset button', function(hooks) {
2323

2424
assert.dom(this.element).hasText('template block text');
2525
});
26+
27+
test('it does not submit a form on click', async function(assert) {
28+
assert.expect(3);
29+
30+
this.set('submitForm', () => {
31+
this.set('formSubmitted', true);
32+
});
33+
34+
this.set('onReset', () => {
35+
this.set('resetClicked', true);
36+
});
37+
38+
await render(hbs`
39+
<form {{action "submitForm" on="submit"}}>
40+
{{paper-reset-button class="reset-btn" onReset=(action onReset)}}
41+
</form>
42+
`);
43+
44+
assert.dom('form .reset-btn').hasAttribute('type', 'button', 'reset-button has type="button"');
45+
await click('form .reset-btn');
46+
assert.ok(this.get('resetClicked'), 'The reset button was clicked');
47+
assert.notOk(this.get('formSubmitted'), 'The outer form should not be submitted when the reset button is clicked');
48+
});
2649
});

0 commit comments

Comments
 (0)