Skip to content

Commit

Permalink
feat(emeis-options): allow multiple custom buttons per view
Browse files Browse the repository at this point in the history
  • Loading branch information
derrabauke committed Dec 21, 2021
1 parent bcf7add commit 5a353ec
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 36 deletions.
17 changes: 12 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,19 @@ export default class EmeisOptionsService extends Service {
// show only a subset of the main navigation entries
navigationEntries = ["users", "scopes"];

// integrate a custom button into any edit view. On each edit view (e.g. users) you can define a label and a callback function for the button.
// On each model edit view (e.g. users) you can define a list of custom buttons. Each button needs a label and a callback function. Optionally you can highlight the button with the 'type' attribute.
customButtons = {
users: {
label: "My Button", // this could also be an ember-intl translation key
callback: () => window.alert("test"),
}
users: [
{
label: "My Button", // this could also be an ember-intl translation key
callback: () => window.alert("test"),
type: "danger" // leave blank or choose between primary, danger
},
{
label: "A second Button",
callback: () => window.alert("test"),
}
]
};

// define custom fields for a given context (user, scope, role or permission)
Expand Down
17 changes: 10 additions & 7 deletions addon/components/edit-form.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@
{{yield}}

<div class="uk-flex uk-flex-left uk-margin">
{{#if this.hasCustomButton}}
<UkButton
@type="button"
@label={{optional-translate this.customButtonLabel}}
{{on "click" this.customAction}}
data-test-custom-button
/>
{{#if this.customButtons}}
{{#each this.customButtons as |button index|}}
<UkButton
@type="button"
@label={{optional-translate button.label}}
class="uk-margin-right {{if button.type (concat 'uk-button-' button.type) ''}}"
{{on "click" (fn this.customAction index)}}
data-test-custom-button
/>
{{/each}}
{{/if}}
</div>

Expand Down
17 changes: 5 additions & 12 deletions addon/components/edit-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,28 +44,21 @@ export default class EditFormComponent extends Component {
return this.relativeParentRouteName.split(".")[0];
}

get hasCustomButton() {
get customButtons() {
return this.emeisOptions.customButtons?.[this.modelName];
}

get customButtonLabel() {
return (
this.emeisOptions.customButtons?.[this.modelName]?.label ||
"custom button"
);
}

@action
customAction() {
customAction(index) {
if (
typeof this.emeisOptions.customButtons?.[this.modelName].callback !==
"function"
typeof this.emeisOptions.customButtons?.[this.modelName][index]
?.callback !== "function"
) {
this.notification.danger(
this.intl.t("emeis.form.custom-button-action-error")
);
}
this.emeisOptions.customButtons?.[this.modelName]?.callback();
this.emeisOptions.customButtons?.[this.modelName][0].callback();
}

@task
Expand Down
15 changes: 11 additions & 4 deletions tests/dummy/app/services/emeis-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,17 @@ export default class EmeisOptionsService extends Service {
// };
// navigationEntries = ["users", "scopes"];
customButtons = {
users: {
label: "This is a custom button",
callback: () => console.warn("test"),
},
users: [
{
label: "This is a custom button",
callback: () => console.warn("test"),
type: "primary",
},
{
label: "Second Button",
callback: () => console.warn("test"),
},
],
};
metaFields = {
user: [
Expand Down
20 changes: 12 additions & 8 deletions tests/integration/components/edit-form-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ import { module, test } from "qunit";

class EmeisOptionsStub extends Service {
customButtons = {
users: {
label: "This is a custom button",
callback: () => {
document
.querySelector("[data-test-custom-button]")
.setAttribute("data-test-action-triggered", true);
users: [
{
label: "This is a custom button",
callback: () => {
document
.querySelector("[data-test-custom-button]")
.setAttribute("data-test-action-triggered", true);
},
type: "danger",
},
},
],
};
}

Expand Down Expand Up @@ -122,14 +125,15 @@ module("Integration | Component | edit-form", function (hooks) {
});

test("custom action", async function (assert) {
assert.expect(3);
assert.expect(4);
this.router.currentRoute.parent.name = "ember-emeis.users.edit";

await render(hbs`
<EditForm></EditForm>
`);

assert.dom("[data-test-custom-button]").exists();
assert.dom("[data-test-custom-button].uk-button-danger").exists();
assert
.dom("[data-test-custom-button]")
.hasNoAttribute("data-test-action-triggered");
Expand Down

0 comments on commit 5a353ec

Please sign in to comment.