Skip to content
This repository has been archived by the owner on Nov 29, 2023. It is now read-only.

Commit

Permalink
Fix: global initialization of widgets where repeat count is zero
Browse files Browse the repository at this point in the history
  • Loading branch information
eyelidlessness committed Aug 15, 2023
1 parent f37eed2 commit a60a269
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/js/widgets-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
52 changes: 52 additions & 0 deletions test/forms/repeat-count-widget-global-init.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?xml version="1.0"?>
<h:html xmlns="http://www.w3.org/2002/xforms"
xmlns:ev="http://www.w3.org/2001/xml-events"
xmlns:h="http://www.w3.org/1999/xhtml"
xmlns:jr="http://openrosa.org/javarosa"
xmlns:odk="http://www.opendatakit.org/xforms"
xmlns:orx="http://openrosa.org/xforms"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<h:head>
<h:title>Global init of widgets in repeat count (default=0)</h:title>
<model odk:xforms-version="1.0.0">
<instance>
<data id="repeat-count-widget-global-init">
<rep-count/>
<rep jr:template="">
<grp_plot>
<plot_id/>
<plot_size/>
<plot_crops/>
</grp_plot>
</rep>
<rep>
<grp_plot>
<plot_id/>
<plot_size/>
<plot_crops/>
</grp_plot>
</rep>
<meta>
<instanceID/>
</meta>
</data>
</instance>
<bind nodeset="/data/rep-count" type="int"/>
<bind nodeset="/data/rep/dec" type="decimal"/>
<bind jr:preload="uid" nodeset="/data/meta/instanceID" readonly="true()" type="string"/>
</model>
</h:head>
<h:body>
<input ref="/data/rep-count">
<label>Repeat count</label>
</input>
<group ref="/data/rep">
<label>Rep</label>
<repeat jr:count="/data/rep-count" nodeset="/data/rep">
<input ref="/data/rep/dec">
<label>Decimal input</label>
</input>
</repeat>
</group>
</h:body>
</h:html>
23 changes: 23 additions & 0 deletions test/spec/widgets-controller.spec.js
Original file line number Diff line number Diff line change
@@ -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();
});
});

0 comments on commit a60a269

Please sign in to comment.