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

Commit a60a269

Browse files
Fix: global initialization of widgets where repeat count is zero
1 parent f37eed2 commit a60a269

File tree

3 files changed

+79
-4
lines changed

3 files changed

+79
-4
lines changed

src/js/widgets-controller.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,14 @@ function _instantiate(Widget, group) {
149149

150150
const elements = _getElements(group, Widget.selector);
151151

152-
if (!elements.length) {
153-
return;
154-
}
155-
156152
if (group === formElement) {
157153
Widget.globalInit(form, formElement);
158154
}
159155

156+
if (!elements.length) {
157+
return;
158+
}
159+
160160
new Collection(elements).instantiate(Widget, opts);
161161

162162
_setLangChangeListener(Widget, elements);
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?xml version="1.0"?>
2+
<h:html xmlns="http://www.w3.org/2002/xforms"
3+
xmlns:ev="http://www.w3.org/2001/xml-events"
4+
xmlns:h="http://www.w3.org/1999/xhtml"
5+
xmlns:jr="http://openrosa.org/javarosa"
6+
xmlns:odk="http://www.opendatakit.org/xforms"
7+
xmlns:orx="http://openrosa.org/xforms"
8+
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
9+
<h:head>
10+
<h:title>Global init of widgets in repeat count (default=0)</h:title>
11+
<model odk:xforms-version="1.0.0">
12+
<instance>
13+
<data id="repeat-count-widget-global-init">
14+
<rep-count/>
15+
<rep jr:template="">
16+
<grp_plot>
17+
<plot_id/>
18+
<plot_size/>
19+
<plot_crops/>
20+
</grp_plot>
21+
</rep>
22+
<rep>
23+
<grp_plot>
24+
<plot_id/>
25+
<plot_size/>
26+
<plot_crops/>
27+
</grp_plot>
28+
</rep>
29+
<meta>
30+
<instanceID/>
31+
</meta>
32+
</data>
33+
</instance>
34+
<bind nodeset="/data/rep-count" type="int"/>
35+
<bind nodeset="/data/rep/dec" type="decimal"/>
36+
<bind jr:preload="uid" nodeset="/data/meta/instanceID" readonly="true()" type="string"/>
37+
</model>
38+
</h:head>
39+
<h:body>
40+
<input ref="/data/rep-count">
41+
<label>Repeat count</label>
42+
</input>
43+
<group ref="/data/rep">
44+
<label>Rep</label>
45+
<repeat jr:count="/data/rep-count" nodeset="/data/rep">
46+
<input ref="/data/rep/dec">
47+
<label>Decimal input</label>
48+
</input>
49+
</repeat>
50+
</group>
51+
</h:body>
52+
</h:html>

test/spec/widgets-controller.spec.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import events from '../../src/js/event';
2+
import loadForm from '../helpers/load-form';
3+
4+
describe('Widgets controller', () => {
5+
it('globally initializes widgets which are only present in a repeat instance with count=0 on load', () => {
6+
const form = loadForm('repeat-count-widget-global-init.xml');
7+
8+
form.init();
9+
10+
const setRepeatCount = () => {
11+
/** @type {HTMLInputElement} */
12+
const countControl = form.view.html.querySelector(
13+
'input[name="/data/rep-count"]'
14+
);
15+
16+
countControl.value = '1';
17+
18+
countControl.dispatchEvent(events.Change());
19+
};
20+
21+
expect(setRepeatCount).not.to.throw();
22+
});
23+
});

0 commit comments

Comments
 (0)