-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(form): enable widget overrides for forms
- Loading branch information
Showing
7 changed files
with
117 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import Helper from "@ember/component/helper"; | ||
import { inject as service } from "@ember/service"; | ||
import { warn } from "@ember/debug"; | ||
|
||
/** | ||
* Helper for getting the right widget for a field. | ||
* | ||
* 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 | ||
* said widget was registered in the caluma options service and then returns | ||
* the widget name. | ||
* | ||
* ```hbs | ||
* {{component (get-widget field default="cf-form") foo=bar}} | ||
* ``` | ||
* | ||
* @function getWidget | ||
* @param {Array} params | ||
* @param {Object} [options] | ||
* @param {String} [options.default] | ||
*/ | ||
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); | ||
|
||
if (!override) { | ||
warn( | ||
`Widget override "${widget}" is not registered. Please register it by calling \`calumaOptions.registerComponentOverride\``, | ||
{ id: "ember-caluma.unregistered-override" } | ||
); | ||
|
||
throw new Error(); | ||
} | ||
|
||
return widget; | ||
} catch (e) { | ||
return defaultWidget; | ||
} | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default, getWidget } from "ember-caluma/helpers/get-widget"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { module, test } from "qunit"; | ||
import { setupRenderingTest } from "ember-qunit"; | ||
import { render } from "@ember/test-helpers"; | ||
import hbs from "htmlbars-inline-precompile"; | ||
|
||
module("Integration | Helper | get-widget", function(hooks) { | ||
setupRenderingTest(hooks); | ||
|
||
test("it returns valid overrides", async function(assert) { | ||
const calumaOptions = this.owner.lookup("service:calumaOptions"); | ||
|
||
calumaOptions.registerComponentOverride({ | ||
label: "Some Component", | ||
component: "some-component" | ||
}); | ||
|
||
await render( | ||
hbs`{{get-widget (hash question=(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")))}}` | ||
); | ||
|
||
assert.dom(this.element).hasText("cf-field/input"); | ||
}); | ||
|
||
test("it has a fallback", async function(assert) { | ||
await render(hbs`{{get-widget null}}`); | ||
|
||
assert.dom(this.element).hasText("cf-field/input"); | ||
|
||
await render(hbs`{{get-widget undefined}}`); | ||
|
||
assert.dom(this.element).hasText("cf-field/input"); | ||
}); | ||
|
||
test("it can pass the default widget", async function(assert) { | ||
await render(hbs`{{get-widget null default="cf-form"}}`); | ||
|
||
assert.dom(this.element).hasText("cf-form"); | ||
}); | ||
}); |