Skip to content

Commit

Permalink
Make more fields cloneable
Browse files Browse the repository at this point in the history
  • Loading branch information
annda committed Oct 24, 2023
1 parent 8653c1f commit e510144
Show file tree
Hide file tree
Showing 13 changed files with 374 additions and 115 deletions.
17 changes: 16 additions & 1 deletion doc/formelements.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ Simple text input.
required: false
```

* `clone` _(optional)_ - Adds a clone button to the field. That way you can repeat the same input to create a list of variable length.
* `clone` _(optional)_ - When set to `true`, adds a clone button to the field. That way you can repeat the same input to create a list of variable length.

### Numberinput

Expand All @@ -192,6 +192,8 @@ Simple number (integer) input.
required: false
```

* `clone` _(optional)_ - When set to `true`, adds a clone button to the field. That way you can repeat the same input to create a list of variable length.

### Textarea

Multiline text input field with optional size attributes.
Expand Down Expand Up @@ -225,6 +227,10 @@ Text input that expects a date and provides a calendar picker.
validation:
required: false
```

Options:
* `clone` _(optional)_ - When set to `true`, adds a clone button to the field. That way you can repeat the same input to create a list of variable length.

### Time

Text input that expects a time and provides a time picker.
Expand All @@ -238,6 +244,9 @@ Text input that expects a time and provides a time picker.
validation:
required: false
```
Options:
* `clone` _(optional)_ - When set to `true`, adds a clone button to the field. That way you can repeat the same input to create a list of variable length.

### Datetime

Text input that expects a date and a time and provides a combined picker.
Expand All @@ -251,6 +260,9 @@ Text input that expects a date and a time and provides a combined picker.
validation:
required: false
```
Options:
* `clone` _(optional)_ - When set to `true`, adds a clone button to the field. That way you can repeat the same input to create a list of variable length.

### Email

Text input that expects a valid email (the HTML5 validation is handled by the browser).
Expand All @@ -265,6 +277,9 @@ Text input that expects a valid email (the HTML5 validation is handled by the br
required: false
```

Options:
* `clone` _(optional)_ - When set to `true`, adds a clone button to the field. That way you can repeat the same input to create a list of variable length.

### Radioset

Representation of a radio group.
Expand Down
11 changes: 11 additions & 0 deletions public/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,17 @@ function cloneHandler(e) {
const label = cloned.querySelector('label');
label.setAttribute('for', newId);

const pickr = input.dataset.calendarType;
if (pickr === 'date') {
flatpickr(input, {'dateFormat' : 'd.m.Y', 'allowInput' : true});
}
if (pickr === 'datetime') {
flatpickr(input, {'enableTime' : true, 'time_24hr' : true, 'dateFormat' : 'd.m.Y H:i', 'allowInput' : true});
}
if (pickr === 'time') {
flatpickr(input, {'noCalendar' : true, 'enableTime' : true, 'time_24hr' : true, 'allowInput' : true});
}

elem.parentNode.parentNode.after(cloned);
elem.parentNode.removeChild(elem);
}
Expand Down
13 changes: 13 additions & 0 deletions src/FormGenerator/FormElements/DateFormElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,18 @@
*/
class DateFormElement extends AbstractDynamicFormElement
{
/**
* Handle values in a clonable field
*
* @return array
*/
public function getViewVariables()
{
$conf = parent::getViewVariables();
if (!empty($conf['clone']) && !is_array($conf['value'])) {
$conf['value'] = [$conf['value']];
}

return $conf;
}
}
13 changes: 13 additions & 0 deletions src/FormGenerator/FormElements/DateTimeFormElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,18 @@
*/
class DateTimeFormElement extends AbstractDynamicFormElement
{
/**
* Handle values in a clonable field
*
* @return array
*/
public function getViewVariables()
{
$conf = parent::getViewVariables();
if (!empty($conf['clone']) && !is_array($conf['value'])) {
$conf['value'] = [$conf['value']];
}

return $conf;
}
}
13 changes: 13 additions & 0 deletions src/FormGenerator/FormElements/EmailFormElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,18 @@
*/
class EmailFormElement extends AbstractDynamicFormElement
{
/**
* Handle values in a clonable field
*
* @return array
*/
public function getViewVariables()
{
$conf = parent::getViewVariables();
if (!empty($conf['clone']) && !is_array($conf['value'])) {
$conf['value'] = [$conf['value']];
}

return $conf;
}
}
13 changes: 13 additions & 0 deletions src/FormGenerator/FormElements/NumberInputFormElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,18 @@
*/
class NumberInputFormElement extends AbstractDynamicFormElement
{
/**
* Handle values in a clonable field
*
* @return array
*/
public function getViewVariables()
{
$conf = parent::getViewVariables();
if (!empty($conf['clone']) && !is_array($conf['value'])) {
$conf['value'] = [$conf['value']];
}

return $conf;
}
}
13 changes: 13 additions & 0 deletions src/FormGenerator/FormElements/TimeFormElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,18 @@
*/
class TimeFormElement extends AbstractDynamicFormElement
{
/**
* Handle values in a clonable field
*
* @return array
*/
public function getViewVariables()
{
$conf = parent::getViewVariables();
if (!empty($conf['clone']) && !is_array($conf['value'])) {
$conf['value'] = [$conf['value']];
}

return $conf;
}
}
80 changes: 57 additions & 23 deletions view/date.twig
Original file line number Diff line number Diff line change
@@ -1,27 +1,61 @@
{% from '_macros' import renderError, renderTooltip %}
<div class="column {{ column is not empty ? column : 'is-full' }}">
<div class="field">
<div class="label-with-tooltip">
<label class="label {{ labelsmall is not empty ? 'label-smaller' }}" for="{{ id }}">
{%- if label is not empty -%}
{{ label }} {{ is_required ? '*' }}
{%- endif -%}
</label>
{{ renderTooltip(tooltip) }}
{% if clone is not empty %}
{% for val in value %}
<div class="field" id="field-{{ id }}-{{ loop.index }}">
<div class="label-with-tooltip">
<label class="label {{ labelsmall is not empty ? 'label-smaller' }}" for="{{ id }}-{{ loop.index }}">
{%- if label is not empty -%}
{{ label }} {{ is_required ? '*' }}
{%- endif -%}
</label>
{{ renderTooltip(tooltip) }}
</div>
<div class="control is-cloneable">
<input
type="text"
id="{{ id }}-{{ loop.index }}"
name="{{ id }}[]"
value="{{ val }}"
class="input form-input{% if is_readonly%} has-background-grey-lighter{% endif %} {{ errors is not empty ? 'is-danger' }}"
data-calendar-type="date"
{% if is_readonly%}readonly{% endif %}
{% if placeholder is not empty -%}
placeholder="{{ placeholder ~ (is_required and label is empty ? ' *') }}"
{%- endif %} />
{% if loop.last %}
<button type="button" class="button is-light clone-field">
+ {{ button_clone_label }}
</button>
{% endif %}
</div>
{{ renderError(errors) }}
</div>
{% endfor %}
{% else %}
<div class="field">
<div class="label-with-tooltip">
<label class="label {{ labelsmall is not empty ? 'label-smaller' }}" for="{{ id }}">
{%- if label is not empty -%}
{{ label }} {{ is_required ? '*' }}
{%- endif -%}
</label>
{{ renderTooltip(tooltip) }}
</div>
<div class="control">
<input
type="text"
id="{{ id }}"
name="{{ id }}"
value="{{ value }}"
class="input form-input{% if is_readonly%} has-background-grey-lighter{% endif %} {{ errors is not empty ? 'is-danger' }}"
data-calendar-type="date"
{% if is_readonly%}readonly{% endif %}
{% if placeholder is not empty -%}
placeholder="{{ placeholder ~ (is_required and label is empty ? ' *') }}"
{%- endif %} />
</div>
{{ renderError(errors) }}
</div>
<div class="control">
<input
type="text"
id="{{ id }}"
name="{{ id }}"
value="{{ value }}"
class="input form-input{% if is_readonly%} has-background-grey-lighter{% endif %} {{ errors is not empty ? 'is-danger' }}"
data-calendar-type="date"
{% if is_readonly%}readonly{% endif %}
{% if placeholder is not empty -%}
placeholder="{{ placeholder ~ (is_required and label is empty ? ' *') }}"
{%- endif %} />
</div>
{{ renderError(errors) }}
</div>
{% endif %}
</div>
80 changes: 57 additions & 23 deletions view/datetime.twig
Original file line number Diff line number Diff line change
@@ -1,27 +1,61 @@
{% from '_macros' import renderError, renderTooltip %}
<div class="column {{ column is not empty ? column : 'is-full' }}">
<div class="field">
<div class="label-with-tooltip">
<label class="label {{ labelsmall is not empty ? 'label-smaller' }}" for="{{ id }}">
{%- if label is not empty -%}
{{ label }} {{ is_required ? '*' }}
{%- endif -%}
</label>
{{ renderTooltip(tooltip) }}
{% if clone is not empty %}
{% for val in value %}
<div class="field" id="field-{{ id }}-{{ loop.index }}">
<div class="label-with-tooltip">
<label class="label {{ labelsmall is not empty ? 'label-smaller' }}" for="{{ id }}-{{ loop.index }}">
{%- if label is not empty -%}
{{ label }} {{ is_required ? '*' }}
{%- endif -%}
</label>
{{ renderTooltip(tooltip) }}
</div>
<div class="control is-cloneable">
<input
type="text"
id="{{ id }}-{{ loop.index }}"
name="{{ id }}[]"
value="{{ val }}"
class="input form-input{% if is_readonly%} has-background-grey-lighter{% endif %} {{ errors is not empty ? 'is-danger' }}"
data-calendar-type="datetime"
{% if is_readonly%}readonly{% endif %}
{% if placeholder is not empty -%}
placeholder="{{ placeholder ~ (is_required and label is empty ? ' *') }}"
{%- endif %} />
{% if loop.last %}
<button type="button" class="button is-light clone-field">
+ {{ button_clone_label }}
</button>
{% endif %}
</div>
{{ renderError(errors) }}
</div>
{% endfor %}
{% else %}
<div class="field">
<div class="label-with-tooltip">
<label class="label {{ labelsmall is not empty ? 'label-smaller' }}" for="{{ id }}">
{%- if label is not empty -%}
{{ label }} {{ is_required ? '*' }}
{%- endif -%}
</label>
{{ renderTooltip(tooltip) }}
</div>
<div class="control">
<input
type="text"
id="{{ id }}"
name="{{ id }}"
value="{{ value }}"
class="input form-input{% if is_readonly%} has-background-grey-lighter{% endif %} {{ errors is not empty ? 'is-danger' }}"
data-calendar-type="datetime"
{% if is_readonly%}readonly{% endif %}
{% if placeholder is not empty -%}
placeholder="{{ placeholder ~ (is_required and label is empty ? ' *') }}"
{%- endif %} />
</div>
{{ renderError(errors) }}
</div>
<div class="control">
<input
type="text"
id="{{ id }}"
name="{{ id }}"
value="{{ value }}"
class="input form-input{% if is_readonly%} has-background-grey-lighter{% endif %} {{ errors is not empty ? 'is-danger' }}"
data-calendar-type="datetime"
{% if is_readonly%}readonly{% endif %}
{% if placeholder is not empty -%}
placeholder="{{ placeholder ~ (is_required and label is empty ? ' *') }}"
{%- endif %} />
</div>
{{ renderError(errors) }}
</div>
{% endif %}
</div>
Loading

0 comments on commit e510144

Please sign in to comment.