Skip to content

Commit

Permalink
feat(form): allow forms to have a widget override
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonas Metzener committed Sep 5, 2019
1 parent 3bbe238 commit ed5b4d6
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 26 deletions.
1 change: 1 addition & 0 deletions addon/gql/queries/get-document-forms.graphql
Expand Up @@ -6,6 +6,7 @@ query($slug: String!) {
node {
slug
name
meta
questions {
edges {
node {
Expand Down
46 changes: 24 additions & 22 deletions addon/helpers/get-widget.js
@@ -1,17 +1,20 @@
import Helper from "@ember/component/helper";
import { get } from "@ember/object";
import { inject as service } from "@ember/service";
import { warn } from "@ember/debug";

/**
* Helper for getting the right widget for a field.
* Helper for getting the right widget.
*
* This helper expects a field as first positional parameter. It checks if the
* field has a widget override in it's metadata. If one exists it checks if
* This helper expects n objects as positional parameters. It checks if the
* object has a widget override in it's metadata. If one exists it checks if
* said widget was registered in the caluma options service and then returns
* the widget name.
* the widget name. If it doesn't have a valid widget, the next object will be
* checked. If no object returns a valid widget, the passed default widget will
* be used.
*
* ```hbs
* {{component (get-widget field default="cf-form") foo=bar}}
* {{component (get-widget field.question someobject default="cf-form") foo=bar}}
* ```
*
* @function getWidget
Expand All @@ -22,25 +25,24 @@ import { warn } from "@ember/debug";
export default Helper.extend({
calumaOptions: service(),

compute([field], { default: defaultWidget = "cf-field/input" }) {
try {
const widget = field.question.meta.widgetOverride;
const overrides = this.calumaOptions.getComponentOverrides();
const override = overrides.find(({ component }) => component === widget);
compute(params, { default: defaultWidget = "cf-field/input" }) {
for (let obj of params) {
const widget = obj && get(obj, "meta.widgetOverride");
const override =
widget &&
this.calumaOptions
.getComponentOverrides()
.find(({ component }) => component === widget);

if (!override) {
warn(
`Widget override "${widget}" is not registered. Please register it by calling \`calumaOptions.registerComponentOverride\``,
!widget,
{ id: "ember-caluma.unregistered-override" }
);
warn(
`Widget override "${widget}" is not registered. Please register it by calling \`calumaOptions.registerComponentOverride\``,
!override,
{ id: "ember-caluma.unregistered-override" }
);

throw new Error();
}

return widget;
} catch (e) {
return defaultWidget;
if (override) return widget;
}

return defaultWidget;
}
});
2 changes: 1 addition & 1 deletion addon/templates/components/cf-field.hbs
Expand Up @@ -4,7 +4,7 @@
{{/if}}
<div class="uk-flex">
<div class="uk-width-expand">
{{component (get-widget field) field=field disabled=disabled context=context onSave=(perform save)}}
{{component (get-widget field.question) field=field disabled=disabled context=context onSave=(perform save)}}
</div>
{{#if field.question.infoText}}
{{cf-field/info text=field.question.infoText}}
Expand Down
2 changes: 1 addition & 1 deletion addon/templates/components/cf-form-wrapper.hbs
@@ -1 +1 @@
{{component (get-widget fieldset.field default="cf-form") document=document fieldset=fieldset context=context disabled=disabled}}
{{component (get-widget fieldset.field.question fieldset.form default="cf-form") document=document fieldset=fieldset context=context disabled=disabled}}
23 changes: 21 additions & 2 deletions tests/integration/helpers/get-widget-test.js
Expand Up @@ -15,15 +15,15 @@ module("Integration | Helper | get-widget", function(hooks) {
});

await render(
hbs`{{get-widget (hash question=(hash meta=(hash widgetOverride="some-component")))}}`
hbs`{{get-widget (hash meta=(hash widgetOverride="some-component"))}}`
);

assert.dom(this.element).hasText("some-component");
});

test("it doesn't return an invalid override", async function(assert) {
await render(
hbs`{{get-widget (hash question=(hash meta=(hash widgetOverride="some-component")))}}`
hbs`{{get-widget (hash meta=(hash widgetOverride="some-component"))}}`
);

assert.dom(this.element).hasText("cf-field/input");
Expand All @@ -44,4 +44,23 @@ module("Integration | Helper | get-widget", function(hooks) {

assert.dom(this.element).hasText("cf-form");
});

test("it can handle multiple objects", async function(assert) {
const calumaOptions = this.owner.lookup("service:calumaOptions");

calumaOptions.registerComponentOverride({
label: "Some Component",
component: "some-component"
});

await render(
hbs`{{get-widget
null
(hash meta=(hash widgetOverride="some-invalid-component"))
(hash meta=(hash widgetOverride="some-component"))
}}`
);

assert.dom(this.element).hasText("some-component");
});
});

0 comments on commit ed5b4d6

Please sign in to comment.