Skip to content

Commit

Permalink
feat: mark optional fields (#507)
Browse files Browse the repository at this point in the history
* fix(user): change phone placeholder

* feat: mark optional fields

Works for built in and custom meta fields.
  • Loading branch information
derrabauke authored Jun 29, 2022
1 parent 2c25eeb commit 51debd0
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 18 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,9 @@ export default class EmeisOptionsService extends Service {
label: "My Input", // this could also be an ember-intl translation key
type: "text",
visible: true,
readOnly: false
readOnly: false,
required: false, //marks this field as optional
placeholder: "some.translation.key" //ember-intl translation key or plain string
},
{
slug: "test-input-2",
Expand All @@ -146,7 +148,8 @@ export default class EmeisOptionsService extends Service {
],
type: "choice",
visible: () => true,
readOnly: false
readOnly: false,
required: true, //marks this field as required
}
]
}
Expand All @@ -161,6 +164,10 @@ There are special options available for `options`, `type` and `visible` properti
Defines the type of the output component and can either be a _text_ or a _choice_.
#### **required** - meta field
Marks this field as optional or validates its presence in case it's set to `true`. Custom _choice_ fields may not be validated as required, tho.
#### **options** - meta field
In combination with `type:"choice"` the options can be a list of options (`{value, label}`) or a (async) function which resolves to a list of options.
Expand Down
3 changes: 3 additions & 0 deletions addon/components/edit-form/element.hbs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<div class="uk-margin">
<label class="uk-form-label" for="form-stacked-text">
{{@label}}
{{#if @optional}}
<span class="uk-text-muted uk-text-lowercase uk-text-normal">({{t "emeis.optional"}})</span>
{{/if}}
</label>
<div class="uk-form-controls">
{{yield}}
Expand Down
6 changes: 4 additions & 2 deletions addon/components/meta-field.hbs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{#if this.visible.value}}
<EditForm::Element @label={{optional-translate @field.label}}>
<EditForm::Element @label={{optional-translate @field.label}} @optional={{@optional}}>
{{#if (eq @field.type "choice")}}
{{#if this.options.isRunning}}
<UkSpinner @ratio="1" />
Expand All @@ -9,7 +9,7 @@
@options={{this.options.value}}
@selected={{find-by "value" (get @model.metainfo @field.slug) this.options.value}}
@onChange={{fn this.updateMetaField @field @model}}
@placeholder={{t @field.label}}
@placeholder={{optional-translate @field.placeholder}}
@allowClear={{true}}
@renderInPlace={{true}}
as |option|
Expand All @@ -25,6 +25,8 @@
type="text"
value={{get @model.metainfo @field.slug}}
disabled={{this.readOnly.value}}
required={{@field.required}}
placeholder={{optional-translate @field.placeholder}}
{{on "input" (fn this.updateMetaField @field @model)}}
>
{{/if}}
Expand Down
4 changes: 3 additions & 1 deletion addon/templates/permissions/edit.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
type="text"
name="slug"
pattern="^[-a-zA-Z0-9_]+$"
required
disabled={{not @model.isNew}}
value={{@model.slug}}
/>
Expand Down Expand Up @@ -46,7 +47,8 @@
@placeholder="{{t "emeis.permissions.headings.roles"}}..."
@onChange={{set @model.roles}}
@multiple="true"
required as |role|
required
as |role|
>
{{role.name}}
</RelationshipSelect>
Expand Down
3 changes: 2 additions & 1 deletion addon/templates/roles/edit.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
{{t "emeis.form.back"}}
</LinkTo>
<EditForm @model={{@model}} @updateModel={{this.updateModel}} class="uk-margin">
<EditForm::Element @label={{t "emeis.roles.headings.slug"}}>
<EditForm::Element @label={{t "emeis.roles.headings.slug"}} @optional={{unless @model.isNew true}}>
<input
class="uk-input"
type="text"
name="slug"
pattern="^[-a-zA-Z0-9_]+$"
required
disabled={{not @model.isNew}}
value={{@model.slug}}
/>
Expand Down
6 changes: 3 additions & 3 deletions addon/templates/scopes/edit.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
/>
</EditForm::Element>

<EditForm::Element @label={{t "emeis.scopes.headings.description"}}>
<EditForm::Element @label={{t "emeis.scopes.headings.description"}} @optional={{true}}>
<textarea
class="uk-textarea"
rows="5"
Expand All @@ -31,7 +31,7 @@
></textarea>
</EditForm::Element>

<EditForm::Element @label={{t "emeis.scopes.headings.parent"}}>
<EditForm::Element @label={{t "emeis.scopes.headings.parent"}} @optional={{true}}>
<RelationshipSelect
@model={{this.allScopes}}
@selected={{@model.parent}}
Expand All @@ -43,7 +43,7 @@
</EditForm::Element>

{{#each this.metaFields as |field|}}
<MetaField @field={{field}} @model={{@model}} />
<MetaField @field={{field}} @model={{@model}} @optional={{unless field.required true}} />
{{/each}}

</EditForm>
Expand Down
16 changes: 8 additions & 8 deletions addon/templates/users/edit.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@
</EditForm::Element>

{{#if (includes "phone" this.visibleFields)}}
<EditForm::Element @label={{t "emeis.users.headings.phone"}}>
<EditForm::Element @label={{t "emeis.users.headings.phone"}} @optional={{not (includes "phone" this.requiredFields)}}>
<input
class="uk-input"
type="tel"
name="phone"
placeholder="{{t "emeis.users.headings.phone"}}..."
placeholder="{{t "emeis.users.headings.phone-placeholder"}}..."
required={{includes "phone" this.requiredFields}}
pattern="^[0-9()\-\s]+$"
value={{@model.phone}}
Expand All @@ -79,7 +79,7 @@
{{/if}}

{{#if (includes "language" this.visibleFields)}}
<EditForm::Element @label={{t "emeis.users.headings.language"}}>
<EditForm::Element @label={{t "emeis.users.headings.language"}} @optional={{not (includes "language" this.requiredFields)}}>
<select
class="uk-select uk-text-uppercase"
required={{includes "language" this.requiredFields}}
Expand All @@ -97,20 +97,20 @@
{{/if}}

{{#if (includes "address" this.visibleFields)}}
<EditForm::Element @label={{t "emeis.users.headings.address"}}>
<EditForm::Element @label={{t "emeis.users.headings.address"}} @optional={{not (includes "address" this.requiredFields)}}>
<input
class="uk-input"
type="text"
name="address"
placeholder="{{t "emeis.users.headings.address"}}..."
required={{includes "phone" this.requiredFields}}
required={{includes "address" this.requiredFields}}
value={{@model.address}}
/>
</EditForm::Element>
{{/if}}

{{#if (includes "city" this.visibleFields)}}
<EditForm::Element @label={{t "emeis.users.headings.city"}}>
<EditForm::Element @label={{t "emeis.users.headings.city"}} @optional={{not (includes "city" this.requiredFields)}}>
<input
class="uk-input"
type="text"
Expand All @@ -123,7 +123,7 @@
{{/if}}

{{#if (includes "zip" this.visibleFields)}}
<EditForm::Element @label={{t "emeis.users.headings.zip"}}>
<EditForm::Element @label={{t "emeis.users.headings.zip"}} @optional={{not (includes "zip" this.requiredFields)}}>
<input
class="uk-input"
type="number"
Expand All @@ -136,7 +136,7 @@
{{/if}}

{{#each this.metaFields as |field|}}
<MetaField @field={{field}} @model={{@model}} />
<MetaField @field={{field}} @model={{@model}} @optional={{unless field.required true}} />
{{/each}}
</EditForm>

Expand Down
3 changes: 3 additions & 0 deletions tests/dummy/app/services/emeis-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ export default class EmeisOptionsService extends Service {
type: "text",
visible: true,
readOnly: false,
required: false,
placeholder: "emeis.options.meta.user.example",
},
// {
// slug: "user-meta-example-2",
Expand Down Expand Up @@ -77,6 +79,7 @@ export default class EmeisOptionsService extends Service {
},
visible: () => true, // boolean or function which evaluates to a boolean value
readOnly: false,
required: false,
},
{
slug: "meta-example-2",
Expand Down
4 changes: 3 additions & 1 deletion translations/de.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ emeis:
empty: "Keine Einträge gefunden..."
loading: "Laden..."
inactive: "Inaktiv"
optional: "optional"
general-error: "Ein Problem ist aufgetretten. Bitte versuchen Sie es erneut."
actions: "Aktionen"
export: "Export"
Expand All @@ -30,7 +31,8 @@ emeis:
firstName: "Vorname"
lastName: "Nachname"
email: "E-Mail"
phone: "Telefonnummer"
phone: "Telefon"
phone-placeholder: "+41 31 123 45 56"
language: "Sprache"
address: "Adresse"
city: "Stadt"
Expand Down
2 changes: 2 additions & 0 deletions translations/en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ emeis:
empty: "No entries found..."
loading: "Loading..."
inactive: "Inactive"
optional: "optional"
general-error: "A problem occured. Please try again."
actions: "Actions"
export: "Export"
Expand Down Expand Up @@ -31,6 +32,7 @@ emeis:
lastName: "Last Name"
email: "E-Mail"
phone: "Phone"
phone-placeholder: "+41 31 123 45 56"
language: "Language"
address: "Address"
city: "City"
Expand Down

0 comments on commit 51debd0

Please sign in to comment.