Skip to content

Commit

Permalink
feat: accept functions as label overrides
Browse files Browse the repository at this point in the history
to allow different labels for active/inactive models
  • Loading branch information
czosel committed Jun 29, 2022
1 parent b833a9b commit e318c6c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
4 changes: 2 additions & 2 deletions addon/components/edit-form.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
data-test-toggle-active
@onClick={{perform this.toggleActiveState}}
>
{{or (optional-translate this.deactivateLabelOverride) (t (concat "emeis.form." (if @model.isActive "deactivate" "activate")))}}
{{optional-translate this.deactivateLabelOverride}}
</UkButton>
{{/if}}

Expand All @@ -34,7 +34,7 @@
data-test-delete
@onClick={{perform this.delete}}
>
{{or (optional-translate this.deleteLabelOverride) (t "emeis.form.delete")}}
{{optional-translate this.deleteLabelOverride}}
</UkButton>
{{/if}}

Expand Down
24 changes: 20 additions & 4 deletions addon/components/edit-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,29 @@ export default class EditFormComponent extends Component {
}

get deactivateLabelOverride() {
return this.emeisOptions[this.args.model?._internalModel?.modelName]
?.actions?.deactivate?.label;
const label =
this.emeisOptions[this.args.model?._internalModel?.modelName]?.actions
?.deactivate?.label;
if (typeof label === "function") {
return label(this.args.model);
} else if (label) {
return label;
}
return this.intl.t(
`emeis.form.${this.args.model.isActive ? "deactivate" : "activate"}`
);
}

get deleteLabelOverride() {
return this.emeisOptions[this.args.model?._internalModel?.modelName]
?.actions?.delete?.label;
const label =
this.emeisOptions[this.args.model?._internalModel?.modelName]?.actions
?.delete?.label;
if (typeof label === "function") {
return label(this.args.model);
} else if (label) {
return label;
}
return this.intl.t("emeis.form.delete");
}

@task
Expand Down
7 changes: 4 additions & 3 deletions tests/dummy/app/services/emeis-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,12 @@ export default class EmeisOptionsService extends Service {
// scope view specific settings
scope = {
actions: {
delete: {
label: "my delete label",
delete: (model) => model.id !== "special",
deactivate: {
label: (model) =>
model.isActive ? "my deactivate label" : "my reactivate label",
fn: (model) => model.id !== "special",
},
deactivate: (model) => model.id !== "special",
},
metaFields: [
{
Expand Down

0 comments on commit e318c6c

Please sign in to comment.