Skip to content

Commit

Permalink
feat(form-builder): add option to use number separator widget as default
Browse files Browse the repository at this point in the history
  • Loading branch information
anehx committed May 2, 2024
1 parent 709a7a2 commit 725eb11
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
10 changes: 10 additions & 0 deletions packages/core/addon/services/caluma-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,18 @@ export default class CalumaOptionsService extends Service {
: null;
}

/**
* Use number separator widget for all viable questions (integer, float and
* calculated float) without exceptions.
*/
alwaysUseNumberSeparatorWidget = false;

/**
* Pre-select number separator widget when creating a viable question in the
* form builder.
*/
useNumberSeparatorWidgetAsDefault = false;

/**
* Registers a new component override.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
@name="__typename"
@required={{true}}
@disabled={{not (is-empty @slug)}}
@on-update={{changeset-set f.model "__typename"}}
@on-update={{this.updateType}}
/>

<f.input
Expand Down
24 changes: 24 additions & 0 deletions packages/form-builder/addon/components/cfb-form-editor/question.js
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,30 @@ export default class CfbFormEditorQuestion extends Component {
}
}

@action
updateType(value, changeset) {
changeset.set("__typename", value);

const defaultWidget = "cf-field/input/number-separator";
const currentWidget = changeset.get("meta.widgetOverride");
const isViableType = [
"IntegerQuestion",
"FloatQuestion",
"CalculatedFloatQuestion",
].includes(value);

if (this.calumaOptions.useNumberSeparatorWidgetAsDefault) {
if (isViableType && !currentWidget) {
// Set the default widget as override if the question type is viable for
// it and there is no widget selected yet
changeset.set("meta.widgetOverride", defaultWidget);
} else if (!isViableType && currentWidget === defaultWidget) {
// Remove default widget for non viable question types
changeset.set("meta.widgetOverride", undefined);
}
}
}

@action
updateSubForm(value, changeset) {
changeset.set("subForm.slug", value.slug);
Expand Down

0 comments on commit 725eb11

Please sign in to comment.