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(); + }); +});