Skip to content

Commit

Permalink
Merge pull request #39 from bobslee/conditional-visibility-selectboxes
Browse files Browse the repository at this point in the history
Refactored conditionally_visible method and implement in selectboxesComponent
  • Loading branch information
bobslee authored Sep 26, 2023
2 parents 42cd67f + 9d5181f commit 9febba3
Show file tree
Hide file tree
Showing 7 changed files with 792 additions and 341 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Changelog

## 1.2.2

Refactored the `Component` class `conditionally_visible` method, to call the following 2 methods which can be extended in component subclasses:
- `conditional_visible_json_when`
- `conditional_visible_json_logic`

Implemented the `conditional_visible_json_when` method for the `selectboxesComponent`.\
Extended the unittest `ConditionalVisibilitySimpleTestCase` with simple conditional visibility for the `selectboxesComponent`.

## 1.2.1

Fix `get_component_object` (Builder) method to handle `ModuleNotFoundError`.\
Expand Down
44 changes: 26 additions & 18 deletions formiodata/components/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,32 +287,40 @@ def conditionally_visible(self):
try:
cond = self.raw['conditional']
if cond.get('json'):
# Optional package
try:
from json_logic import jsonLogic
context = {'data': self._all_data}
try:
context['row'] = self.component_owner.row
except AttributeError:
pass # only datagrid rows have a "row" attribute
return jsonLogic(cond['json'], context)
except ImportError:
logger.warning(f'Could not load json logic extension; will not evaluate visibility of {self.__class__.__name__} {self.id} ("{self.key}")')
return True
return self.conditional_visible_json_logic()
elif cond.get('when'):
triggering_component = self.component_owner.input_components[cond['when']]
triggering_value = cond['eq']
if triggering_component.value == triggering_value:
return cond['show']
else:
return not cond['show']
return self.conditional_visible_when()
except KeyError:
# Unknown component or no 'when', 'eq' or 'show' property
pass

# By default, it's visible
return True

def conditional_visible_when(self):
cond = self.raw['conditional']
triggering_component = self.component_owner.input_components[cond['when']]
triggering_value = cond['eq']
if triggering_component.value == triggering_value:
return cond['show']
else:
return not cond['show']

def conditional_visible_json_logic(self):
# Optional package
try:
from json_logic import jsonLogic
context = {'data': self._all_data}
try:
context['row'] = self.component_owner.row
except AttributeError:
pass # only datagrid rows have a "row" attribute
cond = self.raw['conditional']
return jsonLogic(cond['json'], context)
except ImportError:
logger.warning(f'Could not load json logic extension; will not evaluate visibility of {self.__class__.__name__} {self.id} ("{self.key}")')
return True

@property
def is_visible(self):
conditional = self.raw.get('conditional')
Expand Down
11 changes: 11 additions & 0 deletions formiodata/components/selectboxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,14 @@ def values_labels(self):
val = {'key': b_val['value'], 'label': label, 'value': self.value.get(b_val['value'])}
values_labels[b_val['value']] = val
return values_labels

def conditional_visible_when(self):
cond = self.raw['conditional']
triggering_component = self.component_owner.input_components[cond['when']]
triggering_value = cond['eq']
if triggering_component.value and triggering_component.value.get(
triggering_value
):
return cond['show']
else:
return not cond['show']
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "formio-data"
version = "1.2.1"
version = "1.2.2"
homepage = "https://github.com/novacode-nl/python-formio-data"
description = "formio.js JSON-data API"
readme = "README.md"
Expand Down
Loading

0 comments on commit 9febba3

Please sign in to comment.