Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/add mandatory asterisk #2871

Merged
merged 6 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>
anehx marked this conversation as resolved.
Show resolved Hide resolved
{{/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
Loading