Skip to content

Commit

Permalink
Feature/add mandatory asterisk (#2871)
Browse files Browse the repository at this point in the history
* feat(form): add mandatory indicator for labels and provide configuration flag

* feat(form): add cfField label.js for config handling of mandatory asterisk

* feat(form): improved syntax & tests for mandatory asterisk

* fix(form): fix eslint formatting in label-test.js

* fix(form): css class naming, minor label-test improvements for mandatory asterisk

* fix(form): lint fix
  • Loading branch information
tstoppe89 authored Jan 7, 2025
1 parent 8490d7f commit d23f5ad
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 5 deletions.
17 changes: 12 additions & 5 deletions packages/form/addon/components/cf-field/label.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@
{{@field.question.raw.label}}
</span>

{{#if (and (not @field.question.isCalculated) @field.optional)}}
<span
class="uk-margin-small-left uk-text-muted uk-text-lowercase uk-text-normal"
>({{t "caluma.form.optional"}})</span>
{{/if}}
{{#unless @field.question.isCalculated}}
{{#if (and @field.optional (not this.useMandatoryAsterisk))}}
<span
class="uk-margin-small-left uk-text-muted uk-text-lowercase uk-text-normal"
>({{t "caluma.form.optional"}})</span>
{{else if (and (not @field.optional) this.useMandatoryAsterisk)}}
<span
title={{t "caluma.form.mandatory"}}
class="uk-text-muted uk-text-lowercase uk-text-normal cf-label__mandatory-asterisk"
>* </span>
{{/if}}
{{/unless}}

{{yield}}
</label>
20 changes: 20 additions & 0 deletions packages/form/addon/components/cf-field/label.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { getOwner } from "@ember/application";
import Component from "@glimmer/component";

/**
* Label component of the CfField
*
* @class CfFieldLabelComponent
*/
export default class CfFieldLabelComponent extends Component {
get config() {
return getOwner(this).resolveRegistration("config:environment");
}

get useMandatoryAsterisk() {
const { USE_MANDATORY_ASTERISK = false } =
this.config["ember-caluma"] || {};

return this.args.useMandatoryAsterisk ?? USE_MANDATORY_ASTERISK;
}
}
31 changes: 31 additions & 0 deletions packages/form/tests/integration/components/cf-field/label-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,35 @@ module("Integration | Component | cf-field/label", function (hooks) {

assert.dom("label").hasText("Test (Optional)");
});

test("it marks mandatory fields as such if useMandatoryAsterisk is configured in attribute", async function (assert) {
assert.expect(2);

await render(
hbs`<CfField::Label @field={{this.field}} @useMandatoryAsterisk={{true}} />`,
);

this.set("field.question.raw.isRequired", "false");
assert.dom("label").hasText("Test");

this.set("field.question.raw.isRequired", "true");
assert.dom("label").hasText("Test *");
});

test("it marks mandatory fields as such if useMandatoryAsterisk is globally configured", async function (assert) {
this.owner.resolveRegistration("config:environment")["ember-caluma"] = {
USE_MANDATORY_ASTERISK: true,
};

assert.expect(2);
this.set("useMandatoryAsterisk", true);

await render(hbs`<CfField::Label @field={{this.field}} />`);

this.set("field.question.raw.isRequired", "false");
assert.dom("label").hasText("Test");

this.set("field.question.raw.isRequired", "true");
assert.dom("label").hasText("Test *");
});
});
1 change: 1 addition & 0 deletions packages/form/translations/de.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ caluma:
addRow: "Zeile hinzufügen"
optionNotAvailable: "Diese Option ist nicht mehr verfügbar"
info: "Mehr Informationen"
mandatory: "Pflichtfeld"

error:
details: "Technische Details:"
Expand Down
1 change: 1 addition & 0 deletions packages/form/translations/en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ caluma:
addRow: "Add row"
optionNotAvailable: "This option is not available anymore"
info: "More information"
mandatory: "Mandatory field"

error:
details: "Technical details:"
Expand Down
1 change: 1 addition & 0 deletions packages/form/translations/fr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ caluma:
addRow: "Ajouter une ligne"
optionNotAvailable: "Cette option n'est plus disponible"
info: "Plus d'informations"
mandatory: "Champ obligatoire"

error:
details: "Détails techniques :"
Expand Down
1 change: 1 addition & 0 deletions packages/form/translations/it.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ caluma:
addRow: "Aggiungi riga"
optionNotAvailable: "Questa opzione non è più disponibile"
info: "Ulteriori informazioni"
mandatory: "Campo obbligatorio"

error:
details: "Dettagli tecnici:"
Expand Down

0 comments on commit d23f5ad

Please sign in to comment.