Skip to content

Commit

Permalink
Merge pull request #30 from ssciwr/observers-and-preconstruction
Browse files Browse the repository at this point in the history
Observers and preconstruction
  • Loading branch information
dokempf authored Feb 28, 2022
2 parents 324c0e7 + 68e35a0 commit e1aba64
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 22 deletions.
2 changes: 1 addition & 1 deletion ipywidgets_jsonschema/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from ipywidgets_jsonschema.form import Form

__version__ = "0.6.1"
__version__ = "0.7.0"
40 changes: 19 additions & 21 deletions ipywidgets_jsonschema/form.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,22 @@ def add_entry(_):
if element_size == schema["maxItems"]:
return

def trigger_observers():
# Adding or removing an entry to this widget should trigger all value change handlers.
# As we do not have a proper widget to register the handler, we trigger it
# ourselves. This should make proper use of traitlets.
for h, n, t in self._observers:
if t == "change" and (n is traitlets.All or "value" in as_tuple(n)):
h(
{
"name": "value",
"old": {},
"new": {},
"owner": None,
"type": "change",
}
)

# A new element should only be generated if we do not have an excess
# one stored in the elements list
if element_size == len(elements):
Expand All @@ -449,27 +465,6 @@ def add_entry(_):
icon="arrow-down", layout=ipywidgets.Layout(width="33%")
)

def trigger_observers():
# Adding or removing an entry to this widget should trigger all value change handlers.
# As we do not have a proper widget to register the handler, we trigger it
# ourselves. This should make proper use of traitlets.
for h, n, t in self._observers:
if t == "change" and (
n is traitlets.All or "value" in as_tuple(n)
):
h(
{
"name": "value",
"old": {},
"new": {},
"owner": None,
"type": "change",
}
)

# We trigger observers upon adding
trigger_observers()

def remove_entry(b):
nonlocal element_size

Expand Down Expand Up @@ -539,6 +534,9 @@ def _move(b):
)
)

# Trigger observes when item is added regardless whether it was preconstructed
trigger_observers()

# Maybe reset it to the default
elements[element_size].resetter()

Expand Down

0 comments on commit e1aba64

Please sign in to comment.