From 75cc9899655e24ff2c1b857922c0f28e0fc47f5d Mon Sep 17 00:00:00 2001 From: eyelidlessness Date: Thu, 17 Aug 2023 12:17:41 -0700 Subject: [PATCH] Fix: global initialization of widgets where repeat count is zero (#994) --- src/js/widgets-controller.js | 8 +-- .../forms/repeat-count-widget-global-init.xml | 52 +++++++++++++++++++ test/spec/widgets-controller.spec.js | 23 ++++++++ 3 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 test/forms/repeat-count-widget-global-init.xml create mode 100644 test/spec/widgets-controller.spec.js diff --git a/src/js/widgets-controller.js b/src/js/widgets-controller.js index ef05e40c..a47c0315 100644 --- a/src/js/widgets-controller.js +++ b/src/js/widgets-controller.js @@ -149,14 +149,14 @@ function _instantiate(Widget, group) { const elements = _getElements(group, Widget.selector); - if (!elements.length) { - return; - } - if (group === formElement) { Widget.globalInit(form, formElement); } + if (!elements.length) { + return; + } + new Collection(elements).instantiate(Widget, opts); _setLangChangeListener(Widget, elements); diff --git a/test/forms/repeat-count-widget-global-init.xml b/test/forms/repeat-count-widget-global-init.xml new file mode 100644 index 00000000..5a6744f1 --- /dev/null +++ b/test/forms/repeat-count-widget-global-init.xml @@ -0,0 +1,52 @@ + + + + Global init of widgets in repeat count (default=0) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/test/spec/widgets-controller.spec.js b/test/spec/widgets-controller.spec.js new file mode 100644 index 00000000..90fca557 --- /dev/null +++ b/test/spec/widgets-controller.spec.js @@ -0,0 +1,23 @@ +import events from '../../src/js/event'; +import loadForm from '../helpers/load-form'; + +describe('Widgets controller', () => { + it('globally initializes widgets which are only present in a repeat instance with count=0 on load', () => { + const form = loadForm('repeat-count-widget-global-init.xml'); + + form.init(); + + const setRepeatCount = () => { + /** @type {HTMLInputElement} */ + const countControl = form.view.html.querySelector( + 'input[name="/data/rep-count"]' + ); + + countControl.value = '1'; + + countControl.dispatchEvent(events.Change()); + }; + + expect(setRepeatCount).not.to.throw(); + }); +});