Skip to content

Commit

Permalink
Finish conditional choices
Browse files Browse the repository at this point in the history
  • Loading branch information
annda committed Jul 8, 2024
1 parent ebd59d0 commit 23a55aa
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
23 changes: 20 additions & 3 deletions doc/formelements.md
Original file line number Diff line number Diff line change
Expand Up @@ -308,11 +308,12 @@ Options:
Representation of a select input.

Options:
* `choices` _(required)_ - defines available options. Markdown ist supported.
* **EITHER** `choices` _(optionally required)_ - defines available options. Markdown is supported.
* **OR** `conditional_choices` _(optionally required)_ - defines options available if a condition is met (depending on the value of another form field). This option has priority over `choices`!
* `empty_label` _(optional)_ - a placeholder text shown if no value was chosen (e.g. "Please select"). **Note:** this is not a real option and has no value that could be saved.
* `multiselect` _(optional)_ - enables selecting multiple options
* `size` _(optional)_ - if multiselect is turned on this defines the number of rows shown, otherwise ignored
* `default` _(optional)_ : Preselects a choice. This is just triggered if the form was never saved before. **Preselect in toggles are not supported yet.** **BREAKING CHANGE until version 1.0.4 this parameter was used for empty_label**
* `default` _(optional)_ : Preselects a choice. This is only triggered if the form was never saved before. **Preselect in toggles are not supported yet.**

```yaml
<id>:
Expand All @@ -327,9 +328,25 @@ Options:
- second choice
```

```yaml
conditional_choices:
-
field: fieldsetLevel1.FieldsetLevel2.myFieldname
value: 'Value A'
choices:
- A1
- A2
- field: fieldsetLevel1.FieldsetLevel2.myFieldname
value: 'Value B'
choices:
- 10 B
- 20 B
- 30 B
```

### Checklist

Representation of a checkbox group.
Representation of a checkbox group.

Options:
* `alignment` _(optional)_ - sets the alignment of the checkboxes, possible values are `vertical` or `horizontal` (default)
Expand Down
5 changes: 5 additions & 0 deletions public/js/toggle.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ export function initToggles() {
function resetConditional(option, parent, triggerElement, toggleValue) {
if (toggleValue.includes(getFormInputValue(triggerElement))) {
option.removeAttribute('disabled');
option.removeAttribute('hidden');
} else {
option.setAttribute('disabled', true);
option.setAttribute('hidden', true);
option.setAttribute('selected', false);
option.setAttribute('checked', false);
}
Expand Down Expand Up @@ -46,6 +48,9 @@ export function initToggles() {

if (toggleValue.includes(getFormInputValue(triggerElement))) {
option.removeAttribute('disabled');
option.removeAttribute('hidden');
} else {
option.removeAttribute('selected');
}

triggerElement.addEventListener('change', function(e) {
Expand Down
2 changes: 1 addition & 1 deletion view/dropdown.twig
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
{% if conditional_choices is not empty %}
{%- for select in conditional_choices -%}
{%- for choice in select.choices -%}
<option disabled {{ choice in value ? 'selected' }} data-toggle-id="{{ select.field}}" data-toggle-value="{{ select.value }}">{{ choice }}</option>
<option hidden disabled {{ choice in value ? 'selected' }} data-toggle-id="{{ select.field}}" data-toggle-value="{{ select.value }}">{{ choice }}</option>
{%- endfor -%}
{%- endfor -%}
{% else %}
Expand Down

0 comments on commit 23a55aa

Please sign in to comment.