Skip to content

Commit

Permalink
feat(form): add option to enable number separator widget by default
Browse files Browse the repository at this point in the history
  • Loading branch information
anehx committed Mar 26, 2024
1 parent 0e0d1e7 commit dfc3fa0
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 15 deletions.
2 changes: 2 additions & 0 deletions packages/-ember-caluma/app/services/caluma-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export default class CustomCalumaOptionsService extends CalumaOptionsService {
this.currentGroupId = 1;
}

alwaysUseNumberSeparatorWidget = true;

distribution = {
inquiry: {
answer: {
Expand Down
1 change: 0 additions & 1 deletion packages/-ember-caluma/mirage/scenarios/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ export default function (server) {
minValue: 0,
maxValue: null,
hintText: null,
meta: { widgetOverride: "cf-field/input/number-separator" },
});
server.create("question", {
slug: "height",
Expand Down
2 changes: 2 additions & 0 deletions packages/core/addon/services/caluma-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export default class CalumaOptionsService extends Service {
: null;
}

alwaysUseNumberSeparatorWidget = false;

/**
* Registers a new component override.
*
Expand Down
9 changes: 1 addition & 8 deletions packages/form/addon/components/cf-field-value.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,7 @@
@onClick={{fn this.download file.id}}
/>
{{/each}}
{{else if
(and
@field.answer.value
(eq
@field.question.raw.meta.widgetOverride "cf-field/input/number-separator"
)
)
}}
{{else if (and @field.answer.value @field.question.useNumberSeparatorWidget)}}
{{format-number @field.answer.value maximumFractionDigits=20}}
{{else}}
{{@field.answer.value}}
Expand Down
8 changes: 7 additions & 1 deletion packages/form/addon/helpers/get-widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,16 @@ export default class GetWidgetHelper extends Helper {

compute(params, { default: defaultWidget = "cf-field/input" }) {
for (const obj of params) {
const widget = obj?.raw?.meta?.widgetOverride;
let widget = obj?.raw?.meta?.widgetOverride;

if (obj?.useNumberSeparatorWidget) {
widget = "cf-field/input/number-separator";
}

if (!widget) {
continue;
}

const override =
widget &&
this.calumaOptions
Expand Down
15 changes: 15 additions & 0 deletions packages/form/addon/lib/question.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { assert } from "@ember/debug";
import { inject as service } from "@ember/service";
import { camelize } from "@ember/string";
import { queryManager } from "ember-apollo-client";
import { trackedFunction } from "reactiveweb/function";
Expand All @@ -17,6 +18,8 @@ const getValue = (answer) => {
* @class Question
*/
export default class Question extends Base {
@service calumaOptions;

@queryManager apollo;

constructor({ raw, ...args }) {
Expand Down Expand Up @@ -174,4 +177,16 @@ export default class Question extends Base {

return value;
}

get useNumberSeparatorWidget() {
return (
this.raw.meta.widgetOverride === "cf-field/input/number-separator" ||
(this.calumaOptions.alwaysUseNumberSeparatorWidget &&
[
"IntegerQuestion",
"FloatQuestion",
"CalculatedFloatQuestion",
].includes(this.raw.__typename))
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,16 +134,13 @@ module("Integration | Component | cf-field-value", function (hooks) {
assert.dom(this.element).hasText(file.name);
});

test("it numbers using the number-separator widget override", async function (assert) {
test("it renders numbers using the number-separator widget override", async function (assert) {
setLocale(["de-ch", "de"]);

this.field = {
questionType: "FloatQuestion",
question: {
raw: {
__typename: "FloatQuestion",
meta: { widgetOverride: "cf-field/input/number-separator" },
},
useNumberSeparatorWidget: true,
},
answer: {
value: 1111111.111111,
Expand Down

0 comments on commit dfc3fa0

Please sign in to comment.