From dfc3fa0fccdba346d662c338da1a2b19407c484b Mon Sep 17 00:00:00 2001 From: Jonas Metzener Date: Mon, 25 Mar 2024 15:40:40 +0100 Subject: [PATCH] feat(form): add option to enable number separator widget by default --- .../-ember-caluma/app/services/caluma-options.js | 2 ++ .../-ember-caluma/mirage/scenarios/default.js | 1 - packages/core/addon/services/caluma-options.js | 2 ++ packages/form/addon/components/cf-field-value.hbs | 9 +-------- packages/form/addon/helpers/get-widget.js | 8 +++++++- packages/form/addon/lib/question.js | 15 +++++++++++++++ .../integration/components/cf-field-value-test.js | 7 ++----- 7 files changed, 29 insertions(+), 15 deletions(-) diff --git a/packages/-ember-caluma/app/services/caluma-options.js b/packages/-ember-caluma/app/services/caluma-options.js index 62136c3b0..f8c87bf50 100644 --- a/packages/-ember-caluma/app/services/caluma-options.js +++ b/packages/-ember-caluma/app/services/caluma-options.js @@ -29,6 +29,8 @@ export default class CustomCalumaOptionsService extends CalumaOptionsService { this.currentGroupId = 1; } + alwaysUseNumberSeparatorWidget = true; + distribution = { inquiry: { answer: { diff --git a/packages/-ember-caluma/mirage/scenarios/default.js b/packages/-ember-caluma/mirage/scenarios/default.js index 7323c87b7..f2989626e 100644 --- a/packages/-ember-caluma/mirage/scenarios/default.js +++ b/packages/-ember-caluma/mirage/scenarios/default.js @@ -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", diff --git a/packages/core/addon/services/caluma-options.js b/packages/core/addon/services/caluma-options.js index def2f2497..1ace084f9 100644 --- a/packages/core/addon/services/caluma-options.js +++ b/packages/core/addon/services/caluma-options.js @@ -31,6 +31,8 @@ export default class CalumaOptionsService extends Service { : null; } + alwaysUseNumberSeparatorWidget = false; + /** * Registers a new component override. * diff --git a/packages/form/addon/components/cf-field-value.hbs b/packages/form/addon/components/cf-field-value.hbs index 988caca1a..e7e15ae33 100644 --- a/packages/form/addon/components/cf-field-value.hbs +++ b/packages/form/addon/components/cf-field-value.hbs @@ -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}} diff --git a/packages/form/addon/helpers/get-widget.js b/packages/form/addon/helpers/get-widget.js index 04b3504ad..631fadd29 100644 --- a/packages/form/addon/helpers/get-widget.js +++ b/packages/form/addon/helpers/get-widget.js @@ -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 diff --git a/packages/form/addon/lib/question.js b/packages/form/addon/lib/question.js index 2b9b39cfd..3e2e5479f 100644 --- a/packages/form/addon/lib/question.js +++ b/packages/form/addon/lib/question.js @@ -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"; @@ -17,6 +18,8 @@ const getValue = (answer) => { * @class Question */ export default class Question extends Base { + @service calumaOptions; + @queryManager apollo; constructor({ raw, ...args }) { @@ -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)) + ); + } } diff --git a/packages/form/tests/integration/components/cf-field-value-test.js b/packages/form/tests/integration/components/cf-field-value-test.js index 920b72bfd..3d468c747 100644 --- a/packages/form/tests/integration/components/cf-field-value-test.js +++ b/packages/form/tests/integration/components/cf-field-value-test.js @@ -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,