Skip to content

Commit

Permalink
feat(utilities): add confirm utility
Browse files Browse the repository at this point in the history
  • Loading branch information
anehx committed Mar 8, 2022
1 parent 7d010b1 commit 5a97683
Show file tree
Hide file tree
Showing 15 changed files with 181 additions and 3 deletions.
1 change: 1 addition & 0 deletions addon/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as confirm } from "ember-uikit/utils/confirm";
7 changes: 7 additions & 0 deletions addon/instance-initializers/uikit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import UIkit from "uikit";

export function initialize(appInstance) {
UIkit.container = appInstance.rootElement;
}

export default { initialize };
11 changes: 11 additions & 0 deletions addon/utils/confirm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import UIkit from "uikit";

export default async function confirm(text) {
try {
await UIkit.modal.confirm(text);

return true;
} catch (error) {
return false;
}
}
1 change: 1 addition & 0 deletions app/instance-initializers/uikit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default, initialize } from "ember-uikit/instance-initializers/uikit";
1 change: 1 addition & 0 deletions app/utils/confirm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from "ember-uikit/utils/confirm";
16 changes: 16 additions & 0 deletions tests/dummy/app/controllers/docs/utilities/confirm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import Controller from "@ember/controller";
import { action } from "@ember/object";
import { tracked } from "@glimmer/tracking";
import { confirm } from "ember-uikit";

export default class DocsUtilitiesConfirmController extends Controller {
@tracked text = "Test";
@tracked result = "";

@action
async click(e) {
e.preventDefault();

this.result = await confirm(this.text);
}
}
1 change: 1 addition & 0 deletions tests/dummy/app/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Router.map(function () {
});

this.route("utilities", function () {
this.route("confirm");
this.route("flex");
this.route("width");
});
Expand Down
5 changes: 4 additions & 1 deletion tests/dummy/app/routes/docs/components/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import Route from "@ember/routing/route";
import { inject as service } from "@ember/service";

export default class DocsComponentsIndexRoute extends Route {
@service router;

redirect() {
this.replaceWith("docs.components.badge");
this.router.replaceWith("docs.components.badge");
}
}
5 changes: 4 additions & 1 deletion tests/dummy/app/routes/docs/services/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import Route from "@ember/routing/route";
import { inject as service } from "@ember/service";

export default class DocsServicesIndexRoute extends Route {
@service router;

redirect() {
this.replaceWith("docs.services.notification");
this.router.replaceWith("docs.services.notification");
}
}
5 changes: 4 additions & 1 deletion tests/dummy/app/routes/docs/utilities/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import Route from "@ember/routing/route";
import { inject as service } from "@ember/service";

export default class DocsUtilitiesIndexRoute extends Route {
@service router;

redirect() {
this.replaceWith("docs.utilities.flex");
this.router.replaceWith("docs.utilities.confirm");
}
}
9 changes: 9 additions & 0 deletions tests/dummy/app/snippets/confirm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { confirm } from "ember-uikit";

export default async function myFunction() {
if (!(await confirm("Really?"))) {
return;
}

// execute logic
}
1 change: 1 addition & 0 deletions tests/dummy/app/templates/application.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
<li class="uk-parent">
<LinkTo @route="docs.utilities">Utilities</LinkTo>
<ul class="uk-nav-sub">
<li><LinkTo @route="docs.utilities.confirm">Confirm</LinkTo></li>
<li><LinkTo @route="docs.utilities.flex">Flex</LinkTo></li>
<li><LinkTo @route="docs.utilities.width">Width</LinkTo></li>
</ul>
Expand Down
51 changes: 51 additions & 0 deletions tests/dummy/app/templates/docs/utilities/confirm.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<h2>Confirm</h2>

<div class="uk-padding uk-box-shadow-medium uk-margin">
<UkButton @color="primary" @onClick={{this.click}}>Click me!</UkButton>
<span class="uk-margin-small-left">
{{#if (eq this.result true)}}
<UkIcon @icon="check" />
{{else if (eq this.result false)}}
<UkIcon @icon="ban" />
{{/if}}
</span>
</div>

<UkSwitcher @animation="uk-animation-fade" as |switcher|>
<switcher.nav @type="tab" as |nav|>
<nav.component as |tab|>
<tab.item>Arguments</tab.item>
<tab.item>Example</tab.item>
</nav.component>
</switcher.nav>

<switcher.content as |content|>
<content.item>
<div class="uk-overflow-auto">
<table class="uk-table uk-table-divider">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Default</th>
<th>Description</th>
<th>Demo</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>text</code></td>
<td><code>String</code></td>
<td></td>
<td>The content of the confirm dialog, this can contain HTML.</td>
<td><Input class="uk-input" @value={{this.text}} /></td>
</tr>
</tbody>
</table>
</div>
</content.item>
<content.item>
<CodeSnippet @name="confirm.js" />
</content.item>
</switcher.content>
</UkSwitcher>
33 changes: 33 additions & 0 deletions tests/unit/instance-initializers/uikit-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import Application from "@ember/application";
import { run } from "@ember/runloop";
import config from "dummy/config/environment";
import { initialize } from "dummy/instance-initializers/uikit";
import Resolver from "ember-resolver";
import { module, test } from "qunit";

module("Unit | Instance Initializer | uikit", function (hooks) {
hooks.beforeEach(function () {
this.TestApplication = class TestApplication extends Application {
modulePrefix = config.modulePrefix;
podModulePrefix = config.podModulePrefix;
Resolver = Resolver;
};
this.TestApplication.instanceInitializer({
name: "initializer under test",
initialize,
});
this.application = this.TestApplication.create({ autoboot: false });
this.instance = this.application.buildInstance();
});
hooks.afterEach(function () {
run(this.instance, "destroy");
run(this.application, "destroy");
});

// TODO: Replace this with your real tests.
test("it works", async function (assert) {
await this.instance.boot();

assert.ok(true);
});
});
37 changes: 37 additions & 0 deletions tests/unit/utils/confirm-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { click, waitUntil, find } from "@ember/test-helpers";
import confirm from "dummy/utils/confirm";
import { setupRenderingTest } from "ember-qunit";
import { module, test } from "qunit";
import UIkit from "uikit";

module("Unit | Utility | confirm", function (hooks) {
setupRenderingTest(hooks);

test("it works", async function (assert) {
UIkit.container = this.element;

assert.expect(8);

confirm("confirm").then((result) => {
assert.true(result);
assert.step("confirmed");
});

await waitUntil(() => find(".uk-modal.uk-open"));
assert.dom(".uk-modal-body").hasText("confirm");
await click(".uk-modal-footer .uk-button-primary");

assert.verifySteps(["confirmed"]);

confirm("abort").then((result) => {
assert.false(result);
assert.step("rejected");
});

await waitUntil(() => find(".uk-modal.uk-open"));
assert.dom(".uk-modal-body").hasText("abort");
await click(".uk-modal-footer .uk-button-default");

assert.verifySteps(["rejected"]);
});
});

0 comments on commit 5a97683

Please sign in to comment.