Skip to content

Commit

Permalink
fix(modal): fix modal event handling and add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonas Metzener authored and anehx committed Nov 18, 2019
1 parent 9ca6904 commit 916b68b
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 37 deletions.
70 changes: 42 additions & 28 deletions addon/components/uk-modal.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Component from "@ember/component";
import { computed } from "@ember/object";
import layout from "../templates/components/uk-modal";
import UIkit from "uikit";
import { scheduleOnce } from "@ember/runloop";
Expand All @@ -23,56 +24,69 @@ export default Component.extend({
".uk-modal-close-full"
].join(", "),

init() {
this._super(...arguments);
modalId: computed("elementId", function() {
return `modal-${this.elementId}`;
}),

const { APP } = getOwner(this).resolveRegistration("config:environment");
containerElement: computed("container", function() {
return getOwner(this)
.lookup("service:-document")
.querySelector(this.container);
}),

this.set("container", document.querySelector(APP.rootElement || "body"));
},
init() {
this._super(...arguments);

_show() {
this.getWithDefault("on-show", noop)();
},
const config = getOwner(this).resolveRegistration("config:environment");

_hide() {
this.getWithDefault("on-hide", noop)();
this.set("container", config.APP.rootElement || "body");
},

didInsertElement() {
const id = `#modal-${this.elementId}`;

this.set(
"modal",
UIkit.modal(id, {
escClose: this.get("escClose"),
bgClose: this.get("bgClose"),
stack: this.get("stack"),
container: this.get("container"),
clsPage: this.get("clsPage"),
clsPanel: this.get("clsPanel"),
selClose: this.get("selClose")
})
UIkit.modal(
`#${this.modalId}`,
this.getProperties(
"escClose",
"bgClose",
"stack",
"container",
"clsPage",
"clsPanel",
"selClose"
)
)
);

UIkit.util.on(id, "show", () => this._show());
UIkit.util.on(id, "hidden", () => this._hide());
},

didReceiveAttrs() {
scheduleOnce("afterRender", this, "initVisible");
scheduleOnce("afterRender", this, "toggleModal");
},

willDestroyElement() {
this.modal.$el.parentNode.removeChild(this.modal.$el);
this.set("modal", null);
if (this.modal) {
this.modal.$destroy(true);

this.set("modal", null);
}
},

initVisible() {
toggleModal() {
if (!this.modal) return;

if (this.visible) {
this.modal.show();

UIkit.util.on(`#${this.modalId}`, "hidden", () =>
this.getWithDefault("on-hide", noop)
);
} else {
this.modal.hide();

UIkit.util.on(`#${this.modalId}`, "show", () =>
this.getWithDefault("on-show", noop)
);
}
}
});
6 changes: 3 additions & 3 deletions addon/templates/components/uk-modal.hbs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{{#-in-element container}}
<div id="modal-{{elementId}}">
<div class="uk-modal-dialog ">
{{#-in-element containerElement}}
<div id={{this.modalId}}>
<div class="uk-modal-dialog">
{{#if @btnClose}}<button class="uk-modal-close-default" type="button" uk-close></button>{{/if}}
<div class="uk-modal-body">
{{yield}}
Expand Down
2 changes: 1 addition & 1 deletion tests/dummy/app/snippets/modal.hbs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{#uk-modal visible=visible}}
{{#uk-modal visible=visible on-hide=(action (mut visible) false)}}
<h2 class="uk-modal-title">Attention</h2>
<p>Do you really want to proceed?</p>
<p class="uk-text-right">
Expand Down
1 change: 0 additions & 1 deletion tests/dummy/app/templates/docs/components/modal.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
<div class="uk-padding uk-box-shadow-medium uk-margin">
{{#uk-modal
visible=visible
on-submit=(action "submit")
on-show=(action (mut visible) true)
on-hide=(action (mut visible) false)
}}
Expand Down
38 changes: 34 additions & 4 deletions tests/integration/components/uk-modal-test.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,58 @@
import { module, test } from "qunit";
import { setupRenderingTest } from "ember-qunit";
import { render } from "@ember/test-helpers";
import { render, settled, triggerKeyEvent } from "@ember/test-helpers";
import { hbs } from "ember-cli-htmlbars";

module("Integration | Component | uk-modal", function(hooks) {
setupRenderingTest(hooks);

test("it doesn't render by default", async function(assert) {
assert.expect(1);

await render(hbs`
{{#uk-modal}}
<h2>Test</h2>
{{/uk-modal}}
`);

assert.dom(".uk-modal").hasText("Test");
await settled();

assert.dom(".uk-modal.uk-open").doesNotExist();
});

test("it renders if visible=true", async function(assert) {
assert.expect(1);

await render(hbs`
{{#uk-modal visible=true}}
<h2>Test</h2>
{{/uk-modal}}
`);

await settled();

assert.dom(".uk-modal.uk-open").exists();
});

test("it triggers the on-hide action", async function(assert) {
assert.expect(3);

this.set("hide", assert.step("hide"));

await render(hbs`
{{#uk-modal visible="true"}}
{{#uk-modal visible=true on-hide=hide}}
<h2>Test</h2>
{{/uk-modal}}
`);

assert.dom(".uk-modal").hasText("Test");
await settled();

// close by pressing esc
await triggerKeyEvent(".uk-modal", "keydown", 27);

await settled();

assert.dom(".uk-modal.uk-open").doesNotExist();
assert.verifySteps(["hide"]);
});
});

0 comments on commit 916b68b

Please sign in to comment.