Skip to content

Commit

Permalink
fix(Modal): Trigger submit on all forms in modal body. Thanks to @moxide
Browse files Browse the repository at this point in the history


Fixes #362
  • Loading branch information
fpalluel authored and simonihmig committed Jun 18, 2017
1 parent ffee13c commit b2b32b8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
10 changes: 6 additions & 4 deletions addon/components/base/bs-modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -337,12 +337,14 @@ export default Ember.Component.extend(TransitionSupport, {
}
},
submit() {
let form = this.get('modalElement').querySelector('.modal-body form');
if (form) {
// trigger submit event on body form
let forms = this.get('modalElement').querySelectorAll('.modal-body form');
if (forms.length > 0) {
// trigger submit event on body forms
let event = document.createEvent('Events');
event.initEvent('submit', true, true);
form.dispatchEvent(event);
forms.forEach((form) => {
form.dispatchEvent(event);
});
} else {
// if we have no form, we send a submit action
this.get('onSubmit')();
Expand Down
16 changes: 15 additions & 1 deletion tests/integration/components/bs-modal-simple-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,20 @@ test('when modal has a form and the submit button is clicked, the form is submit
assert.notOk(modalSpy.called);
});

test('when modal has several forms and the submit button is clicked, those forms are submitted', async function(assert) {
let modalSpy = this.spy();
let formOneSpy = this.spy();
let formTwoSpy = this.spy();
this.on('modalSubmit', modalSpy);
this.on('formOneSubmit', formOneSpy);
this.on('formTwoSubmit', formTwoSpy);
this.render(hbs`{{#bs-modal-simple title="Simple Dialog" closeTitle="Cancel" submitTitle="Ok" onSubmit=(action "modalSubmit") as |modal|}}{{#bs-form onSubmit=(action "formOneSubmit")}}{{/bs-form}}{{#bs-form onSubmit=(action "formTwoSubmit")}}{{/bs-form}}{{/bs-modal-simple}}`);
await click('.modal .modal-footer button.btn-primary');
assert.ok(formOneSpy.calledOnce);
assert.ok(formTwoSpy.calledOnce);
assert.notOk(modalSpy.called);
});

test('autofocus element is focused when present and fade=false', function(assert) {
let action = this.spy();
this.on('focusAction', action);
Expand Down Expand Up @@ -311,7 +325,7 @@ test('Pressing escape key will close the modal if keyboard=true and element is a
{{#bs-modal-simple title="Simple Dialog" onHide=(action "testAction") keyboard=true}}
<input autofocus="autofocus"/>
{{/bs-modal-simple}}
`);

// wait for fade animation
Expand Down

0 comments on commit b2b32b8

Please sign in to comment.