Skip to content

Commit

Permalink
Add modals to all form elements
Browse files Browse the repository at this point in the history
  • Loading branch information
annda committed Feb 14, 2024
1 parent ce8acfc commit 6e98940
Show file tree
Hide file tree
Showing 25 changed files with 68 additions and 52 deletions.
3 changes: 2 additions & 1 deletion data/EXAMPLE/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ form:
- 'Choice #2'
- 'Choice #3'
modal: |
###This is a modal.
###This is a modal. {.title .is-1}
With paragraphs.
Expand All @@ -109,6 +109,7 @@ form:
And images.
Any markdown goes.
tooltip: 'Optional input\nAllowed characters: a-Z, 0-9'
dropdown1:
type: dropdown
label: 'Dropdown'
Expand Down
1 change: 1 addition & 0 deletions doc/formelements.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Options:
* `label` _(optional)_ - the label of the form element (excluded: [hidden](#hidden), [markdown](#markdown))
* `labelsmall` _(optional)_ - if set to true, the label will be rendered in regular font instead of default bold
* `tooltip` _(optional)_ - Shows a hint for the element. Ignored in structure elements like spacers and HR. Also see [Tooltip styling](meta.md#Tooltips)
* `modal` _(optional)_ - Content of a simple Bulma modal in markdown syntax. Refer to the bundled EXAMPLE configuration. Images are interpreted as files located directly in your form directory. The option is ignored in structure elements like spacers and HR.
* `column` _(optional)_ - [bulma column sizes](https://bulma.io/documentation/columns/sizes/) defining the width of the form element (excluded: [hidden](#hidden)). You can use [offset](https://bulma.io/documentation/columns/sizes/#offset) to position the columns, for example `is-half is-offset-one-quarter` to center a half-width column.
```yaml
<id>:
Expand Down
8 changes: 8 additions & 0 deletions public/css/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -253,3 +253,11 @@ div .field.with-tooltip .control {
margin-left: 0.5rem;
}
}

/**
* Modal
*/

div.modal-content div.box * {
font-weight: initial;
}
3 changes: 3 additions & 0 deletions public/info.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

phpinfo();
6 changes: 3 additions & 3 deletions public/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,6 @@ document.addEventListener('DOMContentLoaded', () => {
});
}

console.log('HERE');
// Add a click event on buttons to open a specific modal
(document.querySelectorAll('.js-modal-trigger') || []).forEach(($trigger) => {
const modal = $trigger.dataset.target;
Expand All @@ -307,7 +306,8 @@ document.addEventListener('DOMContentLoaded', () => {
(document.querySelectorAll('.modal-background, .modal-close, .modal-card-head .delete, .modal-card-foot .button') || []).forEach(($close) => {
const $target = $close.closest('.modal');

$close.addEventListener('click', () => {
$close.addEventListener('click', (evt) => {
evt.preventDefault();
closeModal($target);
});
});
Expand All @@ -318,4 +318,4 @@ document.addEventListener('DOMContentLoaded', () => {
closeAllModals();
}
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -163,16 +163,13 @@ public function getPlaceholderValue()
public function getViewVariables()
{
return array_merge(
$this->getConfig(),
parent::getViewVariables(),
[
'id' => $this->getFormElementId(),
'id_string' => $this->getFormElementIdStringified(),
'value' => $this->getValue(),
'errors' => $this->getErrors(),
'is_required' => $this->isRequired(),
'is_readonly' => $this->isReadonly(),
'tooltip' => $this->parseTooltip(),
'modal' => $this->parseModal(),
'placeholder' => $this->getPlaceholderValue()
]
);
Expand Down
12 changes: 11 additions & 1 deletion src/FormGenerator/FormElements/AbstractFormElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,5 +170,15 @@ protected function parseModal()
*
* @return array
*/
abstract public function getViewVariables();
public function getViewVariables()
{
return array_merge(
$this->config,
[
'id' => $this->getFormElementId(),
'tooltip' => $this->parseTooltip(),
'modal' => $this->parseModal(),
]
);
}
}
13 changes: 0 additions & 13 deletions src/FormGenerator/FormElements/AbstractStaticFormElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,4 @@
*/
abstract class AbstractStaticFormElement extends AbstractFormElement
{
/**
* @inheritdoc
* @return array
*/
public function getViewVariables()
{
return array_merge(
$this->getConfig(),
[
'id' => $this->getFormElementId()
]
);
}
}
4 changes: 1 addition & 3 deletions src/FormGenerator/FormElements/MarkDownFormElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,9 @@ public function getViewVariables()
$markdown = MarkdownExtra::defaultTransform($this->getMarkdown());

return array_merge(
$this->getConfig(),
parent::getViewVariables(),
[
'id' => $this->getFormElementId(),
'markdown' => $markdown,
'tooltip' => $this->parseTooltip(),
]
);
}
Expand Down
11 changes: 1 addition & 10 deletions src/FormGenerator/FormElements/UploadFormElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ class UploadFormElement extends AbstractDynamicFormElement
*/
protected $previousValue;

public function getValue()
{
return $this->value;
}

/**
* Update previous value on every upload.
* Also used when restoring persisted values.
Expand Down Expand Up @@ -145,15 +140,11 @@ public function getMaxSize()
public function getViewVariables()
{
return array_merge(
$this->getConfig(),
parent::getViewVariables(),
[
'id' => $this->getFormElementId(),
'is_uploaded' => $this->hasValue(),
'errors' => $this->getErrors(),
'allowed_extensions' => $this->getAllowedExtensionsAsArray(),
'value' => $this->getValue(),
'previous_value' => json_encode($this->getPreviousValue()),
'is_required' => $this->isRequired(),
'max_size' => $this->getMaxSize(),
'max_size_human' => FileHelper::getMaxSizeHuman($this->getMaxSize()),
]
Expand Down
2 changes: 1 addition & 1 deletion view/_macros.twig
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

{%- macro renderModal(modal, id) -%}
{%- if modal is not empty -%}
<button type="button" class="button is-small js-modal-trigger" data-target="modal-{{ id }}" style="{{ tooltip_style }}">?</button>
<button type="button" class="button is-small tooltip-button js-modal-trigger" data-target="modal-{{ id }}" style="{{ tooltip_style }}">?</button>
<div id="modal-{{ id }}" class="modal">
<div class="modal-background"></div>
<div class="modal-content">
Expand Down
3 changes: 2 additions & 1 deletion view/checklist.twig
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% from '_macros' import renderError, renderTooltip %}
{% from '_macros' import renderError, renderTooltip, renderModal %}
<div class="column {{ column is not empty ? column : 'is-full' }}">
<div class="field">
<div class="label-with-tooltip">
Expand All @@ -7,6 +7,7 @@
{{ label }} {{ is_required ? '*' }}
{%- endif -%}
{{ renderTooltip(tooltip) }}
{{ renderModal(modal, id) }}
</div>
</div>
<div class="control">
Expand Down
4 changes: 3 additions & 1 deletion view/date.twig
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% from '_macros' import renderError, renderTooltip %}
{% from '_macros' import renderError, renderTooltip, renderModal %}
<div class="column {{ column is not empty ? column : 'is-full' }}">
{% if clone is not empty %}
{% for val in value %}
Expand All @@ -10,6 +10,7 @@
{%- endif -%}
</label>
{{ renderTooltip(tooltip) }}
{{ renderModal(modal, id ~ loop.index) }}
</div>
<div class="control is-cloneable">
<input
Expand Down Expand Up @@ -41,6 +42,7 @@
{%- endif -%}
</label>
{{ renderTooltip(tooltip) }}
{{ renderModal(modal, id) }}
</div>
<div class="control">
<input
Expand Down
4 changes: 3 additions & 1 deletion view/datetime.twig
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% from '_macros' import renderError, renderTooltip %}
{% from '_macros' import renderError, renderTooltip, renderModal %}
<div class="column {{ column is not empty ? column : 'is-full' }}">
{% if clone is not empty %}
{% for val in value %}
Expand All @@ -10,6 +10,7 @@
{%- endif -%}
</label>
{{ renderTooltip(tooltip) }}
{{ renderModal(modal, id ~ loop.index) }}
</div>
<div class="control is-cloneable">
<input
Expand Down Expand Up @@ -41,6 +42,7 @@
{%- endif -%}
</label>
{{ renderTooltip(tooltip) }}
{{ renderModal(modal, id) }}
</div>
<div class="control">
<input
Expand Down
3 changes: 2 additions & 1 deletion view/download.twig
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{% from '_macros' import fileUrl, renderTooltip %}
{% from '_macros' import fileUrl, renderTooltip, renderModal %}
<div class="column {{ column is not empty ? column : 'is-full' }}">
<div class="field">
<div class="control">
<a href="{{ fileUrl(href) }}" target="_blank">{{ label }}</a>
{{ renderTooltip(tooltip) }}
{{ renderModal(modal, id) }}
</div>
</div>
</div>
3 changes: 2 additions & 1 deletion view/dropdown.twig
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% from '_macros' import renderError, renderTooltip %}
{% from '_macros' import renderError, renderTooltip, renderModal %}
<div class="column {{ column is not empty ? column : 'is-full' }}">
<div class="field">
<div class="label-with-tooltip">
Expand All @@ -8,6 +8,7 @@
{%- endif -%}
</label>
{{ renderTooltip(tooltip) }}
{{ renderModal(modal, id) }}
</div>
<div class="control">
<div class="select {{ multiselect is defined and multiselect ? 'is-multiple' }} {{ errors is not empty ? 'is-danger' }}">
Expand Down
4 changes: 3 additions & 1 deletion view/email.twig
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% from '_macros' import renderError, renderTooltip %}
{% from '_macros' import renderError, renderTooltip, renderModal %}
<div class="column {{ column is not empty ? column : 'is-full' }}">
{% if clone is not empty %}
{% for val in value %}
Expand All @@ -10,6 +10,7 @@
{%- endif -%}
</label>
{{ renderTooltip(tooltip) }}
{{ renderModal(modal, id ~ loop.index) }}
</div>
<div class="control is-cloneable">
<input
Expand Down Expand Up @@ -40,6 +41,7 @@
{%- endif -%}
</label>
{{ renderTooltip(tooltip) }}
{{ renderModal(modal, id) }}
</div>
<div class="control">
<input
Expand Down
3 changes: 2 additions & 1 deletion view/image.twig
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{% from '_macros' import fileUrl , renderError, renderTooltip %}
{% from '_macros' import fileUrl , renderError, renderTooltip, renderModal %}
<div class="column {{ column is not empty ? column : 'is-full' }}">
<div class="field">
<div class="label {{ labelsmall is not empty ? 'label-smaller' }}">{{ label }}</div>
{{ renderTooltip(tooltip) }}
{{ renderModal(modal, id) }}
<div class="control">
<img src="{{ fileUrl(src) }}" style="{{ width is defined ? 'width:'~width~'px;' }} {{ height is defined ? 'height:'~height~'px;' }}" alt="{{ alt is defined ? alt : label }}">
</div>
Expand Down
5 changes: 3 additions & 2 deletions view/markdown.twig
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{% from '_macros' import renderTooltip %}
{% from '_macros' import renderTooltip, renderModal %}
<div class="column {{ column is not empty ? column : 'is-full' }}">
<div class="field with-tooltip">
<div class="control">
{{ markdown | raw }}
{{ renderTooltip(tooltip) }}
{{ renderModal(modal, id) }}
</div>
</div>
</div>
</div>
4 changes: 3 additions & 1 deletion view/numberinput.twig
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% from '_macros' import renderError, renderTooltip %}
{% from '_macros' import renderError, renderTooltip, renderModal %}
<div class="column {{ column is not empty ? column : 'is-full' }}">
{% if clone is not empty %}
{% for val in value %}
Expand All @@ -10,6 +10,7 @@
{%- endif -%}
</label>
{{ renderTooltip(tooltip) }}
{{ renderModal(modal, id ~ loop.index) }}
</div>
<div class="control is-cloneable">
<input
Expand Down Expand Up @@ -40,6 +41,7 @@
{%- endif -%}
</label>
{{ renderTooltip(tooltip) }}
{{ renderModal(modal, id) }}
</div>
<div class="control">
<input
Expand Down
3 changes: 2 additions & 1 deletion view/signature.twig
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% from '_macros' import renderError, renderTooltip %}
{% from '_macros' import renderError, renderTooltip, renderModal %}
<div class="column {{ column is not empty ? column : 'is-full' }}">

{% if value %}
Expand All @@ -19,6 +19,7 @@
<div class="control">
<div class="label {{ labelsmall is not empty ? 'label-smaller' }}">{{ label }} {{ is_required ? '*' }}
{{ renderTooltip(tooltip) }}
{{ renderModal(modal, id) }}
</div>
<input type="hidden" id="{{ id }}" name="{{ id }}" value="{{ value }}" class="form-input"/>
<canvas></canvas>
Expand Down
3 changes: 2 additions & 1 deletion view/textarea.twig
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% from '_macros' import renderError, renderTooltip %}
{% from '_macros' import renderError, renderTooltip, renderModal %}
<div class="column {{ column is not empty ? column : 'is-full' }}">
<div class="field">
<label class="label {{ labelsmall is not empty ? 'label-smaller' }}" for="{{ id }}">
Expand All @@ -7,6 +7,7 @@
{%- endif -%}
</label>
{{ renderTooltip(tooltip) }}
{{ renderModal(modal, id) }}
<div class="control">
<textarea
id="{{ id }}"
Expand Down
4 changes: 3 additions & 1 deletion view/textinput.twig
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% from '_macros' import renderError, renderTooltip %}
{% from '_macros' import renderError, renderTooltip, renderModal %}
<div class="column {{ column is not empty ? column : 'is-full' }}">

{% if clone is not empty %}
Expand All @@ -9,6 +9,7 @@
{{ label }} {{ is_required ? '*' }}
{%- endif -%}
{{ renderTooltip(tooltip) }}
{{ renderModal(modal, id ~ loop.index) }}
</label>
<div class="control is-cloneable">
<input
Expand Down Expand Up @@ -39,6 +40,7 @@
{%- endif -%}
</label>
{{ renderTooltip(tooltip) }}
{{ renderModal(modal, id) }}
</div>
<div class="control">
<input
Expand Down
4 changes: 3 additions & 1 deletion view/time.twig
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% from '_macros' import renderError, renderTooltip %}
{% from '_macros' import renderError, renderTooltip, renderModal %}
<div class="column {{ column is not empty ? column : 'is-full' }}">
{% if clone is not empty %}
{% for val in value %}
Expand All @@ -10,6 +10,7 @@
{%- endif -%}
</label>
{{ renderTooltip(tooltip) }}
{{ renderModal(modal, id ~ loop.index) }}
</div>
<div class="control is-cloneable">
<input
Expand Down Expand Up @@ -41,6 +42,7 @@
{%- endif -%}
</label>
{{ renderTooltip(tooltip) }}
{{ renderModal(modal, id) }}
</div>
<div class="control">
<input
Expand Down
Loading

0 comments on commit 6e98940

Please sign in to comment.