Skip to content

Commit

Permalink
feat(Modal): dropped autoClose property. Preventing automatically c…
Browse files Browse the repository at this point in the history
…losing the modal can now be done by returning false from `onHide`
  • Loading branch information
simonihmig committed Nov 20, 2016
1 parent 05a9606 commit 1bffd84
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 21 deletions.
2 changes: 1 addition & 1 deletion addon/components/bs-modal-simple.js
Expand Up @@ -13,7 +13,7 @@ import layout from '../templates/components/bs-modal-simple';
```
This will automatically create the appropriate markup, with a modal header containing the title, and a footer containing
a default "Ok" button, that will close the modal automatically (unless you set `autoClose` to false).
a default "Ok" button, that will close the modal automatically (unless you return false from `onHide`).
A modal created this way will be visible at once. You can use the `{{#if ...}}` helper to hide all modal elements form
the DOM until needed. Or you can bind the `open` property to trigger showing and hiding the modal:
Expand Down
21 changes: 5 additions & 16 deletions addon/components/bs-modal.js
Expand Up @@ -33,7 +33,7 @@ Modal.BACKDROP_TRANSITION_DURATION = 150;
Furthermore references to the following actions are yielded:
* `close`: triggers the `onHide` action and closes the modal (if `autoClose` is true)
* `close`: triggers the `onHide` action and closes the modal
* `submit`: triggers the `onSubmit` action (or the submit event on a form if present in the body element)
### Further reading
Expand All @@ -50,6 +50,8 @@ export default Ember.Component.extend({

/**
* Visibility of the modal. Toggle to to show/hide with CSS transitions.
* *Note*: this property will be automatically set to false when clicking the modal's close button, unless you return
* false from the `onHide` action. Beware when using two-way bindings!
*
* @property open
* @type boolean
Expand Down Expand Up @@ -113,18 +115,6 @@ export default Ember.Component.extend({
*/
keyboard: true,

/**
* If true clicking a close button will hide the modal automatically.
* If you want to handle hiding the modal by yourself, you can set this to false and use the closeAction to
* implement your custom logic.
*
* @property autoClose
* @type boolean
* @default true
* @public
*/
autoClose: true,

/**
* The id of the `.modal` element.
*
Expand Down Expand Up @@ -243,7 +233,7 @@ export default Ember.Component.extend({
* Note that this will happen before the modal is hidden from the DOM, as the fade transitions will still need some
* time to finish. Use the `closedAction` if you need the modal to be hidden when the action triggers.
*
* You can set `autoClose` to false to prevent closing the modal automatically, and do that in your closeAction by
* You can return false to prevent closing the modal automatically, and do that in your action by
* setting `open` to false.
*
* @property onHide
Expand Down Expand Up @@ -286,10 +276,9 @@ export default Ember.Component.extend({

actions: {
close() {
if (this.get('autoClose')) {
if (this.get('onHide')() !== false) {
this.set('open', false);
}
this.get('onHide')();
},
submit() {
let form = this.get('modalElement').find('.modal-body form');
Expand Down
12 changes: 8 additions & 4 deletions tests/integration/components/bs-modal-simple-test.js
Expand Up @@ -111,7 +111,7 @@ test('backdrop=false removes backdrop element', function(assert) {
}, transitionTimeout);
});

test('clicking close button closes modal when autoClose=true', function(assert) {
test('clicking close button closes modal', function(assert) {
this.render(hbs`{{#bs-modal-simple title="Simple Dialog"}}Hello world!{{/bs-modal-simple}}`);
let done = assert.async();

Expand All @@ -128,7 +128,7 @@ test('clicking close button closes modal when autoClose=true', function(assert)
}, transitionTimeout);
});

test('clicking ok button closes modal when autoClose=true', function(assert) {
test('clicking ok button closes modal', function(assert) {
this.render(hbs`{{#bs-modal-simple title="Simple Dialog"}}Hello world!{{/bs-modal-simple}}`);
let done = assert.async();

Expand All @@ -145,8 +145,12 @@ test('clicking ok button closes modal when autoClose=true', function(assert) {
}, transitionTimeout);
});

test('clicking close button leaves modal open when autoClose=false', function(assert) {
this.render(hbs`{{#bs-modal-simple title="Simple Dialog" autoClose=false}}Hello world!{{/bs-modal-simple}}`);
test('clicking close button leaves modal open when onHide action returns false', function(assert) {
let hideAction = this.stub();
hideAction.returns(false);
this.on('hide', hideAction);

this.render(hbs`{{#bs-modal-simple title="Simple Dialog" onHide=(action "hide")}}Hello world!{{/bs-modal-simple}}`);
let done = assert.async();

// wait for fade animation
Expand Down

0 comments on commit 1bffd84

Please sign in to comment.